Class: Wonkavision::MessageMapper::Map
- Inherits:
-
Hash
- Object
- Hash
- Wonkavision::MessageMapper::Map
show all
- Includes:
- IndifferentAccess
- Defined in:
- lib/wonkavision/message_mapper/map.rb
Instance Method Summary
collapse
-
#array(source, options = {}, &block) ⇒ Object
-
#boolean(*args) ⇒ Object
-
#child(source, options = {}, &block) ⇒ Object
-
#context ⇒ Object
-
#date(*args) ⇒ Object
-
#dollars(*args) ⇒ Object
(also: #dollar)
-
#duration(*args, &block) ⇒ Object
(also: #elapsed)
-
#exec(map_name, exec_context = self.context) ⇒ Object
(also: #import, #apply)
-
#execute(context, map_block, options = {}) ⇒ Object
-
#float(*args) ⇒ Object
-
#formats ⇒ Object
-
#from(context, &block) ⇒ Object
-
#ignore_nil! ⇒ Object
-
#initialize(context = nil) ⇒ Map
constructor
-
#int(*args) ⇒ Object
(also: #integer)
-
#iso8601(*args) ⇒ Object
-
#percent(*args) ⇒ Object
(also: #percentage)
-
#string(*args) ⇒ Object
-
#time(*args) ⇒ Object
-
#value(*args, &block) ⇒ Object
-
#write_nil! ⇒ Object
-
#yes_no(*args) ⇒ Object
#[], #[]=, #delete, #include?, #method_missing
Constructor Details
#initialize(context = nil) ⇒ Map
Returns a new instance of Map.
7
8
9
10
11
12
|
# File 'lib/wonkavision/message_mapper/map.rb', line 7
def initialize(context = nil)
@write_nils = true
@context_stack = []
@context_stack.push(context) if context
@formats = default_formats
end
|
Instance Method Details
#array(source, options = {}, &block) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/wonkavision/message_mapper/map.rb', line 76
def array(source,options={},&block)
if (source.is_a?(Hash))
field_name = source.keys[0]
ctx = source[field_name]
else
field_name = source
ctx = (context,field_name)
end
result = []
ctx = [ctx].compact.flatten
map_name = options.delete(:map_name)
ctx.each do |item|
if (map_name)
child = MessageMapper.execute(map_name,item)
else
child = Map.new(item)
child.instance_eval(&block)
end
result << child
end
set_value(field_name,result,options)
end
|
#boolean(*args) ⇒ Object
172
173
174
175
176
|
# File 'lib/wonkavision/message_mapper/map.rb', line 172
def boolean(*args)
value(*args) do
%w(true yes).include?(to_s.downcase) ? true : false
end
end
|
#child(source, options = {}, &block) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/wonkavision/message_mapper/map.rb', line 54
def child(source,options={},&block)
raise "Neither a block nor a map_name were provided to 'map'" unless (block || options.keys.include?(:map_name))
if (source.is_a?(Hash))
field_name = source.keys[0]
ctx = source[field_name]
else
field_name = source
ctx = (context,field_name)
end
if ctx
if (map_name = options.delete(:map_name))
child = MessageMapper.execute(map_name,ctx)
else
child = Map.new(ctx)
child.instance_eval(&block)
end
else
child = {}
end
set_value(field_name,child,options)
end
|
#context ⇒ Object
26
27
28
|
# File 'lib/wonkavision/message_mapper/map.rb', line 26
def context
@context_stack[-1]
end
|
#date(*args) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/wonkavision/message_mapper/map.rb', line 154
def date(*args)
value(*args) do
if kind_of?(Date)
self
elsif respond_to?(:to_date)
to_date
elsif (date_str=to_s) && date_str.length > 0
begin
Date.parse(date_str)
rescue
self
end
else
self
end
end
end
|
#dollars(*args) ⇒ Object
Also known as:
dollar
107
108
109
110
|
# File 'lib/wonkavision/message_mapper/map.rb', line 107
def dollars(*args)
args = add_options(args,:format=>:dollars)
float(*args)
end
|
#duration(*args, &block) ⇒ Object
Also known as:
elapsed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/wonkavision/message_mapper/map.rb', line 200
def duration(*args, &block)
opts = args. || {}
from = opts.delete(:from)
to = opts.delete(:to)
return nil unless from || to
from ||= Time.now; to ||= Time.now
unit = opts.delete(:in) || opts.delete(:unit) || :seconds
duration = convert_seconds(to-from,unit)
assignment = {args.shift => duration}
args << assignment << opts
value(*args, &block)
end
|
#exec(map_name, exec_context = self.context) ⇒ Object
Also known as:
import, apply
47
48
49
50
|
# File 'lib/wonkavision/message_mapper/map.rb', line 47
def exec(map_name,exec_context=self.context)
mapped = MessageMapper.execute(map_name,exec_context)
self.merge!(mapped) if mapped
end
|
#execute(context, map_block, options = {}) ⇒ Object
14
15
16
17
18
19
20
|
# File 'lib/wonkavision/message_mapper/map.rb', line 14
def execute(context,map_block,options={})
@write_nils = options[:write_nils].nil? ? true : options[:write_nils]
@context_stack.push(context)
instance_eval(&map_block)
@context_stack.clear
self
end
|
#float(*args) ⇒ Object
103
104
105
|
# File 'lib/wonkavision/message_mapper/map.rb', line 103
def float(*args)
value(*args){respond_to?(:to_f) ? to_f : self}
end
|
22
23
24
|
# File 'lib/wonkavision/message_mapper/map.rb', line 22
def formats
@formats
end
|
#from(context, &block) ⇒ Object
38
39
40
41
42
43
44
45
|
# File 'lib/wonkavision/message_mapper/map.rb', line 38
def from (context,&block)
raise "No block ws provided to 'from'" unless block
return if context.nil?
@context_stack.push(context)
instance_eval(&block)
@context_stack.pop
end
|
#ignore_nil! ⇒ Object
30
31
32
|
# File 'lib/wonkavision/message_mapper/map.rb', line 30
def ignore_nil!
@write_nils = false
end
|
#int(*args) ⇒ Object
Also known as:
integer
178
179
180
|
# File 'lib/wonkavision/message_mapper/map.rb', line 178
def int(*args)
value(*args){respond_to?(:to_i) ? to_i : self}
end
|
#iso8601(*args) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/wonkavision/message_mapper/map.rb', line 124
def iso8601(*args)
value(*args) do
if respond_to?(:strftime)
strftime("%Y-%m-%dT%H:%M:%S")
elsif respond_to?(:ToString)
ToString("yyyy-MM-ddTHH:mm:ss")
else
self
end
end
end
|
#percent(*args) ⇒ Object
Also known as:
percentage
113
114
115
116
|
# File 'lib/wonkavision/message_mapper/map.rb', line 113
def percent(*args)
args = add_options(args,:format=>:percent)
float(*args)
end
|
#string(*args) ⇒ Object
99
100
101
|
# File 'lib/wonkavision/message_mapper/map.rb', line 99
def string(*args)
value(*args) {to_s}
end
|
#time(*args) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/wonkavision/message_mapper/map.rb', line 136
def time(*args)
value(*args) do
if kind_of?(Time)
self
elsif respond_to?(:to_time)
to_time
elsif (time_str=to_s) && time_str.length > 0
begin
Time.parse(time_str)
rescue
self
end
else
self
end
end
end
|
#value(*args, &block) ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/wonkavision/message_mapper/map.rb', line 183
def value(*args,&block)
opts = args.length > 1 ? args. : {}
if (args.length == 1 && args[0].is_a?(Hash))
args[0].each do |key,value|
value = context.instance_eval(&value) if value.is_a?(Proc)
value = value.instance_eval(&block) if block
set_value(key,value,opts)
end
else
args.each do |field_name|
value = (context,field_name,block)
set_value(field_name,value,opts)
end
end
end
|
#write_nil! ⇒ Object
34
35
36
|
# File 'lib/wonkavision/message_mapper/map.rb', line 34
def write_nil!
@write_nils = true
end
|
#yes_no(*args) ⇒ Object
119
120
121
122
|
# File 'lib/wonkavision/message_mapper/map.rb', line 119
def yes_no(*args)
args = add_options(args,:format=>:yes_no)
value(*args)
end
|