omnis 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/omnis/transformer.rb +5 -13
- data/lib/omnis/version.rb +1 -1
- data/spec/lib/omnis/transformer_spec.rb +21 -7
- metadata +1 -1
data/lib/omnis/transformer.rb
CHANGED
@@ -65,23 +65,15 @@ module Omnis
|
|
65
65
|
end
|
66
66
|
|
67
67
|
module InstanceMethods
|
68
|
-
def __extract(property, source)
|
69
|
-
value = property_value(property, source)
|
70
|
-
if property.format
|
71
|
-
property.format.call(value)
|
72
|
-
else
|
73
|
-
value
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
68
|
def property_value(property, source)
|
78
|
-
|
79
|
-
|
80
|
-
|
69
|
+
raw_value = property.extract(source)
|
70
|
+
raw_value = property.default if property.default && (raw_value == Nothing || raw_value.nil?)
|
71
|
+
applied_value = self.class.apply_to_value(raw_value)
|
72
|
+
property.format ? property.format.call(applied_value) : applied_value
|
81
73
|
end
|
82
74
|
|
83
75
|
def transform(source)
|
84
|
-
result = Hash[self.class.properties.map do |k, v| [k,
|
76
|
+
result = Hash[self.class.properties.map do |k, v| [k, property_value(v, source)] end]
|
85
77
|
respond_to?(:to_object) ? to_object(result) : result
|
86
78
|
end
|
87
79
|
|
data/lib/omnis/version.rb
CHANGED
@@ -74,14 +74,28 @@ describe Omnis::Transformer do
|
|
74
74
|
xformer.({}).should == {:ref => 'ref_value'}
|
75
75
|
end
|
76
76
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
77
|
+
context 'to_value' do
|
78
|
+
it 'use to_value if provided' do
|
79
|
+
class TestToValueTransformer
|
80
|
+
include Omnis::Transformer
|
81
|
+
property(:ref) { 'abc' }
|
82
|
+
to_value {|i| i.upcase }
|
83
|
+
end
|
84
|
+
xformer = TestToValueTransformer.new.to_proc
|
85
|
+
xformer.({}).should == { :ref => 'ABC' }
|
82
86
|
end
|
83
87
|
|
84
|
-
|
85
|
-
|
88
|
+
it 'format is applied after to_value' do
|
89
|
+
class TestToValueFormatTransformer
|
90
|
+
include Omnis::Transformer
|
91
|
+
property(:date, nil, :format => ->v { v.strftime('%Y-%m-%d') })
|
92
|
+
|
93
|
+
def self.date(a); Maybe(a); end
|
94
|
+
to_value {|i| i.fetch(Time.at(0)) }
|
95
|
+
end
|
96
|
+
xformer = TestToValueFormatTransformer.new.to_proc
|
97
|
+
xformer.(Time.local(2012, 11, 24, 21, 34)).should == { :date => "2012-11-24"}
|
98
|
+
xformer.(nil).should == { :date => "1970-01-01"}
|
99
|
+
end
|
86
100
|
end
|
87
101
|
end
|