Class: JT
- Inherits:
-
Object
show all
- Defined in:
- lib/json-translator.rb
Class Method Summary
collapse
Instance Method Summary
collapse
-
#iterate(*args, &block) ⇒ Object
(also: #i)
-
#method_missing(name, *args, &block) ⇒ Object
-
#namespace(name, &block) ⇒ Object
(also: #n)
-
#scope(name, &block) ⇒ Object
(also: #s)
-
#set_value(method, name, *args, &block) ⇒ Object
-
#t(name, *args, &block) ⇒ Object
-
#translate(data, &block) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
22
23
24
|
# File 'lib/json-translator.rb', line 22
def method_missing(name, *args, &block)
set_value(name, name, *args, &block)
end
|
Class Method Details
.t(data, &block) ⇒ Object
98
99
100
|
# File 'lib/json-translator.rb', line 98
def self.t(data, &block)
JT.new.translate(data, &block)
end
|
Instance Method Details
#iterate(*args, &block) ⇒ Object
Also known as:
i
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/json-translator.rb', line 81
def iterate(*args, &block)
raise 'JT#iterate no block given' unless block_given?
raise 'JT#iterate expects 0 or 2 arguments' unless [0, 2].include? args.size
if args.empty?
@result_pointer.concat @data_pointer.map { |e| JT.new.translate(e, &block) }
else
raise 'JT#iterate expects symbols or strings' unless args.all? { |a| [Symbol, String].include? a.class }
@result_pointer[args.first] = @data_pointer[args.last.to_s].map { |e| JT.new.translate(e, &block) }
end
end
|
#namespace(name, &block) ⇒ Object
Also known as:
n
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/json-translator.rb', line 59
def namespace(name, &block)
raise 'JT#namespace no block given' unless block_given?
raise 'JT#namespace expects symbol or string' unless [Symbol, String].include? name.class
raise 'JT#namespace could not be called on array' unless @result_pointer.is_a? Hash
tmp = @result_pointer
case name
when Symbol
@result_pointer[name] ||= {}
@result_pointer = @result_pointer[name]
when String
name.split('.').each do |n|
@result_pointer[n] ||= {}
@result_pointer = @result_pointer[n]
end
end
instance_eval &block
@result_pointer = tmp
end
|
#scope(name, &block) ⇒ Object
Also known as:
s
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/json-translator.rb', line 41
def scope(name, &block)
raise 'JT#scope no block given' unless block_given?
raise 'JT#scope expects symbol or string' unless [Symbol, String].include? name.class
raise 'JT#scope could not be called on array' unless @data_pointer.is_a? Hash
tmp = @data_pointer
case name
when Symbol
@data_pointer = @data_pointer[name.to_s]
when String
name.split('.').each { |n| @data_pointer = @data_pointer[n] }
end
instance_eval &block
@data_pointer = tmp
end
|
#set_value(method, name, *args, &block) ⇒ Object
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/json-translator.rb', line 30
def set_value(method, name, *args, &block)
raise "JT##{method} expects 0 or 1 argument" if args.size > 1
raise "JT##{method} can't get key from #{@data_pointer.inspect}" unless @data_pointer.is_a? Hash
raise "JT##{method} can't set key to #{@result_pointer.inspect}" unless @result_pointer.is_a? Hash
value = @data_pointer[(args.first || name).to_s]
value = block.call(value) if block_given?
@result_pointer[name] = value
end
|
#t(name, *args, &block) ⇒ Object
26
27
28
|
# File 'lib/json-translator.rb', line 26
def t(name, *args, &block)
set_value(:t, name, *args, &block)
end
|
#translate(data, &block) ⇒ Object
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/json-translator.rb', line 4
def translate(data, &block)
raise 'JT#translate no block given' unless block_given?
data = JSON.parse(data) if data.is_a? String
raise 'JT#translate expects hash or array' unless [Hash, Array].include? data.class
@data = data
@data_pointer = @data
@result = @data.class.new
@result_pointer = @result
instance_eval &block
@result
end
|