Class: JSONBuilder::Member

Inherits:
Object
  • Object
show all
Defined in:
lib/json_builder/member.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Raises:



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

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/json_builder/member.rb', line 6

def key
  @key
end

#valueObject

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_sObject

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