unidata 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/unidata/field.rb CHANGED
@@ -1,11 +1,20 @@
1
1
  module Unidata
2
2
  class Field
3
- attr_reader :index, :name, :type, :formatter
3
+ attr_reader :index, :name, :type
4
4
 
5
- def initialize(index, name, type=String)
5
+ def initialize(index, name, type=String, options={})
6
6
  @index = [*index]
7
7
  @name = name
8
8
  @type = type
9
+ @default = options[:default]
10
+ end
11
+
12
+ def default
13
+ if @default.respond_to?(:call)
14
+ @default.call
15
+ else
16
+ @default
17
+ end
9
18
  end
10
19
 
11
20
  def typecast(value)
data/lib/unidata/model.rb CHANGED
@@ -20,8 +20,8 @@ module Unidata
20
20
  fields.keys.include?(name.to_sym)
21
21
  end
22
22
 
23
- def field(index, name, type=String)
24
- fields[name.to_sym] = Field.new(index, name, type)
23
+ def field(index, name, type=String, options={})
24
+ fields[name.to_sym] = Field.new(index, name, type, options)
25
25
  define_attribute_accessor(name)
26
26
  end
27
27
 
@@ -79,8 +79,9 @@ module Unidata
79
79
  end
80
80
 
81
81
  def initialize(attributes={})
82
- @attributes = {}
83
- attributes.each do |key,value|
82
+ initialize_attributes
83
+
84
+ attributes.each do |key, value|
84
85
  next unless self.class.field?(key.to_sym)
85
86
  write_attribute(key, value)
86
87
  end
@@ -92,13 +93,23 @@ module Unidata
92
93
  end
93
94
 
94
95
  private
96
+ def initialize_attributes
97
+ @attributes = {}
98
+
99
+ self.class.fields.each do |key, field|
100
+ write_attribute(key, field.default)
101
+ end
102
+ end
103
+
95
104
  def read_attribute(attribute_name)
96
105
  @attributes[attribute_name.to_sym]
97
106
  end
98
107
 
99
108
  def write_attribute(attribute_name, value)
100
109
  field = self.class.fields[attribute_name.to_sym]
101
- @attributes[attribute_name.to_sym] = field.typecast(value)
110
+ value = field.typecast(value) unless value.nil?
111
+
112
+ @attributes[attribute_name.to_sym] = value
102
113
  end
103
114
  end
104
115
  end
@@ -1,3 +1,3 @@
1
1
  module Unidata
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -20,6 +20,46 @@ describe Unidata::Field do
20
20
  field = subject.new(2, :name)
21
21
  field.type.should == String
22
22
  end
23
+
24
+ context 'when default value provided' do
25
+ it 'captures default value' do
26
+ field = subject.new(3, :status, String, :default => 'N')
27
+ field.default.should == 'N'
28
+ end
29
+ end
30
+
31
+ context 'when default value not provided' do
32
+ it 'does not set default value' do
33
+ field = subject.new(3, :status, String)
34
+ field.default.should be_nil
35
+ end
36
+ end
37
+ end
38
+
39
+ describe '#default' do
40
+ context 'when default is a proc/lambda/etc' do
41
+ it 'evaluates and returns result of default' do
42
+ field = subject.new(4, :created, Date, :default => proc { Date.today })
43
+ field.default.should == Date.today
44
+
45
+ field = subject.new(4, :created, Date, :default => lambda { Date.today })
46
+ field.default.should == Date.today
47
+ end
48
+ end
49
+
50
+ context 'when default is a not a proc/lambda/etc' do
51
+ it 'returns default' do
52
+ field = subject.new(3, :status, String, :default => 'active')
53
+ field.default.should == 'active'
54
+ end
55
+ end
56
+
57
+ context 'when no default defined' do
58
+ it 'returns nil' do
59
+ field = subject.new(2, :name)
60
+ field.default.should be_nil
61
+ end
62
+ end
23
63
  end
24
64
 
25
65
  describe '#typecast' do
@@ -12,6 +12,7 @@ describe Unidata::Model do
12
12
  field [4,1], :employer
13
13
  field [4,2], :job_title
14
14
  field [4,3], :salary, BigDecimal
15
+ field 5, :status, String, :default => 'active'
15
16
  end
16
17
 
17
18
  describe '.connection' do
@@ -22,11 +23,17 @@ describe Unidata::Model do
22
23
  end
23
24
 
24
25
  describe '.field' do
25
- it 'adds Field with given index, name, and type to fields hash' do
26
+ it 'adds Field with given index, name, type, and options to fields hash' do
26
27
  field = Record.fields[:age]
27
28
  field.index.should == [2]
28
29
  field.name.should == :age
29
30
  field.type.should == Integer
31
+
32
+ field = Record.fields[:status]
33
+ field.index.should == [5]
34
+ field.name.should == :status
35
+ field.type.should == String
36
+ field.default.should == 'active'
30
37
  end
31
38
 
32
39
  it 'defines attribute reader/writer for field' do
@@ -45,7 +52,8 @@ describe Unidata::Model do
45
52
  :birth_date => Date.today,
46
53
  :employer => 'Awesome Company',
47
54
  :job_title => 'Manager',
48
- :salary => BigDecimal.new('60_000.00')
55
+ :salary => BigDecimal.new('60_000.00'),
56
+ :status => 'inactive'
49
57
  )
50
58
  end
51
59
 
@@ -57,6 +65,7 @@ describe Unidata::Model do
57
65
  record.extract(4, 1).to_s.should == 'AWESOME COMPANY'
58
66
  record.extract(4, 2).to_s.should == 'MANAGER'
59
67
  record.extract(4, 3).to_s.should == '6000000'
68
+ record.extract(5).to_s.should == 'INACTIVE'
60
69
  end
61
70
 
62
71
  it 'skips id field' do
@@ -75,6 +84,7 @@ describe Unidata::Model do
75
84
  @record.replace 4, 1, 'AWESOME COMPANY'
76
85
  @record.replace 4, 2, 'MANAGER'
77
86
  @record.replace 4, 3, 6_000_000
87
+ @record.replace 5, 'INACTIVE'
78
88
  end
79
89
 
80
90
  it 'converts UniDynArray to model' do
@@ -85,6 +95,7 @@ describe Unidata::Model do
85
95
  obj.employer.should == 'AWESOME COMPANY'
86
96
  obj.job_title.should == 'MANAGER'
87
97
  obj.salary.should == BigDecimal.new('60_000.00')
98
+ obj.status.should == 'INACTIVE'
88
99
  end
89
100
 
90
101
  it 'skips id field' do
@@ -120,6 +131,7 @@ describe Unidata::Model do
120
131
  @record.replace 4, 1, 'AWESOME COMPANY'
121
132
  @record.replace 4, 2, 'MANAGER'
122
133
  @record.replace 4, 3, 6_000_000
134
+ @record.replace 5, 'INACTIVE'
123
135
 
124
136
  @connection = double('connection', :read => @record)
125
137
  Unidata.stub(:connection).and_return(@connection)
@@ -139,14 +151,21 @@ describe Unidata::Model do
139
151
  obj.employer.should == 'AWESOME COMPANY'
140
152
  obj.job_title.should == 'MANAGER'
141
153
  obj.salary.should == BigDecimal.new('60_000.00')
154
+ obj.status.should == 'INACTIVE'
142
155
  end
143
156
  end
144
157
 
145
158
  describe '#initialize' do
146
159
  it 'captures provied attributes' do
147
- instance = Record.new(:id => '123', :name => 'John Doe')
160
+ instance = Record.new(:id => '123', :name => 'John Doe', :status => 'inactive')
148
161
  instance.id.should == '123'
149
162
  instance.name.should == 'John Doe'
163
+ instance.status.should == 'inactive'
164
+ end
165
+
166
+ it 'defaults attributes if field defines a default value' do
167
+ instance = Record.new(:id => '123', :name => 'John Doe')
168
+ instance.status.should == 'active'
150
169
  end
151
170
 
152
171
  it 'ignores attributes that are not defined in fields' do
@@ -178,7 +197,8 @@ describe Unidata::Model do
178
197
  :birth_date => Date.today,
179
198
  :employer => 'Awesome Company',
180
199
  :job_title => 'Manager',
181
- :salary => BigDecimal.new('60_000.00')
200
+ :salary => BigDecimal.new('60_000.00'),
201
+ :status => 'inactive'
182
202
  )
183
203
 
184
204
  record = Unidata::UniDynArray.new
@@ -188,6 +208,7 @@ describe Unidata::Model do
188
208
  record.replace 4, 1, 'AWESOME COMPANY'
189
209
  record.replace 4, 2, 'MANAGER'
190
210
  record.replace 4, 3, 6_000_000
211
+ record.replace 5, 'INACTIVE'
191
212
 
192
213
  connection.should_receive(:write).with('TEST', '1234', record)
193
214
  obj.save
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: unidata
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jeremy Israelsen