Class: JSONBuilder::Member
- Inherits:
-
Object
- Object
- JSONBuilder::Member
- Defined in:
- lib/json_builder/member.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(key, scope, *args, &block) ⇒ Member
constructor
Public: Returns a key value pair for the stored value which could be an instance of JSONBuilder::Elements or JSONBuilder::Value.
-
#to_s ⇒ Object
Public: Returns a key value pair for the stored value which could be an instance of JSONBuilder::Elements or JSONBuilder::Value.
Constructor Details
#initialize(key, scope, *args, &block) ⇒ Member
Public: Returns a key value pair for the stored value which could be an instance of JSONBuilder::Elements or JSONBuilder::Value.
key - Used to generate the JSON member’s key. Can be a String or Symbol. scope - The view scope context for any variables. args - Can be an Array or other standard Ruby value. block - Yielding any block passed to the element.
Raises JSONBuilder::MissingKeyError if the key passed is nil. Returns instance of JSONBuilder::Member.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/json_builder/member.rb', line 18 def initialize(key, scope, *args, &block) raise MissingKeyError if key.nil? @key = key argument = args.shift if argument.is_a?(Array) || defined?(ActiveRecord::Relation) && argument.is_a?(ActiveRecord::Relation) @value = Elements.new(scope, argument, &block) else @value = Value.new(scope, argument, &block) end end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
6 7 8 |
# File 'lib/json_builder/member.rb', line 6 def key @key end |
#value ⇒ Object
Returns the value of attribute value.
6 7 8 |
# File 'lib/json_builder/member.rb', line 6 def value @value end |
Instance Method Details
#to_s ⇒ Object
Public: Returns a key value pair for the stored value which could be an instance of JSONBuilder::Elements or JSONBuilder::Value.
Returns a String.
35 36 37 |
# File 'lib/json_builder/member.rb', line 35 def to_s "\"#{@key}\": #{@value}" end |