attrio 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +3 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +49 -0
  7. data/Rakefile +8 -0
  8. data/attrio.gemspec +27 -0
  9. data/lib/attrio.rb +62 -0
  10. data/lib/attrio/attribute.rb +68 -0
  11. data/lib/attrio/attributes_parser.rb +56 -0
  12. data/lib/attrio/builders/accessor_builder.rb +24 -0
  13. data/lib/attrio/builders/reader_builder.rb +23 -0
  14. data/lib/attrio/builders/writer_builder.rb +24 -0
  15. data/lib/attrio/core_ext/array.rb +23 -0
  16. data/lib/attrio/core_ext/class.rb +55 -0
  17. data/lib/attrio/core_ext/hash.rb +31 -0
  18. data/lib/attrio/core_ext/object.rb +11 -0
  19. data/lib/attrio/core_ext/string.rb +15 -0
  20. data/lib/attrio/initialize.rb +29 -0
  21. data/lib/attrio/inspect.rb +33 -0
  22. data/lib/attrio/reset.rb +21 -0
  23. data/lib/attrio/types/base.rb +29 -0
  24. data/lib/attrio/types/boolean.rb +26 -0
  25. data/lib/attrio/types/date.rb +19 -0
  26. data/lib/attrio/types/date_time.rb +19 -0
  27. data/lib/attrio/types/float.rb +19 -0
  28. data/lib/attrio/types/integer.rb +19 -0
  29. data/lib/attrio/types/symbol.rb +19 -0
  30. data/lib/attrio/types/time.rb +19 -0
  31. data/lib/attrio/version.rb +12 -0
  32. data/spec/attrio/initialize_spec.rb +29 -0
  33. data/spec/attrio/types/boolean_spec.rb +101 -0
  34. data/spec/attrio/types/date_spec.rb +80 -0
  35. data/spec/attrio/types/date_time_spec.rb +80 -0
  36. data/spec/attrio/types/float_spec.rb +38 -0
  37. data/spec/attrio/types/integer_spec.rb +38 -0
  38. data/spec/attrio/types/symbol_spec.rb +41 -0
  39. data/spec/attrio/types/time_spec.rb +80 -0
  40. data/spec/spec_helper.rb +31 -0
  41. metadata +177 -0
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe Attrio::Types::Date do
4
+ context 'standard casting conventions' do
5
+ let(:model) do
6
+ Class.new do
7
+ include Attrio
8
+
9
+ define_attributes do
10
+ attr :date_attribute, Date
11
+ end
12
+ end
13
+ end
14
+
15
+ let(:object){ model.new }
16
+
17
+ context 'with not typecasted assignment' do
18
+ it 'should cast <String>' do
19
+ today = Date.today
20
+
21
+ object.date_attribute = today.to_s
22
+ object.date_attribute.should be_instance_of(Date)
23
+ object.date_attribute.should == today
24
+ end
25
+ end
26
+
27
+ context 'with typecasted assignment' do
28
+ it 'should assign <Date>' do
29
+ today = Date.today
30
+
31
+ object.date_attribute = today
32
+ object.date_attribute.should be_instance_of(Date)
33
+ object.date_attribute.should be_equal(today)
34
+ end
35
+ end
36
+ end
37
+
38
+ context ':format option passed' do
39
+ let(:model) do
40
+ Class.new do
41
+ include Attrio
42
+
43
+ define_attributes do
44
+ attr :date_attribute, Date, :format => '%m/%d/%y'
45
+ end
46
+ end
47
+ end
48
+
49
+ let(:object){ model.new }
50
+
51
+ context 'with not typecasted assignment' do
52
+ it 'should cast <String> of appropriate format' do
53
+ today = Date.today
54
+
55
+ object.date_attribute = today.strftime('%m/%d/%y')
56
+ object.date_attribute.should be_instance_of(Date)
57
+ object.date_attribute.should == today
58
+ end
59
+
60
+ it 'should not cast <String> with invalid format' do
61
+ today = Date.today
62
+
63
+ lambda {
64
+ object.date_attribute = today.strftime('%d-%m-%Y')
65
+ }.should_not raise_exception
66
+ object.date_attribute.should be_nil
67
+ end
68
+ end
69
+
70
+ context 'with typecasted assignment' do
71
+ it 'should assign <Date>' do
72
+ today = Date.today
73
+
74
+ object.date_attribute = today
75
+ object.date_attribute.should be_instance_of(Date)
76
+ object.date_attribute.should be_equal(today)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe Attrio::Types::DateTime do
4
+ context 'standard casting conventions' do
5
+ let(:model) do
6
+ Class.new do
7
+ include Attrio
8
+
9
+ define_attributes do
10
+ attr :datetime_attribute, DateTime
11
+ end
12
+ end
13
+ end
14
+
15
+ let(:object){ model.new }
16
+
17
+ context 'with not typecasted assignment' do
18
+ it 'should cast <String>' do
19
+ now = Time.at(Time.now.to_i).to_datetime
20
+
21
+ object.datetime_attribute = now.to_s
22
+ object.datetime_attribute.should be_instance_of(DateTime)
23
+ object.datetime_attribute.should == now
24
+ end
25
+ end
26
+
27
+ context 'with typecasted assignment' do
28
+ it 'should assign <DateTime>' do
29
+ now = DateTime.now
30
+
31
+ object.datetime_attribute = now
32
+ object.datetime_attribute.should be_instance_of(DateTime)
33
+ object.datetime_attribute.should be_equal(now)
34
+ end
35
+ end
36
+ end
37
+
38
+ context ':format option passed' do
39
+ let(:model) do
40
+ Class.new do
41
+ include Attrio
42
+
43
+ define_attributes do
44
+ attr :datetime_attribute, DateTime, :format => '%m/%d/%y-%H:%M:%S-%z'
45
+ end
46
+ end
47
+ end
48
+
49
+ let(:object){ model.new }
50
+
51
+ context 'with not typecasted assignment' do
52
+ it 'should cast <String> of appropriate format' do
53
+ now = Time.at(Time.now.to_i).to_datetime
54
+
55
+ object.datetime_attribute = now.strftime('%m/%d/%y-%H:%M:%S-%z')
56
+ object.datetime_attribute.should be_instance_of(DateTime)
57
+ object.datetime_attribute.should == now
58
+ end
59
+
60
+ it 'should not cast <String> with invalid format' do
61
+ now = DateTime.now
62
+
63
+ lambda {
64
+ object.datetime_attribute = now.strftime('%H:%M:%S-%m/%d/%y')
65
+ }.should_not raise_exception
66
+ object.datetime_attribute.should be_nil
67
+ end
68
+ end
69
+
70
+ context 'with typecasted assignment' do
71
+ it 'should assign <DateTime>' do
72
+ now = DateTime.now
73
+
74
+ object.datetime_attribute = now
75
+ object.datetime_attribute.should be_instance_of(DateTime)
76
+ object.datetime_attribute.should be_equal(now)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Attrio::Types::Float do
4
+ context 'standard casting conventions' do
5
+ let(:model) do
6
+ Class.new do
7
+ include Attrio
8
+
9
+ define_attributes do
10
+ attr :float_attribute, Float
11
+ end
12
+ end
13
+ end
14
+
15
+ let(:object){ model.new }
16
+
17
+ context 'with not typecasted assignment' do
18
+ it 'should cast object which has method to_f' do
19
+ object.float_attribute = "10 test"
20
+ object.float_attribute.should == 10.0
21
+ end
22
+
23
+ it 'should not cast object which has not method to_f' do
24
+ lambda {
25
+ object.float_attribute = []
26
+ }.should_not raise_exception
27
+ object.float_attribute.should be_nil
28
+ end
29
+ end
30
+
31
+ context 'with typecasted assignment' do
32
+ it 'should assign <Float>' do
33
+ object.float_attribute = 10.0
34
+ object.float_attribute.should == 10.0
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Attrio::Types::Integer do
4
+ context 'standard casting conventions' do
5
+ let(:model) do
6
+ Class.new do
7
+ include Attrio
8
+
9
+ define_attributes do
10
+ attr :integer_attribute, Integer
11
+ end
12
+ end
13
+ end
14
+
15
+ let(:object){ model.new }
16
+
17
+ context 'with not typecasted assignment' do
18
+ it 'should cast object which has method to_i' do
19
+ object.integer_attribute = "10 test"
20
+ object.integer_attribute.should == 10
21
+ end
22
+
23
+ it 'should not cast object which has not method to_i' do
24
+ lambda {
25
+ object.integer_attribute = []
26
+ }.should_not raise_exception
27
+ object.integer_attribute.should be_nil
28
+ end
29
+ end
30
+
31
+ context 'with typecasted assignment' do
32
+ it 'should assign <Float>' do
33
+ object.integer_attribute = 10
34
+ object.integer_attribute.should == 10
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Attrio::Types::Symbol do
4
+ context 'standard casting conventions' do
5
+ let(:model) do
6
+ Class.new do
7
+ include Attrio
8
+
9
+ define_attributes do
10
+ attr :symbol_attribute, Symbol
11
+ end
12
+ end
13
+ end
14
+
15
+ let(:object){ model.new }
16
+
17
+ context 'with not typecasted assignment' do
18
+ it 'should cast <String>' do
19
+ object.symbol_attribute = "symbol"
20
+ object.symbol_attribute.should == :symbol
21
+ end
22
+
23
+ it 'should not cast 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
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe Attrio::Types::Time do
4
+ context 'standard casting conventions' do
5
+ let(:model) do
6
+ Class.new do
7
+ include Attrio
8
+
9
+ define_attributes do
10
+ attr :time_attribute, Time
11
+ end
12
+ end
13
+ end
14
+
15
+ let(:object){ model.new }
16
+
17
+ context 'with not typecasted assignment' do
18
+ it 'should cast <String>' do
19
+ now = Time.at(Time.now.to_i)
20
+
21
+ object.time_attribute = now.to_s
22
+ object.time_attribute.should be_instance_of(Time)
23
+ object.time_attribute.should == now
24
+ end
25
+ end
26
+
27
+ context 'with typecasted assignment' do
28
+ it 'should assign <Time>' do
29
+ now = Time.now
30
+
31
+ object.time_attribute = now
32
+ object.time_attribute.should be_instance_of(Time)
33
+ object.time_attribute.should be_equal(now)
34
+ end
35
+ end
36
+ end
37
+
38
+ context ':format option passed' do
39
+ let(:model) do
40
+ Class.new do
41
+ include Attrio
42
+
43
+ define_attributes do
44
+ attr :time_attribute, Time, :format => '%m/%d/%y-%H:%M:%S'
45
+ end
46
+ end
47
+ end
48
+
49
+ let(:object){ model.new }
50
+
51
+ context 'with not typecasted assignment' do
52
+ it 'should cast <String> of appropriate format' do
53
+ now = Time.at(Time.now.to_i)
54
+
55
+ object.time_attribute = now.strftime('%m/%d/%y-%H:%M:%S')
56
+ object.time_attribute.should be_instance_of(Time)
57
+ object.time_attribute.should == now
58
+ end
59
+
60
+ it 'should not cast <String> with invalid format' do
61
+ now = Time.now
62
+
63
+ lambda {
64
+ object.time_attribute = now.strftime('%H:%M:%S-%m/%d/%y')
65
+ }.should_not raise_exception
66
+ object.time_attribute.should be_nil
67
+ end
68
+ end
69
+
70
+ context 'with typecasted assignment' do
71
+ it 'should assign <Time>' do
72
+ now = Time.now
73
+
74
+ object.time_attribute = now
75
+ object.time_attribute.should be_instance_of(Time)
76
+ object.time_attribute.should be_equal(now)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+
3
+ $:.unshift File.expand_path('..', __FILE__)
4
+ $:.unshift File.expand_path('../../lib', __FILE__)
5
+
6
+ require 'attrio'
7
+ require 'rspec'
8
+ require 'webmock/rspec'
9
+
10
+ require 'coveralls'
11
+ Coveralls.wear!
12
+
13
+ Class.class_eval do
14
+ def const_missing(name)
15
+ Attrio::AttributesParser.cast_type(name) || super
16
+ end
17
+ end
18
+
19
+ RSpec.configure do |config|
20
+ config.include WebMock::API
21
+ config.order = :rand
22
+ config.color_enabled = true
23
+ config.treat_symbols_as_metadata_keys_with_true_values = true
24
+
25
+ config.before(:each) do
26
+ WebMock.reset!
27
+ end
28
+ config.after(:each) do
29
+ WebMock.reset!
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attrio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Igor Alexandrov
9
+ - Julia Egorova
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-05-22 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &70202349679820 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *70202349679820
26
+ - !ruby/object:Gem::Dependency
27
+ name: webmock
28
+ requirement: &70202349679320 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.9.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70202349679320
37
+ - !ruby/object:Gem::Dependency
38
+ name: simplecov
39
+ requirement: &70202349678900 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70202349678900
48
+ - !ruby/object:Gem::Dependency
49
+ name: coveralls
50
+ requirement: &70202349685760 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70202349685760
59
+ - !ruby/object:Gem::Dependency
60
+ name: rake
61
+ requirement: &70202349684640 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *70202349684640
70
+ - !ruby/object:Gem::Dependency
71
+ name: bundler
72
+ requirement: &70202349684080 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *70202349684080
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: &70202349683560 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *70202349683560
92
+ description:
93
+ email: hello@jetrockets.ru
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - .gitignore
99
+ - .rspec
100
+ - .travis.yml
101
+ - Gemfile
102
+ - LICENSE.txt
103
+ - README.md
104
+ - Rakefile
105
+ - attrio.gemspec
106
+ - lib/attrio.rb
107
+ - lib/attrio/attribute.rb
108
+ - lib/attrio/attributes_parser.rb
109
+ - lib/attrio/builders/accessor_builder.rb
110
+ - lib/attrio/builders/reader_builder.rb
111
+ - lib/attrio/builders/writer_builder.rb
112
+ - lib/attrio/core_ext/array.rb
113
+ - lib/attrio/core_ext/class.rb
114
+ - lib/attrio/core_ext/hash.rb
115
+ - lib/attrio/core_ext/object.rb
116
+ - lib/attrio/core_ext/string.rb
117
+ - lib/attrio/initialize.rb
118
+ - lib/attrio/inspect.rb
119
+ - lib/attrio/reset.rb
120
+ - lib/attrio/types/base.rb
121
+ - lib/attrio/types/boolean.rb
122
+ - lib/attrio/types/date.rb
123
+ - lib/attrio/types/date_time.rb
124
+ - lib/attrio/types/float.rb
125
+ - lib/attrio/types/integer.rb
126
+ - lib/attrio/types/symbol.rb
127
+ - lib/attrio/types/time.rb
128
+ - lib/attrio/version.rb
129
+ - spec/attrio/initialize_spec.rb
130
+ - spec/attrio/types/boolean_spec.rb
131
+ - spec/attrio/types/date_spec.rb
132
+ - spec/attrio/types/date_time_spec.rb
133
+ - spec/attrio/types/float_spec.rb
134
+ - spec/attrio/types/integer_spec.rb
135
+ - spec/attrio/types/symbol_spec.rb
136
+ - spec/attrio/types/time_spec.rb
137
+ - spec/spec_helper.rb
138
+ homepage: https://github.com/jetrockets/attrio
139
+ licenses: []
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ segments:
151
+ - 0
152
+ hash: 1284830475475913603
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ! '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ segments:
160
+ - 0
161
+ hash: 1284830475475913603
162
+ requirements: []
163
+ rubyforge_project:
164
+ rubygems_version: 1.8.15
165
+ signing_key:
166
+ specification_version: 3
167
+ summary: Attributes for Plain Old Ruby Objects
168
+ test_files:
169
+ - spec/attrio/initialize_spec.rb
170
+ - spec/attrio/types/boolean_spec.rb
171
+ - spec/attrio/types/date_spec.rb
172
+ - spec/attrio/types/date_time_spec.rb
173
+ - spec/attrio/types/float_spec.rb
174
+ - spec/attrio/types/integer_spec.rb
175
+ - spec/attrio/types/symbol_spec.rb
176
+ - spec/attrio/types/time_spec.rb
177
+ - spec/spec_helper.rb