Class: Argonaut::Generator
Instance Method Summary
collapse
Methods inherited from BlankSlate
find_hidden_method, hide, reveal
Constructor Details
#initialize(array_keys = [], &block) ⇒ Generator
instance_methods.each {|m| undef_method(m) unless %w(__id__ __send__ to_json to_xml instance_eval nil? is_a? class).include?(m.to_s)}
8
9
10
11
12
|
# File 'lib/argonaut/generator.rb', line 8
def initialize(array_keys=[], &block)
@array_keys = array_keys.map(&:to_sym)
@hash = {}
block.call(self)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, *values) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/argonaut/generator.rb', line 22
def method_missing(key, *values)
value = if block_given?
Generator.new(@array_keys) { |a| yield a }
else
values.size == 1 ? values.first : values
end
if @array_keys.include?(key) || @hash[key].is_a?(Array)
if @hash.include?(key)
@hash[key] << value
else
@hash[key] = [value]
end
elsif @hash[key].nil?
@hash[key] = value
else
@hash[key] = [@hash[key], value]
end
end
|
Instance Method Details
#array_keys!(array_keys) ⇒ Object
14
15
16
|
# File 'lib/argonaut/generator.rb', line 14
def array_keys!(array_keys)
@array_keys = array_keys.map(&:to_sym)
end
|
55
56
57
|
# File 'lib/argonaut/generator.rb', line 55
def inspect
@hash.inspect
end
|
#key(key, value) ⇒ Object
18
19
20
|
# File 'lib/argonaut/generator.rb', line 18
def key(key, value)
method_missing(key.to_sym, value)
end
|
#respond_to?(method) ⇒ Boolean
42
43
44
|
# File 'lib/argonaut/generator.rb', line 42
def respond_to?(method)
true
end
|
#to_json(filler = nil) ⇒ Object
Sometimes Rails adds a parameter
47
48
49
|
# File 'lib/argonaut/generator.rb', line 47
def to_json(filler=nil)
JSON.generate(@hash)
end
|
51
52
53
|
# File 'lib/argonaut/generator.rb', line 51
def to_s
@hash.to_s
end
|