lib/jsonapi/link_builder.rb in jsonapi-resources-0.7.1.beta1 vs lib/jsonapi/link_builder.rb in jsonapi-resources-0.7.1.beta2
- old
+ new
@@ -1,26 +1,29 @@
module JSONAPI
class LinkBuilder
attr_reader :base_url,
:primary_resource_klass,
- :route_formatter
+ :route_formatter,
+ :engine_name
def initialize(config = {})
@base_url = config[:base_url]
@primary_resource_klass = config[:primary_resource_klass]
@route_formatter = config[:route_formatter]
- @is_engine = !!engine_name
+ @engine_name = build_engine_name
+
+ # Warning: These make LinkBuilder non-thread-safe. That's not a problem with the
+ # request-specific way it's currently used, though.
+ @resources_path_cache = JSONAPI::NaiveCache.new do |source_klass|
+ formatted_module_path_from_class(source_klass) + format_route(source_klass._type.to_s)
+ end
end
def engine?
- @is_engine
+ !!@engine_name
end
- def engine_name
- @engine_name ||= build_engine_name
- end
-
def primary_resources_url
if engine?
engine_primary_resources_url
else
regular_primary_resources_url
@@ -94,43 +97,40 @@
end_path_name = klass._type.to_s
"#{ base_path_name }_#{ end_path_name }_path"
end
def format_route(route)
- route_formatter.format(route.to_s)
+ route_formatter.format(route)
end
def formatted_module_path_from_class(klass)
scopes = module_scopes_from_class(klass)
unless scopes.empty?
- "/#{ scopes.map(&:underscore).join('/') }/"
+ "/#{ scopes.map{ |scope| format_route(scope.to_s.underscore) }.join('/') }/"
else
"/"
end
end
def module_scopes_from_class(klass)
klass.name.to_s.split("::")[0...-1]
end
+ def regular_resources_path(source_klass)
+ @resources_path_cache.get(source_klass)
+ end
+
def regular_primary_resources_path
- [
- formatted_module_path_from_class(primary_resource_klass),
- format_route(primary_resource_klass._type.to_s),
- ].join
+ regular_resources_path(primary_resource_klass)
end
def regular_primary_resources_url
"#{ base_url }#{ regular_primary_resources_path }"
end
def regular_resource_path(source)
- [
- formatted_module_path_from_class(source.class),
- format_route(source.class._type.to_s),
- "/#{ source.id }",
- ].join
+ "#{regular_resources_path(source.class)}/#{source.id}"
end
def regular_resource_url(source)
"#{ base_url }#{ regular_resource_path(source) }"
end