Class: ActiveJson::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/activejson/activejson_core.rb

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Core

Returns a new instance of Core.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
# File 'lib/activejson/activejson_core.rb', line 9

def initialize()
  @hash = {}
  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *values) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/activejson/activejson_core.rb', line 14

def method_missing(key, *values)
  value = 
    if block_given?
      if values.size == 1
        values.first.map do |v|
          new_json_ele = ActiveJson::Core.new
          yield new_json_ele, v
          new_json_ele
        end
      else
        new_json_ele = ActiveJson::Core.new
        yield new_json_ele
        new_json_ele
      end
    else
      values.size == 1 ? values.first : values
    end

  if @hash[key].nil?
    @hash[key] = value
  elsif @hash[key].is_a? Array
    @hash[key] << value
  else
    @hash[key] = value
  end
end

Instance Method Details

#inspectObject



44
# File 'lib/activejson/activejson_core.rb', line 44

def inspect() @hash.inspect end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


41
# File 'lib/activejson/activejson_core.rb', line 41

def respond_to?(method) true end

#to_jsonObject



42
# File 'lib/activejson/activejson_core.rb', line 42

def to_json() MultiJson.encode(@hash) end

#to_sObject



43
# File 'lib/activejson/activejson_core.rb', line 43

def to_s() @hash.to_s end