Module: Rabl::Helpers
Instance Method Summary collapse
-
#data_name(data) ⇒ Object
data_name(data) => “user” data_name(@user => :person) => :person data_name(@users) => :user data_name() => “user” data_name([]) => “array”.
-
#data_object(data) ⇒ Object
data_object(data) => <AR Object> data_object(@user => :person) => @user data_object(:user => :person) => @_object.send(:user).
-
#fetch_source(file, options = {}) ⇒ Object
Returns source for a given relative file fetch_source(“show”, :view_path => “…”) => “…contents…”.
-
#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_to_hash(object, options = {}, &block) ⇒ Object
Returns a hash based representation of any data object given ejs template block object_to_hash(@user) { attribute :full_name } => { … } object_to_hash(@user, :source => “…”) { attribute :full_name } => { … } options must have :source (rabl file contents) options can have :source_location (source filename).
-
#partial(file, options = {}, &block) ⇒ Object
Renders a partial hash based on another rabl template partial(“users/show”, :object => @user) options must have :object options can have :view_path, :child_root, :root.
-
#resolve_condition(options) ⇒ Object
resolve_condition(:if => true) => true resolve_condition(:if => lambda { |m| false }) => false resolve_condition(:unless => lambda { |m| true }) => true.
Instance Method Details
#data_name(data) ⇒ Object
data_name(data) => “user” data_name(@user => :person) => :person data_name(@users) => :user data_name() => “user” data_name([]) => “array”
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rabl/helpers.rb', line 16 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 data.respond_to?(:first) data_name(data.first).pluralize if data.first.present? else # actual data object object_name = @_collection_name.to_s.singularize if defined? @_collection_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)
6 7 8 9 |
# File 'lib/rabl/helpers.rb', line 6 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 |
#fetch_source(file, options = {}) ⇒ Object
Returns source for a given relative file fetch_source(“show”, :view_path => “…”) => “…contents…”
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rabl/helpers.rb', line 76 def fetch_source(file, ={}) if defined? Rails root_path = Rails.root view_path = [:view_path] || File.join(root_path, "app/views/") file_path = Dir[File.join(view_path, file + ".{*.,}rabl")].first elsif defined? Padrino root_path = Padrino.root # use Padrino's own template resolution mechanism file_path, _ = @_scope.instance_eval { resolve_template(file) } # Padrino chops the extension, stitch it back on file_path = File.join(@_scope.settings.views, (file_path.to_s + ".rabl")) elsif defined? Sinatra view_path = [:view_path] || @_scope.settings.views file_path = Dir[File.join(view_path, file + ".{*.,}rabl")].first end if file_path return File.read(file_path.to_s), file_path.to_s else # no file path specified nil end end |
#is_collection?(obj) ⇒ Boolean
Returns true if the obj is a collection of items
70 71 72 |
# File 'lib/rabl/helpers.rb', line 70 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
65 66 67 |
# File 'lib/rabl/helpers.rb', line 65 def is_object?(obj) obj && !data_object(obj).respond_to?(:each) end |
#object_to_hash(object, options = {}, &block) ⇒ Object
Returns a hash based representation of any data object given ejs template block object_to_hash(@user) { attribute :full_name } => { … } object_to_hash(@user, :source => “…”) { attribute :full_name } => { … } options must have :source (rabl file contents) options can have :source_location (source filename)
45 46 47 48 49 |
# File 'lib/rabl/helpers.rb', line 45 def object_to_hash(object, ={}, &block) return object unless is_object?(object) || is_collection?(object) = .merge(:format => "hash", :root => ([:root] || false)) Rabl::Engine.new([:source], ).render(@_scope, :object => object, &block) end |
#partial(file, options = {}, &block) ⇒ Object
Renders a partial hash based on another rabl template partial(“users/show”, :object => @user) options must have :object options can have :view_path, :child_root, :root
33 34 35 36 37 38 |
# File 'lib/rabl/helpers.rb', line 33 def partial(file, ={}, &block) object, view_path = .delete(:object), .delete(:view_path) source, location = self.fetch_source(file, :view_path => view_path) = .merge(:source => source, :source_location => location) self.object_to_hash(object, , &block) end |
#resolve_condition(options) ⇒ Object
resolve_condition(:if => true) => true resolve_condition(:if => lambda { |m| false }) => false resolve_condition(:unless => lambda { |m| true }) => true
54 55 56 57 58 59 |
# File 'lib/rabl/helpers.rb', line 54 def resolve_condition() return true if [:if].nil? && [:unless].nil? result = [:if] == true || ([:if].respond_to?(:call) && [:if].call(@_object)) if .has_key?(:if) result = [:unless] == false || ([:unless].respond_to?(:call) && ![:unless].call(@_object)) if .has_key?(:unless) result end |