attrio 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,14 +20,16 @@ describe Attrio::Reset do
20
20
  end
21
21
 
22
22
  it 'should respond_to reset_attributes' do
23
- object.respond_to?(:reset_attributes).should be_true
24
- object.respond_to?(:reset_attributes!).should be_true
23
+ object.respond_to?(:reset_attributes).should be_true
25
24
  end
26
25
 
27
- it 'should' do
26
+ it 'should reset attributes without :default option to nil' do
28
27
  object.reset_attributes
29
28
  object.first.should be_nil
30
- object.second.should == 'default'
31
29
  end
32
30
 
31
+ it 'should reset attributes with :default option to default value' do
32
+ object.reset_attributes
33
+ object.second.should == 'default'
34
+ end
33
35
  end
@@ -1,152 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "Type" do
4
- context '' do
5
- context '' do
6
- let(:model) do
7
- Class.new do
8
- include Attrio
9
-
10
- define_attributes do
11
- attr :attribute, Integer
12
- end
13
- end
14
- end
15
-
16
- let(:object) { model.new }
17
-
18
- it 'should' do
19
- object.attribute = 10
20
- object.attribute.is_a?(Integer).should be_true
21
- end
22
-
23
- it 'should' do
24
- object.attribute = 'string'
25
- object.attribute.should == 0
26
- end
27
- end
28
-
29
- context '' do
30
- let(:model) do
31
- Class.new do
32
- include Attrio
33
-
34
- define_attributes do
35
- attr :attribute, 'integer'
36
- end
37
- end
38
- end
39
-
40
- let(:object) { model.new }
41
-
42
- it 'should' do
43
- object.attribute = 10
44
- object.attribute.is_a?(Integer).should be_true
45
- end
46
-
47
- it 'should' do
48
- object.attribute = 'string'
49
- object.attribute.should == 0
50
- end
51
- end
52
-
53
- context '' do
54
- let(:model) do
55
- Class.new do
56
- include Attrio
57
-
58
- define_attributes do
59
- attr :attribute, :integer
60
- end
61
- end
62
- end
63
-
64
- let(:object) { model.new }
65
-
66
- it 'should' do
67
- object.attribute = 10
68
- object.attribute.is_a?(Integer).should be_true
69
- end
70
-
71
- it 'should' do
72
- object.attribute = 'string'
73
- object.attribute.should == 0
3
+ describe Attrio::Types::Base do
4
+ let(:model) do
5
+ Class.new do
6
+ include Attrio
7
+
8
+ define_attributes do
9
+ attr :type_as_constant, Integer
10
+ attr :type_in_options_as_constant, Integer
11
+ attr :type_as_string, 'integer'
12
+ attr :type_in_options_as_string, 'integer'
13
+ attr :type_as_symbol, :integer
14
+ attr :type_in_options_as_symbol, :integer
74
15
  end
75
16
  end
76
17
  end
77
18
 
78
- context '' do
79
- context '' do
80
- let(:model) do
81
- Class.new do
82
- include Attrio
83
-
84
- define_attributes do
85
- attr :attribute, :type => Integer
86
- end
87
- end
88
- end
89
-
90
- let(:object) { model.new }
91
-
92
- it 'should' do
93
- object.attribute = 10
94
- object.attribute.is_a?(Integer).should be_true
95
- end
96
-
97
- it 'should' do
98
- object.attribute = 'string'
99
- object.attribute.should == 0
100
- end
101
- end
102
-
103
- context '' do
104
- let(:model) do
105
- Class.new do
106
- include Attrio
107
-
108
- define_attributes do
109
- attr :attribute, :type => 'integer'
110
- end
111
- end
112
- end
113
-
114
- let(:object) { model.new }
115
-
116
- it 'should' do
117
- object.attribute = 10
118
- object.attribute.is_a?(Integer).should be_true
119
- end
120
-
121
- it 'should' do
122
- object.attribute = 'string'
123
- object.attribute.should == 0
124
- end
125
- end
126
-
127
- context '' do
128
- let(:model) do
129
- Class.new do
130
- include Attrio
131
-
132
- define_attributes do
133
- attr :attribute, :type => :integer
134
- end
135
- end
136
- end
137
-
138
- let(:object) { model.new }
139
-
140
- it 'should' do
141
- object.attribute = 10
142
- object.attribute.is_a?(Integer).should be_true
143
- end
144
-
145
- it 'should' do
146
- object.attribute = 'string'
147
- object.attribute.is_a?(Integer).should be_true
148
- object.attribute.should == 0
149
- end
150
- end
19
+ it 'should set appropriate type by all available options ' do
20
+ model.attributes.values.each { |attribute| attribute.type.should be(Attrio::Types::Integer) }
151
21
  end
152
22
  end
@@ -15,12 +15,12 @@ describe Attrio::Types::Integer do
15
15
  let(:object){ model.new }
16
16
 
17
17
  context 'with not typecasted assignment' do
18
- it 'should cast an object which implements method to_i' do
18
+ it 'should cast an object which has method to_i' do
19
19
  object.integer_attribute = "10 test"
20
20
  object.integer_attribute.should == 10
21
21
  end
22
22
 
23
- it 'should not cast an object which does not implement method to_i' do
23
+ it 'should not cast an object which has not method to_i' do
24
24
  lambda {
25
25
  object.integer_attribute = []
26
26
  }.should_not raise_exception
@@ -50,17 +50,17 @@ describe Attrio::Types::Integer do
50
50
  let(:object){ model.new }
51
51
 
52
52
  context 'with not typecasted assignment' do
53
- it 'should cast' do
53
+ it 'should cast <String> considering :base option' do
54
54
  object.integer_attribute = "A"
55
55
  object.integer_attribute.should == 10
56
56
  end
57
57
 
58
- it 'should cast an object which implements method to_i' do
58
+ it 'should cast an object which has method to_i' do
59
59
  object.integer_attribute = 10.0
60
60
  object.integer_attribute.should == 10
61
61
  end
62
62
 
63
- it 'should not cast an object which does not implement method to_i' do
63
+ it 'should not cast an object which has not method to_i' do
64
64
  lambda {
65
65
  object.integer_attribute = []
66
66
  }.should_not raise_exception
@@ -16,11 +16,49 @@ describe Attrio::Types::Symbol do
16
16
 
17
17
  context 'with not typecasted assignment' do
18
18
  it 'should cast <String>' do
19
- object.symbol_attribute = "symbol"
20
- object.symbol_attribute.should == :symbol
19
+ object.symbol_attribute = "CamelCase"
20
+ object.symbol_attribute.should == :CamelCase
21
21
  end
22
22
 
23
- it 'should not cast object which has not method to_sym' do
23
+ it 'should not cast an object which has not method to_sym' do
24
+ lambda {
25
+ object.symbol_attribute = []
26
+ }.should_not raise_exception
27
+ object.symbol_attribute.should be_nil
28
+ end
29
+ end
30
+
31
+ context 'with typecasted assignment' do
32
+ it 'should assign <Symbol>' do
33
+ symbol = :symbol
34
+
35
+ object.symbol_attribute = symbol
36
+ object.symbol_attribute.should == symbol
37
+ object.symbol_attribute.should be_equal(symbol)
38
+ end
39
+ end
40
+ end
41
+
42
+ context ':underscore option passed' do
43
+ let(:model) do
44
+ Class.new do
45
+ include Attrio
46
+
47
+ define_attributes do
48
+ attr :symbol_attribute, Symbol, :underscore => true
49
+ end
50
+ end
51
+ end
52
+
53
+ let(:object){ model.new }
54
+
55
+ context 'with not typecasted assignment' do
56
+ it 'should cast <String>' do
57
+ object.symbol_attribute = "CamelCase"
58
+ object.symbol_attribute.should == :camel_case
59
+ end
60
+
61
+ it 'should not cast an object which has not method to_sym' do
24
62
  lambda {
25
63
  object.symbol_attribute = []
26
64
  }.should_not raise_exception
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attrio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-05-23 00:00:00.000000000 Z
13
+ date: 2013-05-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -151,6 +151,11 @@ files:
151
151
  - lib/attrio/core_ext/object.rb
152
152
  - lib/attrio/core_ext/string.rb
153
153
  - lib/attrio/core_ext/time.rb
154
+ - lib/attrio/default_value.rb
155
+ - lib/attrio/default_value/base.rb
156
+ - lib/attrio/default_value/callable.rb
157
+ - lib/attrio/default_value/clonable.rb
158
+ - lib/attrio/default_value/symbol.rb
154
159
  - lib/attrio/initialize.rb
155
160
  - lib/attrio/inspect.rb
156
161
  - lib/attrio/reset.rb
@@ -164,7 +169,12 @@ files:
164
169
  - lib/attrio/types/time.rb
165
170
  - lib/attrio/version.rb
166
171
  - spec/attrio/attrio_spec.rb
167
- - spec/attrio/default_value_spec.rb
172
+ - spec/attrio/default_value/callable_spec.rb
173
+ - spec/attrio/default_value/clonable_spec.rb
174
+ - spec/attrio/default_value/handle_spec.rb
175
+ - spec/attrio/default_value/symbol_spec.rb
176
+ - spec/attrio/default_value/value_spec.rb
177
+ - spec/attrio/embed_value_spec.rb
168
178
  - spec/attrio/inspect_spec.rb
169
179
  - spec/attrio/reset_spec.rb
170
180
  - spec/attrio/type_spec.rb
@@ -190,7 +200,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
190
200
  version: '0'
191
201
  segments:
192
202
  - 0
193
- hash: 1888202450921559517
203
+ hash: 2070292704708193097
194
204
  required_rubygems_version: !ruby/object:Gem::Requirement
195
205
  none: false
196
206
  requirements:
@@ -199,7 +209,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
199
209
  version: '0'
200
210
  segments:
201
211
  - 0
202
- hash: 1888202450921559517
212
+ hash: 2070292704708193097
203
213
  requirements: []
204
214
  rubyforge_project:
205
215
  rubygems_version: 1.8.24
@@ -209,7 +219,12 @@ summary: Attributes for plain old Ruby objects. No dependencies, only simplicity
209
219
  clearness.
210
220
  test_files:
211
221
  - spec/attrio/attrio_spec.rb
212
- - spec/attrio/default_value_spec.rb
222
+ - spec/attrio/default_value/callable_spec.rb
223
+ - spec/attrio/default_value/clonable_spec.rb
224
+ - spec/attrio/default_value/handle_spec.rb
225
+ - spec/attrio/default_value/symbol_spec.rb
226
+ - spec/attrio/default_value/value_spec.rb
227
+ - spec/attrio/embed_value_spec.rb
213
228
  - spec/attrio/inspect_spec.rb
214
229
  - spec/attrio/reset_spec.rb
215
230
  - spec/attrio/type_spec.rb
@@ -1,60 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "DefaultValue" do
4
- context "without 'default' option" do
5
- let(:model) do
6
- Class.new do
7
- include Attrio
8
-
9
- define_attributes do
10
- attr :attribute, Date
11
- end
12
- end
13
- end
14
-
15
- let(:object){ model.new }
16
-
17
- it "should set variable to nil" do
18
- object.attribute.should be_nil
19
- end
20
- end
21
-
22
- context "without 'default' option" do
23
- context 'with not typecasted assignment' do
24
- let(:model) do
25
- Class.new do
26
- include Attrio
27
-
28
- define_attributes do
29
- attr :attribute, Date, :default => Date.today.to_s
30
- end
31
- end
32
- end
33
-
34
- let(:object){ model.new }
35
-
36
- it "should set variable to default value" do
37
- object.attribute.should == Date.today
38
- end
39
- end
40
-
41
- context 'with typecasted assignment' do
42
- let(:model) do
43
- Class.new do
44
- include Attrio
45
-
46
- define_attributes do
47
- attr :attribute, Date, :default => Date.today
48
- end
49
- end
50
- end
51
-
52
- let(:object){ model.new }
53
-
54
- it "should set variable to default value" do
55
- object.attribute.should == Date.today
56
- end
57
- end
58
- end
59
-
60
- end