Class: Notator::JavascriptObjectNotation

Inherits:
BlankSlate show all
Defined in:
lib/notator/javascript_object_notation.rb

Overview


Instance Method Summary collapse

Methods inherited from BlankSlate

find_hidden_method, hide, reveal

Constructor Details

#initialize(options = {}) ⇒ JavascriptObjectNotation




12
13
14
15
16
17
18
19
20
# File 'lib/notator/javascript_object_notation.rb', line 12

def initialize( options = {} )
#-------------------------------
  
  @pretty = options[ :pretty ] || false;

  @context_stack = [];
  @context_stack.push( Hash.new )

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/notator/javascript_object_notation.rb', line 87

def method_missing( key, *args, &block )
#---------------------------------------

  raise 'A key-value pair cannot be added in the context of an array.' \
    if @context_stack.last.is_a?( Array )
  
  if block.nil?
    @context_stack.last[ key ] = args.first
  else
    hash = Hash.new
    @context_stack.last[ key ] = hash
    @context_stack.push( hash )
    block.call( self )
    @context_stack.pop
  end

end

Instance Method Details

#<<(json) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/notator/javascript_object_notation.rb', line 105

def <<( json )
#-------------
  
  hash = JSON[ json ]
  
  @context_stack.last.is_a?( Array ) ?
    @context_stack.last.push( hash ) :
    @context_stack.last.merge!( hash )
                 
end

#array!(key = nil, *args, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/notator/javascript_object_notation.rb', line 29

def array!( key = nil, *args, &block )
#-------------------------------------

  if block.nil?
    
    raise 'Thearray! method requires an array argument.' \
      unless args.first.is_a?( Array )
        
    @context_stack.last[ key ] = args.first
  
  else
    
    array = Array.new
    
    if @context_stack.last.is_a?( Hash )

      raise 'The array! method requires a key in the context of a hash.' \
        if key.nil?

      @context_stack.last[ key ] = array
      
    else  
        
      @context_stack.last.push( array );
      
    end
    
    @context_stack.push( array )
    block.call( self )
    @context_stack.pop
  
  end

end

#hash!(key, *args, &block) ⇒ Object Also known as: tag!



22
23
24
25
# File 'lib/notator/javascript_object_notation.rb', line 22

def hash!( key, *args, &block )
#------------------------------
  method_missing( key.to_sym, *args, &block )
end

#to_jsonObject



116
117
118
119
120
121
# File 'lib/notator/javascript_object_notation.rb', line 116

def to_json
#----------
  @pretty ? 
    JSON.pretty_generate( @context_stack.first ) :
    @context_stack.first.to_json
end

#value!(value = nil, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/notator/javascript_object_notation.rb', line 64

def value!( value = nil, &block )
#--------------------------------

  raise 'The value! method can only be called in the context of ' + 
        'an array.' \
    unless @context_stack.last.is_a?( Array )

  if block.nil?
        
    @context_stack.last.push( value )
          
  else
                
    hash = Hash.new
    @context_stack.last.push( hash )
    @context_stack.push( hash )
    block.call( self )
    @context_stack.pop
  
  end

end