Class: Rabl::Builder
Instance Method Summary collapse
-
#attribute(*args) ⇒ Object
(also: #attributes)
Indicates an attribute or method should be included in the json output attribute :foo, :as => “bar” attribute :foo => :bar.
-
#child(data, options = {}, &block) ⇒ Object
Creates a child node that is included in json output child(@user) { attribute :full_name } child(@user => :person) { … } child(@users => :people) { … }.
-
#code(name, options = {}, &block) ⇒ Object
(also: #node)
Creates an arbitrary code node that is included in the json output node(:foo) { “bar” } code(:foo) { “bar” } code(:foo, :if => lambda { |m| m.foo.present? }) { “bar” }.
-
#extends(file, options = {}, &block) ⇒ Object
Extends an existing rabl template with additional attributes in the block extends(“users/show”) { attribute :full_name }.
-
#glue(data, &block) ⇒ Object
Glues data from a child node to the json_output glue(@user) { attribute :full_name => :user_full_name }.
-
#initialize(data, options = {}, &block) ⇒ Builder
constructor
Constructs a new ejs hash based on given object and options options = { :format => “json”, :attributes, :root => true, :child_root => true, :code, :child, :glue, :extends }.
-
#to_hash(options = {}) ⇒ Object
Returns a hash representation of the data object to_hash(:root => true).
Methods included from Helpers
#data_name, #data_object, #fetch_source, #is_record?, #object_to_hash, #partial, #resolve_condition
Constructor Details
#initialize(data, options = {}, &block) ⇒ Builder
Constructs a new ejs hash based on given object and options options = { :format => “json”, :attributes, :root => true,
:child_root => true, :code, :child, :glue, :extends }
8 9 10 11 12 13 14 |
# File 'lib/rabl/builder.rb', line 8 def initialize(data, ={}, &block) = @_scope = [:scope] @_data = data @_object = data_object(data) @_result = {} end |
Instance Method Details
#attribute(*args) ⇒ Object Also known as: attributes
Indicates an attribute or method should be included in the json output attribute :foo, :as => “bar” attribute :foo => :bar
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rabl/builder.rb', line 47 def attribute(*args) if args.first.is_a?(Hash) args.first.each_pair { |k,v| self.attribute(k, :as => v) } else # array of attributes = args. args.each do |attribute| @_result[[:as] || attribute] = @_object.send(attribute) if @_object && @_object.respond_to?(attribute) end end end |
#child(data, options = {}, &block) ⇒ Object
Creates a child node that is included in json output child(@user) { attribute :full_name } child(@user => :person) { … } child(@users => :people) { … }
72 73 74 75 76 77 78 |
# File 'lib/rabl/builder.rb', line 72 def child(data, ={}, &block) return false unless data.present? name, object = data_name(data), data_object(data) include_root = object.respond_to?(:each) && [:child_root] # child @users object = { object => name } if data.respond_to?(:each_pair) && object # child :users => :people @_result[name] = self.object_to_hash(object, :root => include_root, &block) if resolve_condition() end |
#code(name, options = {}, &block) ⇒ Object Also known as: node
Creates an arbitrary code node that is included in the json output node(:foo) { “bar” } code(:foo) { “bar” } code(:foo, :if => lambda { |m| m.foo.present? }) { “bar” }
63 64 65 |
# File 'lib/rabl/builder.rb', line 63 def code(name, ={}, &block) @_result[name] = block.call(@_object) if resolve_condition() end |
#extends(file, options = {}, &block) ⇒ Object
Extends an existing rabl template with additional attributes in the block extends(“users/show”) { attribute :full_name }
91 92 93 94 95 |
# File 'lib/rabl/builder.rb', line 91 def extends(file, ={}, &block) = .merge(:object => @_object) result = self.partial(file, , &block) @_result.merge!(result) if result end |
#glue(data, &block) ⇒ Object
Glues data from a child node to the json_output glue(@user) { attribute :full_name => :user_full_name }
82 83 84 85 86 87 |
# File 'lib/rabl/builder.rb', line 82 def glue(data, &block) return false unless data.present? object = data_object(data) glued_attributes = self.object_to_hash(object, :root => false, &block) @_result.merge!(glued_attributes) if glued_attributes end |
#to_hash(options = {}) ⇒ Object
Returns a hash representation of the data object to_hash(:root => true)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rabl/builder.rb', line 18 def to_hash(={}) # Extends [:extends].each do |settings| extends(settings[:file], settings[:options], &settings[:block]) end if .has_key?(:extends) # Attributes [:attributes].each_pair do |attribute, name| attribute(attribute, :as => name) end if .has_key?(:attributes) # Code [:code].each_pair do |name, settings| code(name, settings[:options], &settings[:block]) end if .has_key?(:code) # Children [:child].each do |settings| child(settings[:data], settings[:options], &settings[:block]) end if .has_key?(:child) # Glues [:glue].each do |settings| glue(settings[:data], &settings[:block]) end if .has_key?(:glue) # Return Hash @_root_name ||= data_name(@_data) ([:root] || [:root]) && @_root_name ? { @_root_name => @_result } : @_result end |