Class: Rabl::Builder
- Inherits:
-
Object
- Object
- Rabl::Builder
- Defined in:
- lib/rabl/builder.rb
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) { … }.
-
#code(name, &block) ⇒ Object
Creates an arbitrary code node that is included in the json output code(:foo) { “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(object, options, &block) ⇒ Builder
constructor
Constructs a new ejs hash based on given object and options.
-
#to_hash(options = {}) ⇒ Object
Returns a hash representation of the data object to_hash(:root => true).
Constructor Details
#initialize(object, options, &block) ⇒ Builder
Constructs a new ejs hash based on given object and options
4 5 6 7 8 |
# File 'lib/rabl/builder.rb', line 4 def initialize(object, , &block) @_object = object @_result = {} @options = 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
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rabl/builder.rb', line 41 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.try(attribute) if @_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) { … }
62 63 64 65 66 |
# File 'lib/rabl/builder.rb', line 62 def child(data, ={}, &block) return false unless data.present? name, object = data_name(data), data_object(data) @_result[name] = self.object_to_hash(object, &block) end |
#code(name, &block) ⇒ Object
Creates an arbitrary code node that is included in the json output code(:foo) { “bar” }
55 56 57 |
# File 'lib/rabl/builder.rb', line 55 def code(name, &block) @_result[name] = block.call(@_object) end |
#extends(file, options = {}, &block) ⇒ Object
Extends an existing rabl template with additional attributes in the block extends(“users/show”) { attribute :full_name }
79 80 81 82 83 |
# File 'lib/rabl/builder.rb', line 79 def extends(file, ={}, &block) = .merge(:object => @_object) result = @options[:engine].partial(file, , &block) @_result.merge!(result) end |
#glue(data, &block) ⇒ Object
Glues data from a child node to the json_output glue(@user) { attribute :full_name => :user_full_name }
70 71 72 73 74 75 |
# File 'lib/rabl/builder.rb', line 70 def glue(data, &block) return false unless data.present? object = data_object(data) glued_attributes = self.object_to_hash(object, &block) @_result.merge!(glued_attributes) end |
#to_hash(options = {}) ⇒ Object
Returns a hash representation of the data object to_hash(:root => true)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rabl/builder.rb', line 12 def to_hash(={}) # Attributes @options[:attributes].each_pair do |attribute, name| attribute(attribute, :as => name) end if @options.has_key?(:attributes) # Code @options[:code].each_pair do |name, block| code(name, &block) end if @options.has_key?(:code) # Children @options[:child].each do |settings| child(settings[:data], settings[:options], &settings[:block]) end if @options.has_key?(:child) # Glues @options[:glue].each do |settings| glue(settings[:data], &settings[:block]) end if @options.has_key?(:glue) # Extends @options[:extends].each do |settings| extends(settings[:file], settings[:options], &settings[:block]) end if @options.has_key?(:extends) @_root_name ||= @_object.class.model_name.element (@options[:root] || [:root]) ? { @_root_name => @_result } : @_result end |