superstore 2.4.4 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +4 -6
  3. data/Gemfile +2 -3
  4. data/{Gemfile-rails4.2 → Gemfile.rails6} +2 -3
  5. data/README.md +4 -34
  6. data/lib/superstore.rb +17 -18
  7. data/lib/superstore/adapters/abstract_adapter.rb +1 -27
  8. data/lib/superstore/adapters/jsonb_adapter.rb +4 -132
  9. data/lib/superstore/associations.rb +6 -1
  10. data/lib/superstore/associations/association.rb +6 -0
  11. data/lib/superstore/associations/association_scope.rb +20 -0
  12. data/lib/superstore/associations/belongs_to.rb +3 -1
  13. data/lib/superstore/associations/has_many.rb +15 -2
  14. data/lib/superstore/associations/reflection.rb +8 -2
  15. data/lib/superstore/attribute_assignment.rb +7 -0
  16. data/lib/superstore/attribute_methods.rb +1 -109
  17. data/lib/superstore/attribute_methods/primary_key.rb +20 -11
  18. data/lib/superstore/attributes.rb +13 -0
  19. data/lib/superstore/base.rb +8 -33
  20. data/lib/superstore/core.rb +7 -65
  21. data/lib/superstore/model_schema.rb +35 -0
  22. data/lib/superstore/persistence.rb +31 -115
  23. data/lib/superstore/railtie.rb +3 -11
  24. data/lib/superstore/relation/scrolling.rb +48 -0
  25. data/lib/superstore/timestamp.rb +13 -0
  26. data/lib/superstore/types.rb +11 -9
  27. data/lib/superstore/types/array_type.rb +3 -7
  28. data/lib/superstore/types/boolean_type.rb +7 -12
  29. data/lib/superstore/types/date_range_type.rb +7 -0
  30. data/lib/superstore/types/date_type.rb +7 -10
  31. data/lib/superstore/types/float_type.rb +3 -11
  32. data/lib/superstore/types/geo_point_type.rb +30 -0
  33. data/lib/superstore/types/integer_range_type.rb +19 -0
  34. data/lib/superstore/types/integer_type.rb +8 -14
  35. data/lib/superstore/types/json_type.rb +1 -1
  36. data/lib/superstore/types/range_type.rb +51 -0
  37. data/lib/superstore/types/string_type.rb +4 -4
  38. data/lib/superstore/types/time_type.rb +10 -8
  39. data/superstore.gemspec +4 -3
  40. data/test/support/jsonb.rb +3 -1
  41. data/test/support/models.rb +8 -5
  42. data/test/test_helper.rb +6 -2
  43. data/test/unit/adapters/adapter_test.rb +1 -3
  44. data/test/unit/associations/belongs_to_test.rb +1 -1
  45. data/test/unit/associations/has_many_test.rb +10 -2
  46. data/test/unit/attribute_methods/dirty_test.rb +8 -19
  47. data/test/unit/attribute_methods/primary_key_test.rb +1 -1
  48. data/test/unit/attribute_methods_test.rb +10 -22
  49. data/test/unit/{attribute_methods/typecasting_test.rb → attributes_test.rb} +13 -39
  50. data/test/unit/base_test.rb +4 -0
  51. data/test/unit/caching_test.rb +1 -1
  52. data/test/unit/callbacks_test.rb +4 -4
  53. data/test/unit/core_test.rb +9 -19
  54. data/test/unit/persistence_test.rb +17 -54
  55. data/test/unit/{scope/batches_test.rb → relation/scrolling_test.rb} +9 -5
  56. data/test/unit/serialization_test.rb +10 -2
  57. data/test/unit/{timestamps_test.rb → timestamp_test.rb} +5 -5
  58. data/test/unit/types/array_type_test.rb +3 -18
  59. data/test/unit/types/boolean_type_test.rb +7 -21
  60. data/test/unit/types/date_range_type_test.rb +28 -0
  61. data/test/unit/types/date_type_test.rb +15 -6
  62. data/test/unit/types/float_type_test.rb +4 -19
  63. data/test/unit/types/geo_point_type_test.rb +24 -0
  64. data/test/unit/types/integer_range_type_test.rb +28 -0
  65. data/test/unit/types/integer_type_test.rb +7 -16
  66. data/test/unit/types/string_type_test.rb +9 -13
  67. data/test/unit/types/time_type_test.rb +17 -11
  68. data/test/unit/validations_test.rb +2 -2
  69. metadata +39 -39
  70. data/lib/superstore/attribute_methods/definition.rb +0 -17
  71. data/lib/superstore/attribute_methods/dirty.rb +0 -52
  72. data/lib/superstore/attribute_methods/typecasting.rb +0 -53
  73. data/lib/superstore/caching.rb +0 -13
  74. data/lib/superstore/callbacks.rb +0 -29
  75. data/lib/superstore/connection.rb +0 -24
  76. data/lib/superstore/errors.rb +0 -10
  77. data/lib/superstore/inspect.rb +0 -25
  78. data/lib/superstore/model.rb +0 -38
  79. data/lib/superstore/schema.rb +0 -20
  80. data/lib/superstore/scope.rb +0 -73
  81. data/lib/superstore/scope/batches.rb +0 -27
  82. data/lib/superstore/scope/finder_methods.rb +0 -51
  83. data/lib/superstore/scope/query_methods.rb +0 -52
  84. data/lib/superstore/scoping.rb +0 -30
  85. data/lib/superstore/timestamps.rb +0 -19
  86. data/lib/superstore/type.rb +0 -16
  87. data/lib/superstore/types/base_type.rb +0 -23
  88. data/lib/superstore/validations.rb +0 -44
  89. data/test/unit/attribute_methods/definition_test.rb +0 -16
  90. data/test/unit/inspect_test.rb +0 -26
  91. data/test/unit/schema_test.rb +0 -15
  92. data/test/unit/scope/finder_methods_test.rb +0 -62
  93. data/test/unit/scope/query_methods_test.rb +0 -37
  94. data/test/unit/scoping_test.rb +0 -7
  95. data/test/unit/types/base_type_test.rb +0 -11
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class Superstore::Types::GeoPointTypeTest < Superstore::Types::TestCase
4
+ test 'deserialize' do
5
+ lat, lon = 47.604, -122.329
6
+ seattle = {lat: lat, lon: lon}
7
+
8
+ assert_equal seattle, type.deserialize('lat' => lat, 'lon' => lon)
9
+ end
10
+
11
+ test 'cast_value' do
12
+ lat, lon = 47.604, -122.329
13
+ seattle = {lat: lat, lon: lon}
14
+
15
+ assert_equal seattle, type.cast_value(lat: lat, lon: lon)
16
+ assert_equal seattle, type.cast_value({ "lat" => lat, "lon" => lon })
17
+ assert_equal seattle, type.cast_value([lat, lon])
18
+
19
+ assert_equal({lat: 0.0, lon: 0.0}, type.cast_value(lat: "cats", lon: "dogs"))
20
+
21
+ assert_nil type.cast_value([])
22
+ assert_nil type.cast_value('invalid')
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ class Superstore::Types::IntegerRangeTypeTest < Superstore::Types::TestCase
4
+ test 'serialize' do
5
+ assert_equal [4, 5], type.serialize(4..5)
6
+ assert_equal [4, nil], type.serialize(4..Float::INFINITY)
7
+ assert_equal [nil, 5], type.serialize(-Float::INFINITY..5)
8
+ assert_equal [nil, nil], type.serialize(-Float::INFINITY..Float::INFINITY)
9
+ end
10
+
11
+ test 'deserialize' do
12
+ assert_equal 4..5, type.deserialize([4, 5])
13
+ assert_nil type.deserialize([5, 4])
14
+ assert_equal 4..Float::INFINITY, type.deserialize([4, nil])
15
+ assert_equal (-Float::INFINITY..5), type.deserialize([nil, 5])
16
+ assert_equal (-Float::INFINITY..Float::INFINITY), type.deserialize([nil, nil])
17
+ end
18
+
19
+ test 'cast_value' do
20
+ assert_equal 1..5, type.cast_value(1..5)
21
+ assert_nil type.cast_value(5..1)
22
+ assert_equal 1..5, type.cast_value([1, 5])
23
+ assert_nil type.cast_value([5, 1])
24
+ assert_equal 1..Float::INFINITY, type.cast_value([1, nil])
25
+ assert_equal (-Float::INFINITY..2), type.cast_value([nil, 2])
26
+ assert_equal (-Float::INFINITY..Float::INFINITY), type.cast_value([nil, nil])
27
+ end
28
+ end
@@ -1,21 +1,12 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Superstore::Types::IntegerTypeTest < Superstore::Types::TestCase
4
- if Superstore::Base.adapter.class.name == 'Superstore::Adapters::CassandraAdapter'
5
- test 'encode' do
6
- assert_equal '3', type.encode(3)
7
- assert_equal '-3', type.encode(-3)
8
-
9
- assert_raise ArgumentError do
10
- type.encode('xx')
11
- end
12
- end
13
- end
14
-
15
- test 'decode' do
16
- assert_nil type.decode('')
17
- assert_equal(0, type.decode('abc'))
18
- assert_equal(3, type.decode('3'))
19
- assert_equal(-3, type.decode('-3'))
4
+ test 'cast_value' do
5
+ assert_nil type.cast_value('')
6
+ assert_nil type.cast_value('abc')
7
+ assert_equal 3, type.cast_value(3)
8
+ assert_equal 3, type.cast_value('3')
9
+ assert_equal(-3, type.cast_value('-3'))
10
+ assert_equal 27, type.cast_value('027')
20
11
  end
21
12
  end
@@ -1,30 +1,26 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Superstore::Types::StringTypeTest < Superstore::Types::TestCase
4
- test 'encode' do
5
- assert_equal 'abc', type.encode('abc')
6
-
7
- assert_raise ArgumentError do
8
- type.encode(123)
9
- end
4
+ test 'serialize' do
5
+ assert_equal 'abc', type.serialize('abc')
10
6
  end
11
7
 
12
- test 'encode as utf' do
8
+ test 'serialize as utf' do
13
9
  assert_equal(
14
10
  '123'.force_encoding('UTF-8').encoding,
15
- type.encode('123'.force_encoding('ASCII-8BIT')).encoding
11
+ type.serialize('123'.force_encoding('ASCII-8BIT')).encoding
16
12
  )
17
13
  end
18
14
 
19
- test 'encode frozen as utf' do
15
+ test 'serialize frozen as utf' do
20
16
  assert_equal(
21
17
  '123'.force_encoding('UTF-8').encoding,
22
- type.encode('123'.force_encoding('ASCII-8BIT').freeze).encoding
18
+ type.serialize('123'.force_encoding('ASCII-8BIT').freeze).encoding
23
19
  )
24
20
  end
25
21
 
26
- test 'typecast' do
27
- assert_equal '123', type.typecast(123)
28
- assert_equal '123', type.typecast('123')
22
+ test 'cast_value' do
23
+ assert_equal '123', type.cast_value(123)
24
+ assert_equal '123', type.cast_value('123')
29
25
  end
30
26
  end
@@ -1,23 +1,29 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Superstore::Types::TimeTypeTest < Superstore::Types::TestCase
4
- test 'encode' do
5
- assert_equal '2004-12-24T01:02:03.000000Z', type.encode(Time.utc(2004, 12, 24, 1, 2, 3))
6
- assert_equal '2004-12-24T01:02:03.000000Z', type.encode(DateTime.new(2004, 12, 24, 1, 2, 3))
7
- assert_raise ArgumentError do
8
- type.encode 123
9
- end
4
+ test 'serialize' do
5
+ assert_equal '2004-12-24T01:02:03.000000Z', type.serialize(Time.utc(2004, 12, 24, 1, 2, 3))
6
+ assert_nil type.serialize(nil)
10
7
  end
11
8
 
12
- test 'decode' do
13
- assert_nil type.decode(nil)
14
- assert_nil type.decode('bad format')
15
- assert_equal Time.utc(2004, 12, 24, 1, 2, 3), type.decode('2004-12-24T01:02:03.000000Z')
9
+ test 'deserialize' do
10
+ assert_nil type.deserialize(nil)
11
+ assert_nil type.deserialize('bad format')
12
+ assert_equal Time.utc(2004, 12, 24, 1, 2, 3), type.deserialize('2004-12-24T01:02:03.000000Z')
13
+ assert_equal Time.utc(2004, 12, 24, 12, 13, 45), type.deserialize('2004-12-24 12:13:45 -0000')
16
14
 
17
15
  Time.use_zone 'Central Time (US & Canada)' do
18
- with_zone = type.decode('2013-07-18 13:12:46 -0700')
16
+ with_zone = type.deserialize('2013-07-18T13:12:46-07:00')
19
17
  assert_equal Time.utc(2013, 07, 18, 20, 12, 46), with_zone
20
18
  assert_equal 'CDT', with_zone.zone
21
19
  end
22
20
  end
21
+
22
+ test 'cast_value' do
23
+ assert_nil type.cast_value(1000)
24
+ assert_nil type.cast_value(1000.0)
25
+
26
+ my_date = Date.new
27
+ assert_equal my_date.to_time, type.cast_value(my_date)
28
+ end
23
29
  end
@@ -7,7 +7,7 @@ class Superstore::ValidationsTest < Superstore::TestCase
7
7
 
8
8
  Issue.create!(description: 'lol')
9
9
 
10
- assert_raise(Superstore::RecordInvalid) { Issue.create!(description: '') }
10
+ assert_raise(ActiveRecord::RecordInvalid) { Issue.create!(description: '') }
11
11
  ensure
12
12
  Issue.reset_callbacks(:validate)
13
13
  end
@@ -19,7 +19,7 @@ class Superstore::ValidationsTest < Superstore::TestCase
19
19
 
20
20
  Issue.new(description: 'lol').save!
21
21
 
22
- assert_raise(Superstore::RecordInvalid) { Issue.new(description: '').save! }
22
+ assert_raise(ActiveRecord::RecordInvalid) { Issue.new(description: '').save! }
23
23
  ensure
24
24
  Issue.reset_callbacks(:validate)
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: superstore
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.4
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Koziarski
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-12-06 00:00:00.000000000 Z
12
+ date: 2019-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -17,14 +17,28 @@ dependencies:
17
17
  requirements:
18
18
  - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: '3.0'
20
+ version: '5.2'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: '3.0'
27
+ version: '5.2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activerecord
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '5.2'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '5.2'
28
42
  - !ruby/object:Gem::Dependency
29
43
  name: globalid
30
44
  requirement: !ruby/object:Gem::Requirement
@@ -40,13 +54,13 @@ dependencies:
40
54
  - !ruby/object:Gem::Version
41
55
  version: '0'
42
56
  - !ruby/object:Gem::Dependency
43
- name: oj
57
+ name: bundler
44
58
  requirement: !ruby/object:Gem::Requirement
45
59
  requirements:
46
60
  - - ">="
47
61
  - !ruby/object:Gem::Version
48
62
  version: '0'
49
- type: :runtime
63
+ type: :development
50
64
  prerelease: false
51
65
  version_requirements: !ruby/object:Gem::Requirement
52
66
  requirements:
@@ -54,7 +68,7 @@ dependencies:
54
68
  - !ruby/object:Gem::Version
55
69
  version: '0'
56
70
  - !ruby/object:Gem::Dependency
57
- name: bundler
71
+ name: rails
58
72
  requirement: !ruby/object:Gem::Requirement
59
73
  requirements:
60
74
  - - ">="
@@ -78,7 +92,7 @@ files:
78
92
  - ".travis.yml"
79
93
  - CHANGELOG.md
80
94
  - Gemfile
81
- - Gemfile-rails4.2
95
+ - Gemfile.rails6
82
96
  - LICENSE
83
97
  - MIT-LICENSE
84
98
  - README.md
@@ -88,6 +102,7 @@ files:
88
102
  - lib/superstore/adapters/jsonb_adapter.rb
89
103
  - lib/superstore/associations.rb
90
104
  - lib/superstore/associations/association.rb
105
+ - lib/superstore/associations/association_scope.rb
91
106
  - lib/superstore/associations/belongs_to.rb
92
107
  - lib/superstore/associations/builder/association.rb
93
108
  - lib/superstore/associations/builder/belongs_to.rb
@@ -96,41 +111,31 @@ files:
96
111
  - lib/superstore/associations/has_many.rb
97
112
  - lib/superstore/associations/has_one.rb
98
113
  - lib/superstore/associations/reflection.rb
114
+ - lib/superstore/attribute_assignment.rb
99
115
  - lib/superstore/attribute_methods.rb
100
- - lib/superstore/attribute_methods/definition.rb
101
- - lib/superstore/attribute_methods/dirty.rb
102
116
  - lib/superstore/attribute_methods/primary_key.rb
103
- - lib/superstore/attribute_methods/typecasting.rb
117
+ - lib/superstore/attributes.rb
104
118
  - lib/superstore/base.rb
105
- - lib/superstore/caching.rb
106
- - lib/superstore/callbacks.rb
107
- - lib/superstore/connection.rb
108
119
  - lib/superstore/core.rb
109
- - lib/superstore/errors.rb
110
120
  - lib/superstore/identity.rb
111
- - lib/superstore/inspect.rb
112
- - lib/superstore/model.rb
121
+ - lib/superstore/model_schema.rb
113
122
  - lib/superstore/persistence.rb
114
123
  - lib/superstore/railtie.rb
115
- - lib/superstore/schema.rb
116
- - lib/superstore/scope.rb
117
- - lib/superstore/scope/batches.rb
118
- - lib/superstore/scope/finder_methods.rb
119
- - lib/superstore/scope/query_methods.rb
120
- - lib/superstore/scoping.rb
121
- - lib/superstore/timestamps.rb
122
- - lib/superstore/type.rb
124
+ - lib/superstore/relation/scrolling.rb
125
+ - lib/superstore/timestamp.rb
123
126
  - lib/superstore/types.rb
124
127
  - lib/superstore/types/array_type.rb
125
- - lib/superstore/types/base_type.rb
126
128
  - lib/superstore/types/boolean_type.rb
129
+ - lib/superstore/types/date_range_type.rb
127
130
  - lib/superstore/types/date_type.rb
128
131
  - lib/superstore/types/float_type.rb
132
+ - lib/superstore/types/geo_point_type.rb
133
+ - lib/superstore/types/integer_range_type.rb
129
134
  - lib/superstore/types/integer_type.rb
130
135
  - lib/superstore/types/json_type.rb
136
+ - lib/superstore/types/range_type.rb
131
137
  - lib/superstore/types/string_type.rb
132
138
  - lib/superstore/types/time_type.rb
133
- - lib/superstore/validations.rb
134
139
  - superstore.gemspec
135
140
  - test/support/jsonb.rb
136
141
  - test/support/models.rb
@@ -142,31 +147,27 @@ files:
142
147
  - test/unit/associations/has_many_test.rb
143
148
  - test/unit/associations/has_one_test.rb
144
149
  - test/unit/associations/reflection_test.rb
145
- - test/unit/attribute_methods/definition_test.rb
146
150
  - test/unit/attribute_methods/dirty_test.rb
147
151
  - test/unit/attribute_methods/primary_key_test.rb
148
- - test/unit/attribute_methods/typecasting_test.rb
149
152
  - test/unit/attribute_methods_test.rb
153
+ - test/unit/attributes_test.rb
150
154
  - test/unit/base_test.rb
151
155
  - test/unit/caching_test.rb
152
156
  - test/unit/callbacks_test.rb
153
157
  - test/unit/connection_test.rb
154
158
  - test/unit/core_test.rb
155
159
  - test/unit/identity_test.rb
156
- - test/unit/inspect_test.rb
157
160
  - test/unit/persistence_test.rb
158
- - test/unit/schema_test.rb
159
- - test/unit/scope/batches_test.rb
160
- - test/unit/scope/finder_methods_test.rb
161
- - test/unit/scope/query_methods_test.rb
162
- - test/unit/scoping_test.rb
161
+ - test/unit/relation/scrolling_test.rb
163
162
  - test/unit/serialization_test.rb
164
- - test/unit/timestamps_test.rb
163
+ - test/unit/timestamp_test.rb
165
164
  - test/unit/types/array_type_test.rb
166
- - test/unit/types/base_type_test.rb
167
165
  - test/unit/types/boolean_type_test.rb
166
+ - test/unit/types/date_range_type_test.rb
168
167
  - test/unit/types/date_type_test.rb
169
168
  - test/unit/types/float_type_test.rb
169
+ - test/unit/types/geo_point_type_test.rb
170
+ - test/unit/types/integer_range_type_test.rb
170
171
  - test/unit/types/integer_type_test.rb
171
172
  - test/unit/types/json_type_test.rb
172
173
  - test/unit/types/string_type_test.rb
@@ -192,8 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
193
  - !ruby/object:Gem::Version
193
194
  version: 1.3.5
194
195
  requirements: []
195
- rubyforge_project:
196
- rubygems_version: 2.5.1
196
+ rubygems_version: 3.0.3
197
197
  signing_key:
198
198
  specification_version: 4
199
199
  summary: ActiveModel for JSONB documents
@@ -1,17 +0,0 @@
1
- module Superstore
2
- module AttributeMethods
3
- class Definition
4
- attr_reader :name, :type
5
- def initialize(model, name, type_class, options)
6
- @name = name.to_s
7
- @type = type_class.new(model, options)
8
- end
9
-
10
- def instantiate(value)
11
- return if value.nil?
12
-
13
- value.kind_of?(String) ? type.decode(value) : type.typecast(value)
14
- end
15
- end
16
- end
17
- end
@@ -1,52 +0,0 @@
1
- module Superstore
2
- module AttributeMethods
3
- module Dirty
4
- extend ActiveSupport::Concern
5
- include ActiveModel::Dirty
6
-
7
- # Attempts to +save+ the record and clears changed attributes if successful.
8
- def save(*) #:nodoc:
9
- if status = super
10
- @previously_changed = changes
11
- @changed_attributes = {}
12
- end
13
- status
14
- end
15
-
16
- # <tt>reload</tt> the record and clears changed attributes.
17
- def reload
18
- super.tap do
19
- @previously_changed.try :clear
20
- @changed_attributes.try :clear
21
- end
22
- end
23
-
24
- def unapplied_changes
25
- result = {}
26
- changed_attributes.each_key { |attr| result[attr] = read_attribute(attr) }
27
- result
28
- end
29
-
30
- def old_attribute_value(attr)
31
- if attribute_changed?(attr)
32
- changed_attributes[attr]
33
- else
34
- read_attribute attr
35
- end
36
- end
37
-
38
- def write_attribute(name, value)
39
- name = name.to_s
40
- old = old_attribute_value(name)
41
-
42
- super
43
-
44
- if old == read_attribute(name)
45
- changed_attributes.delete(name)
46
- else
47
- changed_attributes[name] = old
48
- end
49
- end
50
- end
51
- end
52
- end
@@ -1,53 +0,0 @@
1
- module Superstore
2
- module AttributeMethods
3
- module Typecasting
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- class_attribute :attribute_definitions
8
- self.attribute_definitions = {}
9
-
10
- %w(array boolean date float integer json string time).each do |type|
11
- instance_eval <<-EOV, __FILE__, __LINE__ + 1
12
- def #{type}(*args)
13
- options = args.extract_options!
14
- args.each do |name|
15
- attribute(name, options.merge(:type => :#{type}))
16
- end
17
- end
18
- EOV
19
- end
20
- end
21
-
22
- module ClassMethods
23
- def inherited(child)
24
- super
25
- child.attribute_definitions = attribute_definitions.dup
26
- end
27
-
28
- #
29
- # attribute :name, type: :string
30
- # attribute :ammo, type: :integer
31
- #
32
- def attribute(name, options)
33
- type_name = options[:type]
34
- type_class = Superstore::Type.get_type_class(type_name) || (raise "Unknown type #{type_name}")
35
-
36
- attribute_definitions[name.to_s] = AttributeMethods::Definition.new(self, name, type_class, options)
37
- end
38
-
39
- def typecast_attribute(name, value)
40
- if attribute_definition = attribute_definitions[name.to_s]
41
- attribute_definition.instantiate(value)
42
- else
43
- raise NoMethodError, "Unknown attribute #{name.inspect}"
44
- end
45
- end
46
-
47
- def type_for(attribute)
48
- attribute_definitions[attribute.to_s].type
49
- end
50
- end
51
- end
52
- end
53
- end