unidata 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,4 +3,3 @@ require 'unidata/extensions/date'
3
3
  require 'unidata/extensions/float'
4
4
  require 'unidata/extensions/integer'
5
5
  require 'unidata/extensions/string'
6
- require 'unidata/extensions/time'
@@ -1,14 +1,19 @@
1
1
  require 'bigdecimal'
2
+ require 'bigdecimal/util'
2
3
 
3
4
  module Unidata
4
5
  module Extensions
5
6
  module BigDecimal
7
+ def typecast(value)
8
+ value.kind_of?(::BigDecimal) ? value : value.to_d
9
+ end
10
+
6
11
  def to_unidata(value)
7
12
  (value * 100).to_i
8
13
  end
9
14
 
10
15
  def from_unidata(value)
11
- ::BigDecimal.new(value.to_s) / 100
16
+ typecast(value) / 100
12
17
  end
13
18
  end
14
19
  end
@@ -5,6 +5,10 @@ module Unidata
5
5
  module Date
6
6
  PICK_EPOCH = ::Date.parse('1968-01-01')
7
7
 
8
+ def typecast(value)
9
+ value.kind_of?(::Date) ? value : value.to_date
10
+ end
11
+
8
12
  def to_unidata(value)
9
13
  (value - PICK_EPOCH).to_i
10
14
  end
@@ -1,12 +1,16 @@
1
1
  module Unidata
2
2
  module Extensions
3
3
  module Float
4
+ def typecast(value)
5
+ value.kind_of?(::Float) ? value : value.to_f
6
+ end
7
+
4
8
  def to_unidata(value)
5
- ::BigDecimal.to_unidata(value)
9
+ (value * 100).to_i
6
10
  end
7
11
 
8
12
  def from_unidata(value)
9
- ::BigDecimal.from_unidata(value).to_f
13
+ typecast(value) / 100
10
14
  end
11
15
  end
12
16
  end
@@ -1,12 +1,16 @@
1
1
  module Unidata
2
2
  module Extensions
3
3
  module Integer
4
+ def typecast(value)
5
+ value.kind_of?(::Integer) ? value : value.to_i
6
+ end
7
+
4
8
  def to_unidata(value)
5
9
  value
6
10
  end
7
11
 
8
12
  def from_unidata(value)
9
- value.to_i
13
+ typecast(value)
10
14
  end
11
15
  end
12
16
  end
@@ -14,4 +18,8 @@ end
14
18
 
15
19
  class Integer
16
20
  extend Unidata::Extensions::Integer
21
+
22
+ def to_d
23
+ BigDecimal.new(self.to_s)
24
+ end
17
25
  end
@@ -1,12 +1,16 @@
1
1
  module Unidata
2
2
  module Extensions
3
3
  module String
4
+ def typecast(value)
5
+ value.kind_of?(::String) ? value : value.to_s
6
+ end
7
+
4
8
  def to_unidata(value)
5
9
  value.upcase
6
10
  end
7
11
 
8
12
  def from_unidata(value)
9
- value
13
+ typecast(value)
10
14
  end
11
15
  end
12
16
  end
data/lib/unidata/field.rb CHANGED
@@ -8,6 +8,10 @@ module Unidata
8
8
  @type = type
9
9
  end
10
10
 
11
+ def typecast(value)
12
+ type.typecast(value)
13
+ end
14
+
11
15
  def to_unidata(value)
12
16
  type.to_unidata(value)
13
17
  end
data/lib/unidata/model.rb CHANGED
@@ -28,10 +28,11 @@ module Unidata
28
28
  def to_unidata(instance)
29
29
  record = Unidata::UniDynArray.new
30
30
 
31
- instance.attributes.each do |key, value|
31
+ fields.each do |key, field|
32
32
  next if key == :id
33
33
 
34
- field = fields[key]
34
+ value = instance.send(key)
35
+ next if value.nil?
35
36
  record.replace *field.index, field.to_unidata(value)
36
37
  end
37
38
 
@@ -44,10 +45,8 @@ module Unidata
44
45
  fields.each do |key, field|
45
46
  next if key == :id
46
47
 
47
- instance.send(
48
- "#{key}=",
49
- field.from_unidata(record.extract(*field.index).to_s)
50
- )
48
+ value = record.extract(*field.index).to_s
49
+ instance.send("#{key}=", field.from_unidata(value))
51
50
  end
52
51
 
53
52
  instance
@@ -79,13 +78,11 @@ module Unidata
79
78
  end
80
79
  end
81
80
 
82
- attr_reader :attributes
83
-
84
81
  def initialize(attributes={})
85
82
  @attributes = {}
86
83
  attributes.each do |key,value|
87
84
  next unless self.class.field?(key.to_sym)
88
- @attributes[key.to_sym] = value
85
+ write_attribute(key, value)
89
86
  end
90
87
  end
91
88
 
@@ -100,7 +97,8 @@ module Unidata
100
97
  end
101
98
 
102
99
  def write_attribute(attribute_name, value)
103
- @attributes[attribute_name.to_sym] = value
100
+ field = self.class.fields[attribute_name.to_sym]
101
+ @attributes[attribute_name.to_sym] = field.typecast(value)
104
102
  end
105
103
  end
106
104
  end
@@ -1,3 +1,3 @@
1
1
  module Unidata
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,7 @@ if ENV['COVERAGE']
6
6
  end
7
7
 
8
8
  require 'rspec'
9
+ require 'time'
9
10
  require 'unidata'
10
11
 
11
12
  def package_local_constructor klass,*values
@@ -22,14 +22,117 @@ describe Unidata::Field do
22
22
  end
23
23
  end
24
24
 
25
- describe '#to_unidata' do
26
- context 'when type is Time' do
27
- it 'converts value to pick time' do
28
- field = subject.new(1, :created_at, Time)
29
- field.to_unidata(Time.parse('2012-04-02 12:34:00')).should == 16163
25
+ describe '#typecast' do
26
+ context 'when type is Date' do
27
+ context 'when value is a Date' do
28
+ it 'returns value' do
29
+ value = Date.today
30
+
31
+ field = subject.new(1, :created_on, Date)
32
+ field.typecast(value).should == value
33
+ end
34
+ end
35
+
36
+ context 'when value is not a Date' do
37
+ it 'converts value to a Date' do
38
+ value = Time.parse('2012-01-01 12:00:00')
39
+
40
+ field = subject.new(1, :created_on, Date)
41
+ field.typecast(value).should == Date.parse('2012-01-01')
42
+ end
43
+ end
44
+ end
45
+
46
+ context 'when type is String' do
47
+ context 'when value is a String' do
48
+ it 'returns value' do
49
+ value = 'John Doe'
50
+
51
+ field = subject.new(1, :name, String)
52
+ field.typecast(value).should == value
53
+ end
54
+ end
55
+
56
+ context 'when value is not a String' do
57
+ it 'converts value to a String' do
58
+ value1 = Time.parse('2012-01-01 12:00:00')
59
+ value2 = 12345
60
+
61
+ field = subject.new(1, :name, String)
62
+ field.typecast(value1).should == value1.to_s
63
+ field.typecast(value2).should == '12345'
64
+ end
65
+ end
66
+ end
67
+
68
+ context 'when type is Float' do
69
+ context 'when value is a Float' do
70
+ it 'returns value' do
71
+ value = 123.45
72
+
73
+ field = subject.new(1, :price, Float)
74
+ field.typecast(value).should == value
75
+ end
76
+ end
77
+
78
+ context 'when value is not a Float' do
79
+ it 'converts value to a Float' do
80
+ value1 = BigDecimal.new('123.45')
81
+ value2 = 12345
82
+
83
+ field = subject.new(1, :price, Float)
84
+ field.typecast(value1).should == 123.45
85
+ field.typecast(value2).should == 12345.0
86
+ end
30
87
  end
31
88
  end
32
89
 
90
+ context 'when type is BigDecimal' do
91
+ context 'when value is a BigDecimal' do
92
+ it 'returns value' do
93
+ value = BigDecimal.new('123.45')
94
+
95
+ field = subject.new(1, :price, BigDecimal)
96
+ field.typecast(value).should == value
97
+ end
98
+ end
99
+
100
+ context 'when value is not a BigDecimal' do
101
+ it 'converts value to a BigDecimal' do
102
+ value1 = 123.45
103
+ value2 = 12345
104
+
105
+ field = subject.new(1, :price, BigDecimal)
106
+ field.typecast(value1).should == BigDecimal.new('123.45')
107
+ field.typecast(value2).should == BigDecimal.new('12345.0')
108
+ end
109
+ end
110
+ end
111
+
112
+ context 'when type is Integer' do
113
+ context 'when value is a Integer' do
114
+ it 'returns value' do
115
+ value = 123
116
+
117
+ field = subject.new(1, :price, Integer)
118
+ field.typecast(value).should == value
119
+ end
120
+ end
121
+
122
+ context 'when value is not a Integer' do
123
+ it 'converts value to a Integer' do
124
+ value1 = BigDecimal.new('123.45')
125
+ value2 = 12345.0
126
+
127
+ field = subject.new(1, :price, Integer)
128
+ field.typecast(value1).should == 123
129
+ field.typecast(value2).should == 12345
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ describe '#to_unidata' do
33
136
  context 'when type is Date' do
34
137
  it 'converts value to pick time' do
35
138
  field = subject.new(1, :created_on, Date)
@@ -67,13 +170,6 @@ describe Unidata::Field do
67
170
  end
68
171
 
69
172
  describe '#from_unidata' do
70
- context 'when type is Time' do
71
- it 'converts value from pick time' do
72
- field = subject.new(1, :created_at, Time)
73
- field.from_unidata(16163).should == Time.parse('2012-04-02 00:00:00')
74
- end
75
- end
76
-
77
173
  context 'when type is Date' do
78
174
  it 'converts value from pick time' do
79
175
  field = subject.new(1, :created_on, Date)
@@ -39,7 +39,7 @@ describe Unidata::Model do
39
39
  describe '.to_unidata' do
40
40
  before(:each) do
41
41
  @obj = Record.new(
42
- :id => 1234,
42
+ :id => '1234',
43
43
  :name => 'John Doe',
44
44
  :age => 25,
45
45
  :birth_date => Date.today,
@@ -68,7 +68,7 @@ describe Unidata::Model do
68
68
  describe '.from_unidata' do
69
69
  before(:each) do
70
70
  @record = Unidata::UniDynArray.new
71
- @record.replace 0, 123
71
+ @record.replace 0, '123'
72
72
  @record.replace 1, 'JOHN DOE'
73
73
  @record.replace 2, 25
74
74
  @record.replace 3, Date.to_unidata(Date.today)
@@ -96,18 +96,18 @@ describe Unidata::Model do
96
96
  describe '.exists?' do
97
97
  before(:each) do
98
98
  @connection = double('connection')
99
- @connection.stub(:exists?).with('TEST', 123).and_return(true)
100
- @connection.stub(:exists?).with('TEST', 234).and_return(false)
99
+ @connection.stub(:exists?).with('TEST', '123').and_return(true)
100
+ @connection.stub(:exists?).with('TEST', '234').and_return(false)
101
101
 
102
102
  Unidata.stub(:connection).and_return(@connection)
103
103
  end
104
104
 
105
105
  it 'returns true if record with id exists in file' do
106
- Record.exists?(123).should == true
106
+ Record.exists?('123').should == true
107
107
  end
108
108
 
109
109
  it 'returns false if record with id does not exist in file' do
110
- Record.exists?(234).should == false
110
+ Record.exists?('234').should == false
111
111
  end
112
112
  end
113
113
 
@@ -126,13 +126,13 @@ describe Unidata::Model do
126
126
  end
127
127
 
128
128
  it 'reads record from file' do
129
- @connection.should_receive(:read).with('TEST', 123).and_return(@record)
130
- Record.find(123)
129
+ @connection.should_receive(:read).with('TEST', '123').and_return(@record)
130
+ Record.find('123')
131
131
  end
132
132
 
133
133
  it 'returns model' do
134
134
  obj = Record.find(123)
135
- obj.id.should == 123
135
+ obj.id.should == '123'
136
136
  obj.name.should == 'JOHN DOE'
137
137
  obj.age.should == 25
138
138
  obj.birth_date.should == Date.today
@@ -144,13 +144,25 @@ describe Unidata::Model do
144
144
 
145
145
  describe '#initialize' do
146
146
  it 'captures provied attributes' do
147
- instance = Record.new(:id => 123, :name => 'John Doe')
148
- instance.attributes.should == { :id => 123, :name => 'John Doe' }
147
+ instance = Record.new(:id => '123', :name => 'John Doe')
148
+ instance.id.should == '123'
149
+ instance.name.should == 'John Doe'
149
150
  end
150
151
 
151
152
  it 'ignores attributes that are not defined in fields' do
152
- instance = Record.new(:id => 123, :name => 'John Doe', :nickname => 'J-Dog')
153
- instance.attributes.should == { :id => 123, :name => 'John Doe' }
153
+ instance = Record.new(:id => '123', :name => 'John Doe', :nickname => 'J-Dog')
154
+ instance.should_not respond_to(:nickname)
155
+ end
156
+
157
+ it 'typecasts attributes' do
158
+ instance = Record.new(
159
+ :age => '12',
160
+ :birth_date => Time.parse('2012-01-01 12:00:00'),
161
+ :salary => '5000.00'
162
+ )
163
+ instance.age.should == 12
164
+ instance.birth_date.should == Date.parse('2012-01-01')
165
+ instance.salary.should == BigDecimal.new('5000.00')
154
166
  end
155
167
  end
156
168
 
@@ -160,7 +172,7 @@ describe Unidata::Model do
160
172
  Unidata.stub(:connection).and_return(connection)
161
173
 
162
174
  obj = Record.new(
163
- :id => 1234,
175
+ :id => '1234',
164
176
  :name => 'John Doe',
165
177
  :age => 25,
166
178
  :birth_date => Date.today,
@@ -177,7 +189,7 @@ describe Unidata::Model do
177
189
  record.replace 4, 2, 'MANAGER'
178
190
  record.replace 4, 3, 6_000_000
179
191
 
180
- connection.should_receive(:write).with('TEST', 1234, record)
192
+ connection.should_receive(:write).with('TEST', '1234', record)
181
193
  obj.save
182
194
  end
183
195
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: unidata
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jeremy Israelsen
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-25 00:00:00.000000000 Z
12
+ date: 2012-06-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -75,7 +75,6 @@ files:
75
75
  - lib/unidata/extensions/float.rb
76
76
  - lib/unidata/extensions/integer.rb
77
77
  - lib/unidata/extensions/string.rb
78
- - lib/unidata/extensions/time.rb
79
78
  - lib/unidata/field.rb
80
79
  - lib/unidata/model.rb
81
80
  - lib/unidata/version.rb
@@ -1,19 +0,0 @@
1
- require 'time'
2
-
3
- module Unidata
4
- module Extensions
5
- module Time
6
- def to_unidata(value)
7
- ::Date.to_unidata(value.to_date)
8
- end
9
-
10
- def from_unidata(value)
11
- ::Date.from_unidata(value).to_time
12
- end
13
- end
14
- end
15
- end
16
-
17
- class Time
18
- extend Unidata::Extensions::Time
19
- end