Permalink
Browse files

Move 'code' to 'node' as primary keyword to use for custom nodes

  • Loading branch information...
1 parent f8b5b26 commit 8af3f6731fa16542ac6cc471c5b0892918b4ec01 @nesquena nesquena committed Dec 23, 2011
Showing with 19 additions and 19 deletions.
  1. +11 −11 lib/rabl/builder.rb
  2. +8 −8 lib/rabl/engine.rb
View
@@ -4,7 +4,7 @@ class 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 }
+ # :child_root => true, :node, :child, :glue, :extends }
def initialize(data, options={}, &block)
@options = options
@_scope = options[:scope]
@@ -24,10 +24,10 @@ def to_hash(options={})
@options[:attributes].each_pair do |attribute, name|
attribute(attribute, :as => name)
end if @options.has_key?(:attributes)
- # Code
- @options[:code].each do |settings|
- code(settings[:name], settings[:options], &settings[:block])
- end if @options.has_key?(:code)
+ # Node
+ @options[:node].each do |settings|
+ node(settings[:name], settings[:options], &settings[:block])
+ end if @options.has_key?(:node)
# Children
@options[:child].each do |settings|
child(settings[:data], settings[:options], &settings[:block])
@@ -56,11 +56,11 @@ def attribute(*args)
end
alias_method :attributes, :attribute
- # Creates an arbitrary code node that is included in the json output
+ # Creates an arbitrary node that is included in the json output
# node(:foo) { "bar" }
- # code(:foo) { "bar" }
- # code(:foo, :if => lambda { |m| m.foo.present? }) { "bar" }
- def code(name, options={}, &block)
+ # node(:foo) { "bar" }
+ # node(:foo, :if => lambda { |m| m.foo.present? }) { "bar" }
+ def node(name, options={}, &block)
return unless resolve_condition(options)
result = block.call(@_object)
if name.present?
@@ -69,7 +69,7 @@ def code(name, options={}, &block)
@_result.merge!(result) if result
end
end
- alias_method :node, :code
+ alias_method :code, :node
# Creates a child node that is included in json output
# child(@user) { attribute :full_name }
@@ -81,7 +81,7 @@ def child(data, options={}, &block)
include_root = is_collection?(object) && @options[:child_root] # child @users
engine_options = @options.slice(:child_root).merge(:root => include_root)
object = { object => name } if data.respond_to?(:each_pair) && object # child :users => :people
- @_result[name] = self.object_to_hash(object, engine_options, &block)
+ @_result[name] = self.object_to_hash(object, engine_options, &block)
end
# Glues data from a child node to the json_output
View
@@ -102,14 +102,14 @@ def attribute(*args)
end
alias_method :attributes, :attribute
- # Creates an arbitrary code node that is included in the json output
- # code(:foo) { "bar" }
- # code(:foo, :if => lambda { ... }) { "bar" }
- def code(name = nil, options={}, &block)
- @_options[:code] ||= []
- @_options[:code] << { :name => name, :options => options, :block => block }
+ # Creates an arbitrary node that is included in the json output.
+ # node(:foo) { "bar" }
+ # node(:foo, :if => lambda { ... }) { "bar" }
+ def node(name = nil, options={}, &block)
+ @_options[:node] ||= []
+ @_options[:node] << { :name => name, :options => options, :block => block }
end
- alias_method :node, :code
+ alias_method :code, :node
# Creates a child node that is included in json output
# child(@user) { attribute :full_name }
@@ -194,7 +194,7 @@ def copy_instance_variables_from(object, exclude = []) #:nodoc:
def clear_compile_state
@_options.delete(:extends)
@_options.delete(:attributes)
- @_options.delete(:code)
+ @_options.delete(:node)
@_options.delete(:child)
@_options.delete(:glue)
end

0 comments on commit 8af3f67

Please sign in to comment.