Module: Rabl::Helpers
- Included in:
- Partials
- Defined in:
- lib/rabl/helpers.rb
Instance Method Summary collapse
-
#collection_root_name ⇒ Object
Returns the root for the collection Sets the name of the collection i.e “people” => { “people” : [] }.
-
#context_scope ⇒ Object
Returns the scope wrapping this engine, used for retrieving data, invoking methods, etc In Rails, this is the controller and in Padrino this is the request context.
-
#data_name(data) ⇒ Object
data_name(data) => “user” data_name(@user => :person) => :person data_name(@users) => :user data_name() => “users” data_name([]) => “array”.
-
#data_object(data) ⇒ Object
data_object(data) => <AR Object> data_object(@user => :person) => @user data_object(:user => :person) => @_object.send(:user).
-
#determine_object_root(data, include_root = true) ⇒ Object
Returns the object rootname based on if the root should be included Can be called with data as a collection or object determine_object_root(@user, true) => “user” determine_object_root(@user => :person) => “person” determine_object_root([@user, @user]) => “user”.
-
#fetch_result_from_cache(cache_key, cache_options = nil, &block) ⇒ Object
Fetches a key from the cache and stores rabl template result otherwise fetch_from_cache(‘some_key’) { …rabl template result… }.
-
#is_collection?(obj) ⇒ Boolean
Returns true if the obj is a collection of items.
-
#is_object?(obj) ⇒ Boolean
Returns true if obj is not enumerable is_object?(@user) => true is_object?([]) => false is_object?({}) => false.
-
#object_root_name ⇒ Object
Returns the root (if any) name for an object within a collection Sets the name of the object i.e “person” => { “users” : [{ “person” : {} }] }.
-
#template_cache_configured? ⇒ Boolean
Returns true if the cache has been enabled for the application.
Instance Method Details
#collection_root_name ⇒ Object
Returns the root for the collection Sets the name of the collection i.e “people”
=> { "people" : [] }
77 78 79 |
# File 'lib/rabl/helpers.rb', line 77 def collection_root_name defined?(@_collection_name) ? @_collection_name : nil end |
#context_scope ⇒ Object
Returns the scope wrapping this engine, used for retrieving data, invoking methods, etc In Rails, this is the controller and in Padrino this is the request context
63 64 65 |
# File 'lib/rabl/helpers.rb', line 63 def context_scope defined?(@_scope) ? @_scope : nil end |
#data_name(data) ⇒ Object
data_name(data) => “user” data_name(@user => :person) => :person data_name(@users) => :user data_name() => “users” data_name([]) => “array”
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rabl/helpers.rb', line 19 def data_name(data) return nil unless data # nil or false return data.values.first if data.is_a?(Hash) # @user => :user data = @_object.send(data) if data.is_a?(Symbol) && @_object # :address if is_collection?(data) && data.respond_to?(:first) # data collection data_name(data.first).to_s.pluralize if data.first.present? elsif is_object?(data) # actual data object object_name = object_root_name if object_root_name object_name ||= collection_root_name.to_s.singularize if collection_root_name object_name ||= data.class.respond_to?(:model_name) ? data.class.model_name.element : data.class.to_s.downcase object_name end end |
#data_object(data) ⇒ Object
data_object(data) => <AR Object> data_object(@user => :person) => @user data_object(:user => :person) => @_object.send(:user)
9 10 11 12 |
# File 'lib/rabl/helpers.rb', line 9 def data_object(data) data = (data.is_a?(Hash) && data.keys.size == 1) ? data.keys.first : data data.is_a?(Symbol) && @_object ? @_object.send(data) : data end |
#determine_object_root(data, include_root = true) ⇒ Object
Returns the object rootname based on if the root should be included Can be called with data as a collection or object determine_object_root(@user, true) => “user” determine_object_root(@user => :person) => “person” determine_object_root([@user, @user]) => “user”
38 39 40 41 42 43 44 45 46 |
# File 'lib/rabl/helpers.rb', line 38 def determine_object_root(data, include_root=true) return if object_root_name == false root_name = data_name(data).to_s if include_root if is_object?(data) root_name elsif is_collection?(data) object_root_name || (root_name.singularize if root_name) end end |
#fetch_result_from_cache(cache_key, cache_options = nil, &block) ⇒ Object
Fetches a key from the cache and stores rabl template result otherwise fetch_from_cache(‘some_key’) { …rabl template result… }
83 84 85 86 |
# File 'lib/rabl/helpers.rb', line 83 def fetch_result_from_cache(cache_key, =nil, &block) = ActiveSupport::Cache.(cache_key, :rabl) Rails.cache.fetch(, , &block) if defined?(Rails) end |
#is_collection?(obj) ⇒ Boolean
Returns true if the obj is a collection of items
57 58 59 |
# File 'lib/rabl/helpers.rb', line 57 def is_collection?(obj) obj && data_object(obj).respond_to?(:each) end |
#is_object?(obj) ⇒ Boolean
Returns true if obj is not enumerable is_object?(@user) => true is_object?([]) => false is_object?({}) => false
52 53 54 |
# File 'lib/rabl/helpers.rb', line 52 def is_object?(obj) obj && !data_object(obj).respond_to?(:each) end |
#object_root_name ⇒ Object
Returns the root (if any) name for an object within a collection Sets the name of the object i.e “person”
> { “users” : [{ “person” : {} }] }
70 71 72 |
# File 'lib/rabl/helpers.rb', line 70 def object_root_name defined?(@_object_root_name) ? @_object_root_name : nil end |
#template_cache_configured? ⇒ Boolean
Returns true if the cache has been enabled for the application
89 90 91 |
# File 'lib/rabl/helpers.rb', line 89 def template_cache_configured? defined?(Rails) && defined?(ActionController) && ActionController::Base.perform_caching end |