Class: JSONBuilder::Value

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, arg, &block) ⇒ Value

Public: Creates

scope - The view scope context for any variables. arg - Could be string, hash, or any other Ruby value. block - Yielding any block passed to the element.

Returns an instance of JSONBuilder::Member, JSONBuilder::Compiler or String.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/json_builder/value.rb', line 15

def initialize(scope, arg, &block)
  if block_given?
    @value = Compiler.new(:scope => scope)
    compiled = @value.compile(arg, &block)

    # For the use case that the passed in block returns a non-member object
    # or normal Ruby object
    @value = compiled unless compiled.is_a?(Member)
  else
    @value = arg
  end
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



5
6
7
# File 'lib/json_builder/value.rb', line 5

def value
  @value
end

Instance Method Details

#to_sObject

Public: Determines of the stored value has a special return value or calls the default to_s on it.

Returns a String.



32
33
34
# File 'lib/json_builder/value.rb', line 32

def to_s
  @value.respond_to?(:to_builder) ? @value.to_builder : @value.to_s
end