omnis 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -0
- data/lib/omnis/transformer.rb +9 -1
- data/lib/omnis/version.rb +1 -1
- data/spec/lib/omnis/transformer_spec.rb +11 -0
- metadata +1 -7
data/README.md
CHANGED
@@ -94,6 +94,14 @@ class BookingTransformer
|
|
94
94
|
property :agency, "agency"
|
95
95
|
property :date_from, "services.0.date_from", :default => "n/a", :format => ->v { v.to_s(:date) }
|
96
96
|
property :date_to, "services.0.date_to", :default => "n/a", :format => ->v { v.to_s(:date) }
|
97
|
+
|
98
|
+
property :ref # if no extra params are provided it will call self.ref at runtime
|
99
|
+
|
100
|
+
to_value {|i| i.upcase } # apply this lambda to all extraced values
|
101
|
+
|
102
|
+
def self.ref(src)
|
103
|
+
extract(src, 'ref')
|
104
|
+
end
|
97
105
|
end
|
98
106
|
```
|
99
107
|
|
data/lib/omnis/transformer.rb
CHANGED
@@ -47,6 +47,14 @@ module Omnis
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
+
def to_value(&block)
|
51
|
+
@to_value = block
|
52
|
+
end
|
53
|
+
|
54
|
+
def apply_to_value(value)
|
55
|
+
@to_value ? @to_value.(value) : value
|
56
|
+
end
|
57
|
+
|
50
58
|
def extractor(obj)
|
51
59
|
@extractor = obj
|
52
60
|
end
|
@@ -69,7 +77,7 @@ module Omnis
|
|
69
77
|
def property_value(property, source)
|
70
78
|
value = property.extract(source)
|
71
79
|
return property.default if property.default && (value == Nothing || value.nil?)
|
72
|
-
return value
|
80
|
+
return self.class.apply_to_value(value)
|
73
81
|
end
|
74
82
|
|
75
83
|
def transform(source)
|
data/lib/omnis/version.rb
CHANGED
@@ -73,4 +73,15 @@ describe Omnis::Transformer do
|
|
73
73
|
xformer = TestBuiltInXformer.new.to_proc
|
74
74
|
xformer.({}).should == {:ref => 'ref_value'}
|
75
75
|
end
|
76
|
+
|
77
|
+
it 'use to_value if provided' do
|
78
|
+
class TestToValueTransformer
|
79
|
+
include Omnis::Transformer
|
80
|
+
property(:ref) { 'abc' }
|
81
|
+
to_value {|i| i.upcase }
|
82
|
+
end
|
83
|
+
|
84
|
+
xformer = TestToValueTransformer.new.to_proc
|
85
|
+
xformer.({}).should == { :ref => 'ABC' }
|
86
|
+
end
|
76
87
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omnis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -199,18 +199,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
199
199
|
- - ! '>='
|
200
200
|
- !ruby/object:Gem::Version
|
201
201
|
version: '0'
|
202
|
-
segments:
|
203
|
-
- 0
|
204
|
-
hash: -4082639984471618075
|
205
202
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
206
203
|
none: false
|
207
204
|
requirements:
|
208
205
|
- - ! '>='
|
209
206
|
- !ruby/object:Gem::Version
|
210
207
|
version: '0'
|
211
|
-
segments:
|
212
|
-
- 0
|
213
|
-
hash: -4082639984471618075
|
214
208
|
requirements: []
|
215
209
|
rubyforge_project:
|
216
210
|
rubygems_version: 1.8.23
|