attrio 0.1.0 → 0.1.1
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/README.md +168 -0
- data/attrio.gemspec +1 -1
- data/lib/attrio.rb +3 -1
- data/lib/attrio/attributes_parser.rb +1 -1
- data/lib/attrio/core_ext/nil_object.rb +7 -0
- data/lib/attrio/core_ext/object.rb +8 -0
- data/lib/attrio/core_ext/time.rb +17 -0
- data/lib/attrio/types/integer.rb +4 -1
- data/lib/attrio/types/symbol.rb +2 -1
- data/lib/attrio/version.rb +1 -1
- data/spec/attrio/attrio_spec.rb +69 -0
- data/spec/attrio/default_value_spec.rb +60 -0
- data/spec/attrio/inspect_spec.rb +41 -0
- data/spec/attrio/reset_spec.rb +33 -0
- data/spec/attrio/type_spec.rb +152 -0
- data/spec/attrio/types/date_time_spec.rb +2 -2
- data/spec/attrio/types/integer_spec.rb +43 -3
- metadata +68 -22
- data/spec/attrio/initialize_spec.rb +0 -29
data/README.md
CHANGED
@@ -22,6 +22,174 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
$ gem install attrio
|
24
24
|
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Include Attrio into your class and then use `#define_attributes` block to declare you attributes.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class User
|
31
|
+
include Attrio
|
32
|
+
|
33
|
+
define_attributes do
|
34
|
+
attr :name, String
|
35
|
+
attr :age, Integer
|
36
|
+
attr :birthday, DateTime
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
By default Attrio creates `#attributes` accessor which contains `Hash` of with attributes names as keys and instances of `Attrio::Attribute` as values. Each instance of `Attrio::Attribute` contains following information:
|
42
|
+
* type
|
43
|
+
* writer method name
|
44
|
+
* writer method visibility
|
45
|
+
* reader method name
|
46
|
+
* reader method visibility
|
47
|
+
* instance variable name
|
48
|
+
* additional options
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
user = User.new
|
52
|
+
user.attributes
|
53
|
+
# => {
|
54
|
+
# :name => #<Attrio::Attribute:0x007fc44e8ca680 @object=#<User:0x007fc44e8b2b48>, @name="name", @type=String, @options={}, @writer_method_name="name=", @writer_visibility=:public, @instance_variable_name="@name", @reader_method_name="name", @reader_visibility=:public>,
|
55
|
+
# :age => #<Attrio::Attribute:0x007fc44e8d4c98 @object=#<User:0x007fc44e8b2b48>, @name="age", @type=Attrio::Types::Integer, @options={}, @writer_method_name="age=", @writer_visibility=:public, @instance_variable_name="@age", @reader_method_name="age", @reader_visibility=:public>,
|
56
|
+
# :birthday = >#<Attrio::Attribute:0x007fc44e8e2e38 @object=#<User:0x007fc44e8b2b48>, @name="birthday", @type=Attrio::Types::DateTime, @options={}, @writer_method_name="birthday=", @writer_visibility=:public, @instance_variable_name="@birthday", @reader_method_name="birthday", @reader_visibility=:public>
|
57
|
+
# }
|
58
|
+
```
|
59
|
+
|
60
|
+
Accessor name can be easily overriden by passing `:as` option to `define_attributes` block.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class User
|
64
|
+
include Attrio
|
65
|
+
|
66
|
+
define_attributes :as => 'api_attributes' do
|
67
|
+
attr :name, String
|
68
|
+
attr :age, Integer
|
69
|
+
attr :birthday, DateTime
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
user = User.new
|
76
|
+
user.api_attributes # => {...}
|
77
|
+
```
|
78
|
+
|
79
|
+
### Types
|
80
|
+
|
81
|
+
Any Ruby class can be passed as type to Attrio. If this class responds to `typecast` and `typecasted?` methods then they will be called, else `new` will be called.
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
class Klass
|
85
|
+
include Attrio
|
86
|
+
|
87
|
+
define_attributes do
|
88
|
+
attr :custom_attribute, CustomClass
|
89
|
+
end
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
### Built-in Types
|
94
|
+
|
95
|
+
**Boolean**
|
96
|
+
|
97
|
+
By default boolean typecasts 'yes', '1', 1, 'true' as `TrueClass` and all other values as `FalseClass`, but you easily modify this behaviour.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class Klass
|
101
|
+
include Attrio
|
102
|
+
|
103
|
+
define_attributes do
|
104
|
+
attr :boolean_attribute, Boolean
|
105
|
+
|
106
|
+
attr :custom_boolean_attribute, Boolean, :yes => ['ja', '1', 1]
|
107
|
+
# attr :custom_boolean_attribute, Boolean, :yes_values => ['ja', '1', 1]
|
108
|
+
# attr :custom_boolean_attribute, Boolean, :no => ['nein', '0', 0]
|
109
|
+
# attr :custom_boolean_attribute, Boolean, :no_values => ['nein', '0', 0]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
**Date, Time and DateTime**
|
115
|
+
|
116
|
+
These three class have similar behaviour and options. By passing `:format` option you can setup how `strftime` method will try to parse your string.
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
class Klass
|
120
|
+
include Attrio
|
121
|
+
|
122
|
+
define_attributes do
|
123
|
+
attr :date_attribute, Date
|
124
|
+
attr :time_attribute, Time
|
125
|
+
attr :date_time_attribute, DateTime
|
126
|
+
|
127
|
+
attr :custom_date_time_attribute, DateTime, :format => '%m/%d/%y-%H:%M:%S-%z'
|
128
|
+
end
|
129
|
+
end
|
130
|
+
```
|
131
|
+
|
132
|
+
**Float**
|
133
|
+
|
134
|
+
Attribute will be typecasted using `to_f` method.
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
class Klass
|
138
|
+
include Attrio
|
139
|
+
|
140
|
+
define_attributes do
|
141
|
+
attr :float_attribute, Float
|
142
|
+
end
|
143
|
+
end
|
144
|
+
```
|
145
|
+
|
146
|
+
**Integer**
|
147
|
+
|
148
|
+
Attribute will be typecasted using `to_i` method.
|
149
|
+
|
150
|
+
Optional `:base` parameter can be passed, during the typecast attribute will be assumed to be in specified base and will always be translated to decimal base.
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
class Klass
|
154
|
+
include Attrio
|
155
|
+
|
156
|
+
define_attributes do
|
157
|
+
attr :integer_attribute, Integer
|
158
|
+
attr :custom_integer_attribute, Integer, :base => 2
|
159
|
+
end
|
160
|
+
end
|
161
|
+
```
|
162
|
+
|
163
|
+
**Symbol**
|
164
|
+
|
165
|
+
Attribute will be typecasted using `to_sym` method.
|
166
|
+
|
167
|
+
If Optional `:underscore` parameter is passed, then attribute value will be downcased and underscored before calling `to_sym`.
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
class Klass
|
171
|
+
include Attrio
|
172
|
+
|
173
|
+
define_attributes do
|
174
|
+
attr :symbol_attribute, Symbol
|
175
|
+
attr :custom_symbol_attribute, Symbol, :underscore => true
|
176
|
+
end
|
177
|
+
end
|
178
|
+
```
|
179
|
+
|
180
|
+
## Inspect
|
181
|
+
Attrio adds its own `#inspect` method when included to the class. This overridden method prints object attributes in easy to read manner. To disable this feature pass `:inspect => false` to `define_arguments` block.
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
class Klass
|
185
|
+
include Attrio
|
186
|
+
|
187
|
+
define_attributes :inspect => false do
|
188
|
+
attr :attribute, Atring
|
189
|
+
end
|
190
|
+
end
|
191
|
+
```
|
192
|
+
|
25
193
|
## Note on Patches / Pull Requests
|
26
194
|
|
27
195
|
* Fork the project.
|
data/attrio.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |gem|
|
|
9
9
|
gem.version = Attrio::Version::STRING
|
10
10
|
gem.authors = ['Igor Alexandrov', 'Julia Egorova']
|
11
11
|
gem.email = 'hello@jetrockets.ru'
|
12
|
-
gem.summary = "Attributes for
|
12
|
+
gem.summary = "Attributes for plain old Ruby objects. No dependencies, only simplicity and clearness."
|
13
13
|
gem.homepage = "https://github.com/jetrockets/attrio"
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
data/lib/attrio.rb
CHANGED
@@ -5,8 +5,10 @@ require 'attrio/version'
|
|
5
5
|
require 'attrio/core_ext/array'
|
6
6
|
require 'attrio/core_ext/class'
|
7
7
|
require 'attrio/core_ext/hash'
|
8
|
+
require 'attrio/core_ext/nil_object'
|
8
9
|
require 'attrio/core_ext/object'
|
9
10
|
require 'attrio/core_ext/string'
|
11
|
+
require 'attrio/core_ext/time'
|
10
12
|
|
11
13
|
module Attrio
|
12
14
|
autoload :AttributesParser, 'attrio/attributes_parser'
|
@@ -33,8 +35,8 @@ module Attrio
|
|
33
35
|
EOS
|
34
36
|
|
35
37
|
self.define_attrio_new(options[:as])
|
38
|
+
self.define_attrio_reset(options[:as])
|
36
39
|
self.define_attrio_inspect(options[:as]) unless options[:inspect] == false
|
37
|
-
self.define_attrio_reset(options[:as]) unless options[:reset] == false
|
38
40
|
|
39
41
|
Attrio::AttributesParser.new(self, options, &block)
|
40
42
|
end
|
@@ -28,7 +28,7 @@ module Attrio
|
|
28
28
|
return constant if constant.is_a?(Class) && !!(constant < Attrio::Types::Base)
|
29
29
|
|
30
30
|
string = constant.to_s
|
31
|
-
string = string.camelize if (string =~ /\w_\w/ || string.chars.first.downcase == string
|
31
|
+
string = string.camelize if (string =~ /\w_\w/ || string.chars.first.downcase == string.chars.first)
|
32
32
|
|
33
33
|
begin
|
34
34
|
if Attrio::Types.const_defined?(string)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class Time # :nodoc:
|
4
|
+
class << self
|
5
|
+
def strptime(date, format, now=self.now)
|
6
|
+
d = Date._strptime(date, format)
|
7
|
+
raise ArgumentError, "invalid strptime format - `#{format}'" unless d
|
8
|
+
if seconds = d[:seconds]
|
9
|
+
Time.at(seconds)
|
10
|
+
else
|
11
|
+
year = d[:year]
|
12
|
+
year = yield(year) if year && block_given?
|
13
|
+
make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
|
14
|
+
end
|
15
|
+
end unless method_defined?(:strptime)
|
16
|
+
end
|
17
|
+
end
|
data/lib/attrio/types/integer.rb
CHANGED
@@ -4,8 +4,11 @@ module Attrio
|
|
4
4
|
module Types
|
5
5
|
class Integer < Base
|
6
6
|
def self.typecast(value, options = {})
|
7
|
+
options[:base] ||= 10
|
8
|
+
|
7
9
|
begin
|
8
|
-
value.to_i
|
10
|
+
return value.to_i(options[:base]) if value.is_a?(String)
|
11
|
+
return value.to_i
|
9
12
|
rescue NoMethodError => e
|
10
13
|
nil
|
11
14
|
end
|
data/lib/attrio/types/symbol.rb
CHANGED
data/lib/attrio/version.rb
CHANGED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Attrio do
|
4
|
+
describe 'model' do
|
5
|
+
let(:model) do
|
6
|
+
Class.new { include Attrio }
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should respond_to define_attributes' do
|
10
|
+
model.respond_to?(:define_attributes).should be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'without options' do
|
14
|
+
let(:model) do
|
15
|
+
Class.new do
|
16
|
+
include Attrio
|
17
|
+
|
18
|
+
define_attributes do
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should respond_to attributes' do
|
24
|
+
model.respond_to?(:attributes).should be_true
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should respond_to attributes=' do
|
28
|
+
model.respond_to?(:attributes=).should be_true
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'object' do
|
32
|
+
let(:object) { model.new }
|
33
|
+
|
34
|
+
it 'should respond_to reset_attributes' do
|
35
|
+
object.respond_to?(:reset_attributes).should be_true
|
36
|
+
object.respond_to?(:reset_attributes!).should be_true
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with option :as' do
|
42
|
+
let(:model) do
|
43
|
+
Class.new do
|
44
|
+
include Attrio
|
45
|
+
|
46
|
+
define_attributes :as => :model_attributes do
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should respond_to model_attributes' do
|
52
|
+
model.respond_to?(:model_attributes).should be_true
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should respond_to model_attributes=' do
|
56
|
+
model.respond_to?(:model_attributes=).should be_true
|
57
|
+
end
|
58
|
+
|
59
|
+
describe 'object' do
|
60
|
+
let(:object) { model.new }
|
61
|
+
|
62
|
+
it 'should respond_to reset_model_attributes' do
|
63
|
+
object.respond_to?(:reset_model_attributes).should be_true
|
64
|
+
object.respond_to?(:reset_model_attributes!).should be_true
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,60 @@
|
|
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
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Attrio::Inspect do
|
4
|
+
context '' do
|
5
|
+
let(:model) do
|
6
|
+
Class.new do
|
7
|
+
include Attrio
|
8
|
+
|
9
|
+
define_attributes do
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:object) { model.new }
|
15
|
+
|
16
|
+
it 'should' do
|
17
|
+
object.method(:inspect).source_location.first.should match(/lib\/attrio\/inspect.rb/)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context '' do
|
22
|
+
let(:model) do
|
23
|
+
Class.new do
|
24
|
+
include Attrio
|
25
|
+
|
26
|
+
define_attributes :inspect => false do
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:object) { model.new }
|
32
|
+
|
33
|
+
it 'should' do
|
34
|
+
if RUBY_ENGINE == 'rbx'
|
35
|
+
object.method(:inspect).source_location.first.should_not match(/attrio/)
|
36
|
+
else
|
37
|
+
object.method(:inspect).source_location.should be_nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Attrio::Reset do
|
4
|
+
let(:model) do
|
5
|
+
Class.new do
|
6
|
+
include Attrio
|
7
|
+
|
8
|
+
define_attributes do
|
9
|
+
attr :first, String
|
10
|
+
attr :second, String, :default => 'default'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:object) do
|
16
|
+
obj = model.new
|
17
|
+
obj.first = 'first'
|
18
|
+
obj.second = 'second'
|
19
|
+
obj
|
20
|
+
end
|
21
|
+
|
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
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should' do
|
28
|
+
object.reset_attributes
|
29
|
+
object.first.should be_nil
|
30
|
+
object.second.should == 'default'
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'spec_helper'
|
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
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
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
|
151
|
+
end
|
152
|
+
end
|
@@ -16,7 +16,7 @@ describe Attrio::Types::DateTime do
|
|
16
16
|
|
17
17
|
context 'with not typecasted assignment' do
|
18
18
|
it 'should cast <String>' do
|
19
|
-
now = Time.at(Time.now.to_i).to_datetime
|
19
|
+
now = Time.at(Time.now.to_i).send :to_datetime
|
20
20
|
|
21
21
|
object.datetime_attribute = now.to_s
|
22
22
|
object.datetime_attribute.should be_instance_of(DateTime)
|
@@ -50,7 +50,7 @@ describe Attrio::Types::DateTime do
|
|
50
50
|
|
51
51
|
context 'with not typecasted assignment' do
|
52
52
|
it 'should cast <String> of appropriate format' do
|
53
|
-
now = Time.at(Time.now.to_i).to_datetime
|
53
|
+
now = Time.at(Time.now.to_i).send :to_datetime
|
54
54
|
|
55
55
|
object.datetime_attribute = now.strftime('%m/%d/%y-%H:%M:%S-%z')
|
56
56
|
object.datetime_attribute.should be_instance_of(DateTime)
|
@@ -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 object which
|
18
|
+
it 'should cast an object which implements 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 object which
|
23
|
+
it 'should not cast an object which does not implement method to_i' do
|
24
24
|
lambda {
|
25
25
|
object.integer_attribute = []
|
26
26
|
}.should_not raise_exception
|
@@ -29,7 +29,47 @@ describe Attrio::Types::Integer do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
context 'with typecasted assignment' do
|
32
|
-
it 'should assign <
|
32
|
+
it 'should assign <Integer>' do
|
33
|
+
object.integer_attribute = 10
|
34
|
+
object.integer_attribute.should == 10
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context ':base option passed' do
|
40
|
+
let(:model) do
|
41
|
+
Class.new do
|
42
|
+
include Attrio
|
43
|
+
|
44
|
+
define_attributes do
|
45
|
+
attr :integer_attribute, Integer, :base => 16
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
let(:object){ model.new }
|
51
|
+
|
52
|
+
context 'with not typecasted assignment' do
|
53
|
+
it 'should cast' do
|
54
|
+
object.integer_attribute = "A"
|
55
|
+
object.integer_attribute.should == 10
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should cast an object which implements method to_i' do
|
59
|
+
object.integer_attribute = 10.0
|
60
|
+
object.integer_attribute.should == 10
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should not cast an object which does not implement method to_i' do
|
64
|
+
lambda {
|
65
|
+
object.integer_attribute = []
|
66
|
+
}.should_not raise_exception
|
67
|
+
object.integer_attribute.should be_nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'with typecasted assignment' do
|
72
|
+
it 'should assign <Integer>' do
|
33
73
|
object.integer_attribute = 10
|
34
74
|
object.integer_attribute.should == 10
|
35
75
|
end
|
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.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-05-
|
13
|
+
date: 2013-05-23 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
17
|
-
requirement:
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,15 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements:
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
26
31
|
- !ruby/object:Gem::Dependency
|
27
32
|
name: webmock
|
28
|
-
requirement:
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
29
34
|
none: false
|
30
35
|
requirements:
|
31
36
|
- - ~>
|
@@ -33,10 +38,15 @@ dependencies:
|
|
33
38
|
version: 1.9.0
|
34
39
|
type: :development
|
35
40
|
prerelease: false
|
36
|
-
version_requirements:
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 1.9.0
|
37
47
|
- !ruby/object:Gem::Dependency
|
38
48
|
name: simplecov
|
39
|
-
requirement:
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
40
50
|
none: false
|
41
51
|
requirements:
|
42
52
|
- - ! '>='
|
@@ -44,10 +54,15 @@ dependencies:
|
|
44
54
|
version: '0'
|
45
55
|
type: :development
|
46
56
|
prerelease: false
|
47
|
-
version_requirements:
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
48
63
|
- !ruby/object:Gem::Dependency
|
49
64
|
name: coveralls
|
50
|
-
requirement:
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
51
66
|
none: false
|
52
67
|
requirements:
|
53
68
|
- - ! '>='
|
@@ -55,10 +70,15 @@ dependencies:
|
|
55
70
|
version: '0'
|
56
71
|
type: :development
|
57
72
|
prerelease: false
|
58
|
-
version_requirements:
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
59
79
|
- !ruby/object:Gem::Dependency
|
60
80
|
name: rake
|
61
|
-
requirement:
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
62
82
|
none: false
|
63
83
|
requirements:
|
64
84
|
- - ! '>='
|
@@ -66,10 +86,15 @@ dependencies:
|
|
66
86
|
version: '0'
|
67
87
|
type: :development
|
68
88
|
prerelease: false
|
69
|
-
version_requirements:
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
70
95
|
- !ruby/object:Gem::Dependency
|
71
96
|
name: bundler
|
72
|
-
requirement:
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
73
98
|
none: false
|
74
99
|
requirements:
|
75
100
|
- - ! '>='
|
@@ -77,10 +102,15 @@ dependencies:
|
|
77
102
|
version: '0'
|
78
103
|
type: :development
|
79
104
|
prerelease: false
|
80
|
-
version_requirements:
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
81
111
|
- !ruby/object:Gem::Dependency
|
82
112
|
name: rake
|
83
|
-
requirement:
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
84
114
|
none: false
|
85
115
|
requirements:
|
86
116
|
- - ! '>='
|
@@ -88,7 +118,12 @@ dependencies:
|
|
88
118
|
version: '0'
|
89
119
|
type: :development
|
90
120
|
prerelease: false
|
91
|
-
version_requirements:
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ! '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
92
127
|
description:
|
93
128
|
email: hello@jetrockets.ru
|
94
129
|
executables: []
|
@@ -112,8 +147,10 @@ files:
|
|
112
147
|
- lib/attrio/core_ext/array.rb
|
113
148
|
- lib/attrio/core_ext/class.rb
|
114
149
|
- lib/attrio/core_ext/hash.rb
|
150
|
+
- lib/attrio/core_ext/nil_object.rb
|
115
151
|
- lib/attrio/core_ext/object.rb
|
116
152
|
- lib/attrio/core_ext/string.rb
|
153
|
+
- lib/attrio/core_ext/time.rb
|
117
154
|
- lib/attrio/initialize.rb
|
118
155
|
- lib/attrio/inspect.rb
|
119
156
|
- lib/attrio/reset.rb
|
@@ -126,7 +163,11 @@ files:
|
|
126
163
|
- lib/attrio/types/symbol.rb
|
127
164
|
- lib/attrio/types/time.rb
|
128
165
|
- lib/attrio/version.rb
|
129
|
-
- spec/attrio/
|
166
|
+
- spec/attrio/attrio_spec.rb
|
167
|
+
- spec/attrio/default_value_spec.rb
|
168
|
+
- spec/attrio/inspect_spec.rb
|
169
|
+
- spec/attrio/reset_spec.rb
|
170
|
+
- spec/attrio/type_spec.rb
|
130
171
|
- spec/attrio/types/boolean_spec.rb
|
131
172
|
- spec/attrio/types/date_spec.rb
|
132
173
|
- spec/attrio/types/date_time_spec.rb
|
@@ -149,7 +190,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
149
190
|
version: '0'
|
150
191
|
segments:
|
151
192
|
- 0
|
152
|
-
hash:
|
193
|
+
hash: 1888202450921559517
|
153
194
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
154
195
|
none: false
|
155
196
|
requirements:
|
@@ -158,15 +199,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
199
|
version: '0'
|
159
200
|
segments:
|
160
201
|
- 0
|
161
|
-
hash:
|
202
|
+
hash: 1888202450921559517
|
162
203
|
requirements: []
|
163
204
|
rubyforge_project:
|
164
|
-
rubygems_version: 1.8.
|
205
|
+
rubygems_version: 1.8.24
|
165
206
|
signing_key:
|
166
207
|
specification_version: 3
|
167
|
-
summary: Attributes for
|
208
|
+
summary: Attributes for plain old Ruby objects. No dependencies, only simplicity and
|
209
|
+
clearness.
|
168
210
|
test_files:
|
169
|
-
- spec/attrio/
|
211
|
+
- spec/attrio/attrio_spec.rb
|
212
|
+
- spec/attrio/default_value_spec.rb
|
213
|
+
- spec/attrio/inspect_spec.rb
|
214
|
+
- spec/attrio/reset_spec.rb
|
215
|
+
- spec/attrio/type_spec.rb
|
170
216
|
- spec/attrio/types/boolean_spec.rb
|
171
217
|
- spec/attrio/types/date_spec.rb
|
172
218
|
- spec/attrio/types/date_time_spec.rb
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Attrio::Initialize do
|
4
|
-
let(:model) do
|
5
|
-
Class.new do
|
6
|
-
include Attrio
|
7
|
-
|
8
|
-
define_attributes do
|
9
|
-
attr :without_default, Integer
|
10
|
-
attr :with_default, String, :default => "default"
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
let(:object){ model.new }
|
16
|
-
|
17
|
-
context "without 'default' option" do
|
18
|
-
it "should set variable to nil" do
|
19
|
-
object.without_default.should be_nil
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
context "with 'default' option" do
|
24
|
-
it "should set variable to default value" do
|
25
|
-
object.with_default.should == 'default'
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|