fast_attributes 0.5.0.rc1 → 0.5.0.rc2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2 -0
- data/lib/fast_attributes/builder.rb +17 -1
- data/lib/fast_attributes/version.rb +1 -1
- data/lib/fast_attributes.rb +9 -7
- data/spec/fast_attributes_spec.rb +28 -7
- data/spec/fixtures/classes.rb +14 -1
- data/spec/spec_helper.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a28b7232008137b1c12eff7d9f3428abc3f19787
|
4
|
+
data.tar.gz: 12599ab645b73a082b47560be594a9be1cd78694
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0dea100aca631e2d8befc607fe4618c4ffa3794037ceb1078745eddc099a7882aa19ffdbdd4ad422d1fffc145f9f850c72ce8f83dfd6c262dfa268ef99d46e1a
|
7
|
+
data.tar.gz: 5cec57af50df5ac04f65d12f5d90f89c108672c1a66d4c1f743a37a011cd11e28328693616c45c93d264b054042b9df8c33db66d9de3631f02c1e4cc1b7a0b7b
|
data/CHANGELOG.md
CHANGED
@@ -48,9 +48,11 @@ module FastAttributes
|
|
48
48
|
def compile_setter
|
49
49
|
each_attribute do |attribute, type|
|
50
50
|
type_cast = FastAttributes.get_type_casting(type)
|
51
|
+
template = escape_template(type_cast.template, 'value')
|
52
|
+
|
51
53
|
@methods.module_eval <<-EOS, __FILE__, __LINE__ + 1
|
52
54
|
def #{attribute}=(value)
|
53
|
-
@#{attribute} = #{
|
55
|
+
@#{attribute} = #{template}
|
54
56
|
end
|
55
57
|
EOS
|
56
58
|
end
|
@@ -95,5 +97,19 @@ module FastAttributes
|
|
95
97
|
end
|
96
98
|
end
|
97
99
|
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def escape_template(template, argument_name)
|
104
|
+
template.gsub(/%+s/) do |match|
|
105
|
+
match.each_char.each_slice(2).map do |placeholder|
|
106
|
+
case placeholder
|
107
|
+
when %w[% s] then argument_name
|
108
|
+
when %w[% %] then '%'
|
109
|
+
else placeholder.join
|
110
|
+
end
|
111
|
+
end.join
|
112
|
+
end
|
113
|
+
end
|
98
114
|
end
|
99
115
|
end
|
data/lib/fast_attributes.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'bigdecimal'
|
1
2
|
require 'date'
|
2
3
|
require 'time'
|
3
4
|
require 'fast_attributes/version'
|
@@ -49,11 +50,12 @@ module FastAttributes
|
|
49
50
|
builder.compile!
|
50
51
|
end
|
51
52
|
|
52
|
-
set_type_casting String,
|
53
|
-
set_type_casting Integer,
|
54
|
-
set_type_casting Float,
|
55
|
-
set_type_casting Array,
|
56
|
-
set_type_casting Date,
|
57
|
-
set_type_casting Time,
|
58
|
-
set_type_casting DateTime,
|
53
|
+
set_type_casting String, 'String(%s)'
|
54
|
+
set_type_casting Integer, 'Integer(%s)'
|
55
|
+
set_type_casting Float, 'Float(%s)'
|
56
|
+
set_type_casting Array, 'Array(%s)'
|
57
|
+
set_type_casting Date, 'Date.parse(%s)'
|
58
|
+
set_type_casting Time, 'Time.parse(%s)'
|
59
|
+
set_type_casting DateTime, 'DateTime.parse(%s)'
|
60
|
+
set_type_casting BigDecimal, 'Float(%s);BigDecimal(%s)'
|
59
61
|
end
|
@@ -3,9 +3,14 @@ require 'spec_helper'
|
|
3
3
|
describe FastAttributes do
|
4
4
|
describe '.type_casting' do
|
5
5
|
it 'returns predefined type casting rules' do
|
6
|
-
expect(FastAttributes.type_casting.keys).to
|
7
|
-
|
8
|
-
|
6
|
+
expect(FastAttributes.type_casting.keys).to include(String)
|
7
|
+
expect(FastAttributes.type_casting.keys).to include(Integer)
|
8
|
+
expect(FastAttributes.type_casting.keys).to include(Float)
|
9
|
+
expect(FastAttributes.type_casting.keys).to include(Array)
|
10
|
+
expect(FastAttributes.type_casting.keys).to include(Date)
|
11
|
+
expect(FastAttributes.type_casting.keys).to include(Time)
|
12
|
+
expect(FastAttributes.type_casting.keys).to include(DateTime)
|
13
|
+
expect(FastAttributes.type_casting.keys).to include(BigDecimal)
|
9
14
|
end
|
10
15
|
end
|
11
16
|
|
@@ -63,6 +68,7 @@ describe FastAttributes do
|
|
63
68
|
expect(book.respond_to?(:published)).to be(true)
|
64
69
|
expect(book.respond_to?(:sold)).to be(true)
|
65
70
|
expect(book.respond_to?(:finished)).to be(true)
|
71
|
+
expect(book.respond_to?(:rate)).to be(true)
|
66
72
|
end
|
67
73
|
|
68
74
|
it 'is possible to override getter method' do
|
@@ -82,6 +88,7 @@ describe FastAttributes do
|
|
82
88
|
expect(book.respond_to?(:published=)).to be(true)
|
83
89
|
expect(book.respond_to?(:sold=)).to be(true)
|
84
90
|
expect(book.respond_to?(:finished=)).to be(true)
|
91
|
+
expect(book.respond_to?(:rate=)).to be(true)
|
85
92
|
end
|
86
93
|
|
87
94
|
it 'is possible to override setter method' do
|
@@ -101,15 +108,17 @@ describe FastAttributes do
|
|
101
108
|
book.published = '2014-06-21'
|
102
109
|
book.sold = '2014-06-21 20:45:15'
|
103
110
|
book.finished = '2014-05-20 21:35:20'
|
111
|
+
book.rate = '4.1'
|
104
112
|
|
105
113
|
expect(book.title).to eq('123')
|
106
114
|
expect(book.name).to eq('456')
|
107
115
|
expect(book.pages).to be(250)
|
108
|
-
expect(book.price).to eq(2.55)
|
116
|
+
expect(book.price).to eq(BigDecimal.new("2.55"))
|
109
117
|
expect(book.authors).to eq(%w[Jobs])
|
110
118
|
expect(book.published).to eq(Date.new(2014, 6, 21))
|
111
119
|
expect(book.sold).to eq(Time.new(2014, 6, 21, 20, 45, 15))
|
112
120
|
expect(book.finished).to eq(DateTime.new(2014, 5, 20, 21, 35, 20))
|
121
|
+
expect(book.rate).to eq(4.1)
|
113
122
|
end
|
114
123
|
|
115
124
|
it 'setter methods accept values which are already in a proper type' do
|
@@ -117,11 +126,12 @@ describe FastAttributes do
|
|
117
126
|
book.title = title = 'One'
|
118
127
|
book.name = name = 'Two'
|
119
128
|
book.pages = pages = 250
|
120
|
-
book.price = price = 2.55
|
129
|
+
book.price = price = BigDecimal.new("2.55")
|
121
130
|
book.authors = authors = %w[Jobs]
|
122
131
|
book.published = published = Date.new(2014, 06, 21)
|
123
132
|
book.sold = sold = Time.new(2014, 6, 21, 20, 45, 15)
|
124
133
|
book.finished = finished = DateTime.new(2014, 05, 20, 21, 35, 20)
|
134
|
+
book.rate = rate = 4.1
|
125
135
|
|
126
136
|
expect(book.title).to be(title)
|
127
137
|
expect(book.name).to be(name)
|
@@ -131,6 +141,7 @@ describe FastAttributes do
|
|
131
141
|
expect(book.published).to be(published)
|
132
142
|
expect(book.sold).to be(sold)
|
133
143
|
expect(book.finished).to be(finished)
|
144
|
+
expect(book.rate).to be(rate)
|
134
145
|
end
|
135
146
|
|
136
147
|
it 'setter methods accept nil values' do
|
@@ -138,11 +149,12 @@ describe FastAttributes do
|
|
138
149
|
book.title = 'One'
|
139
150
|
book.name = 'Two'
|
140
151
|
book.pages = 250
|
141
|
-
book.price = 2.55
|
152
|
+
book.price = BigDecimal.new("2.55")
|
142
153
|
book.authors = %w[Jobs]
|
143
154
|
book.published = Date.new(2014, 06, 21)
|
144
155
|
book.sold = Time.new(2014, 6, 21, 20, 45, 15)
|
145
156
|
book.finished = DateTime.new(2014, 05, 20, 21, 35, 20)
|
157
|
+
book.rate = 4.1
|
146
158
|
|
147
159
|
book.title = nil
|
148
160
|
book.name = nil
|
@@ -152,6 +164,7 @@ describe FastAttributes do
|
|
152
164
|
book.published = nil
|
153
165
|
book.sold = nil
|
154
166
|
book.finished = nil
|
167
|
+
book.rate = nil
|
155
168
|
|
156
169
|
expect(book.title).to be(nil)
|
157
170
|
expect(book.name).to be(nil)
|
@@ -161,6 +174,7 @@ describe FastAttributes do
|
|
161
174
|
expect(book.published).to be(nil)
|
162
175
|
expect(book.sold).to be(nil)
|
163
176
|
expect(book.finished).to be(nil)
|
177
|
+
expect(book.rate).to be(nil)
|
164
178
|
end
|
165
179
|
|
166
180
|
it 'setter methods raise an exception when cannot parse values' do
|
@@ -169,10 +183,17 @@ describe FastAttributes do
|
|
169
183
|
expect{ book.title = BasicObject.new }.to raise_error(TypeError)
|
170
184
|
expect{ book.name = BasicObject.new }.to raise_error(TypeError)
|
171
185
|
expect{ book.pages = 'number' }.to raise_error(ArgumentError)
|
172
|
-
expect{ book.price = '
|
186
|
+
expect{ book.price = 'bigdecimal' }.to raise_error(ArgumentError)
|
173
187
|
expect{ book.published = 'date' }.to raise_error(ArgumentError)
|
174
188
|
expect{ book.sold = 'time' }.to raise_error(ArgumentError)
|
175
189
|
expect{ book.finished = 'datetime' }.to raise_error(ArgumentError)
|
190
|
+
expect{ book.rate = 'float' }.to raise_error(ArgumentError)
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'setter method can escape placeholder using double %' do
|
194
|
+
placeholder = PlaceholderClass.new
|
195
|
+
placeholder.value = 3
|
196
|
+
expect(placeholder.value).to eq('value %s %value %%s 2')
|
176
197
|
end
|
177
198
|
end
|
178
199
|
|
data/spec/fixtures/classes.rb
CHANGED
@@ -3,11 +3,12 @@ class Book
|
|
3
3
|
|
4
4
|
attribute :title, :name, String
|
5
5
|
attribute :pages, Integer
|
6
|
-
attribute :price,
|
6
|
+
attribute :price, BigDecimal
|
7
7
|
attribute :authors, Array
|
8
8
|
attribute :published, Date
|
9
9
|
attribute :sold, Time
|
10
10
|
attribute :finished, DateTime
|
11
|
+
attribute :rate, Float
|
11
12
|
end
|
12
13
|
|
13
14
|
class Author
|
@@ -71,3 +72,15 @@ class Window
|
|
71
72
|
super.merge('color' => 'white')
|
72
73
|
end
|
73
74
|
end
|
75
|
+
|
76
|
+
class Placeholder < String
|
77
|
+
end
|
78
|
+
|
79
|
+
FastAttributes.type_cast Placeholder do
|
80
|
+
otherwise '"%s %%s %%%s %%%%s #{5%%%s}"'
|
81
|
+
end
|
82
|
+
|
83
|
+
class PlaceholderClass
|
84
|
+
extend FastAttributes
|
85
|
+
attribute :value, Placeholder
|
86
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fast_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.0.
|
4
|
+
version: 0.5.0.rc2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kostiantyn Stepaniuk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|