omnis 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- value = property.extract(source)
79
- return property.default if property.default && (value == Nothing || value.nil?)
80
- return self.class.apply_to_value(value)
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, __extract(v, source)] end]
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
 
@@ -1,3 +1,3 @@
1
1
  module Omnis
2
- VERSION = "0.4.2"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -74,14 +74,28 @@ describe Omnis::Transformer do
74
74
  xformer.({}).should == {:ref => 'ref_value'}
75
75
  end
76
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 }
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
- xformer = TestToValueTransformer.new.to_proc
85
- xformer.({}).should == { :ref => 'ABC' }
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
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.2
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: