fast_attributes 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/benchmarks/Gemfile +1 -1
- data/benchmarks/comparison.rb +9 -11
- data/lib/fast_attributes/builder.rb +24 -12
- data/lib/fast_attributes/version.rb +1 -1
- data/spec/fast_attributes_spec.rb +31 -2
- data/spec/fixtures/classes.rb +35 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ba4ff16c6d56690494729bcb81f3af0b2ba1791
|
4
|
+
data.tar.gz: 04f39c4dd270fd4b89d5401ab09bd136d2a1829f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 145edbabbec7ff5205bd2ffc554cd03764bcd869a87a4b201b55fd5788ff0b893faf5b0fd279657b65f57ff9d3b2acc9137226652c16ef2fe390fb479343671c
|
7
|
+
data.tar.gz: 6ad2af448a020de451358208ec55d9916e3d964837ac0db97afaceb5bc61029d717faba04dd33fc5e2aa3aaca0e7c40892e21224cb6286946ad12fd4c3cc7643
|
data/CHANGELOG.md
CHANGED
data/benchmarks/Gemfile
CHANGED
data/benchmarks/comparison.rb
CHANGED
@@ -5,28 +5,26 @@ require 'attrio'
|
|
5
5
|
|
6
6
|
ATTR_NAMES = [:attr0, :attr1, :attr2, :attr3, :attr4, :attr5, :attr6, :attr7, :attr8, :attr9]
|
7
7
|
|
8
|
-
module Initializer
|
9
|
-
def initialize(attributes = {})
|
10
|
-
attributes.each do |attribute, value|
|
11
|
-
public_send("#{attribute}=", value)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
8
|
class FastIntegers
|
17
9
|
extend FastAttributes
|
18
|
-
|
19
|
-
|
10
|
+
define_attributes initialize: true do
|
11
|
+
attribute *ATTR_NAMES, Integer
|
12
|
+
end
|
20
13
|
end
|
21
14
|
|
22
15
|
class AttrioIntegers
|
23
16
|
include Attrio
|
24
|
-
include Initializer
|
25
17
|
define_attributes do
|
26
18
|
ATTR_NAMES.each do |name|
|
27
19
|
attr name, Integer
|
28
20
|
end
|
29
21
|
end
|
22
|
+
|
23
|
+
def initialize(attributes = {})
|
24
|
+
attributes.each do |attribute, value|
|
25
|
+
public_send("#{attribute}=", value)
|
26
|
+
end
|
27
|
+
end
|
30
28
|
end
|
31
29
|
|
32
30
|
class VirtusIntegers
|
@@ -7,6 +7,7 @@ module FastAttributes
|
|
7
7
|
@klass = klass
|
8
8
|
@options = options
|
9
9
|
@attributes = []
|
10
|
+
@methods = Module.new
|
10
11
|
end
|
11
12
|
|
12
13
|
def attribute(*attributes, type)
|
@@ -18,23 +19,25 @@ module FastAttributes
|
|
18
19
|
end
|
19
20
|
|
20
21
|
def compile!
|
21
|
-
compile_getter
|
22
|
-
compile_setter
|
22
|
+
compile_getter
|
23
|
+
compile_setter
|
23
24
|
|
24
25
|
if @options[:initialize]
|
25
|
-
compile_initialize
|
26
|
+
compile_initialize
|
26
27
|
end
|
27
28
|
|
28
29
|
if @options[:attributes]
|
29
|
-
compile_attributes
|
30
|
+
compile_attributes
|
30
31
|
end
|
32
|
+
|
33
|
+
include_methods
|
31
34
|
end
|
32
35
|
|
33
36
|
private
|
34
37
|
|
35
|
-
def compile_getter
|
38
|
+
def compile_getter
|
36
39
|
each_attribute do |attribute, _|
|
37
|
-
@
|
40
|
+
@methods.module_eval <<-EOS, __FILE__, __LINE__ + 1
|
38
41
|
def #{attribute} # def name
|
39
42
|
@#{attribute} # @name
|
40
43
|
end # end
|
@@ -42,12 +45,12 @@ module FastAttributes
|
|
42
45
|
end
|
43
46
|
end
|
44
47
|
|
45
|
-
def compile_setter
|
48
|
+
def compile_setter
|
46
49
|
each_attribute do |attribute, type|
|
47
50
|
type_matching = "when #{type.name} then value"
|
48
51
|
type_casting = FastAttributes.get_type_casting(type) % 'value'
|
49
52
|
|
50
|
-
@
|
53
|
+
@methods.module_eval <<-EOS, __FILE__, __LINE__ + 1
|
51
54
|
def #{attribute}=(value) # def name=(value)
|
52
55
|
@#{attribute} = case value # @name = case value
|
53
56
|
when nil then nil # when nil then nil
|
@@ -60,8 +63,8 @@ module FastAttributes
|
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
63
|
-
def compile_initialize
|
64
|
-
@
|
66
|
+
def compile_initialize
|
67
|
+
@methods.module_eval <<-EOS, __FILE__, __LINE__ + 1
|
65
68
|
def initialize(attributes = {})
|
66
69
|
attributes.each do |name, value|
|
67
70
|
public_send("\#{name}=", value)
|
@@ -70,19 +73,28 @@ module FastAttributes
|
|
70
73
|
EOS
|
71
74
|
end
|
72
75
|
|
73
|
-
def compile_attributes
|
76
|
+
def compile_attributes
|
74
77
|
attributes = @attributes.flat_map(&:first)
|
75
78
|
attributes = attributes.map do |attribute|
|
76
79
|
"'#{attribute}' => @#{attribute}"
|
77
80
|
end
|
78
81
|
|
79
|
-
@
|
82
|
+
@methods.module_eval <<-EOS, __FILE__, __LINE__ + 1
|
80
83
|
def attributes # def attributes
|
81
84
|
{#{attributes.join(', ')}} # {'name' => @name, ...}
|
82
85
|
end # end
|
83
86
|
EOS
|
84
87
|
end
|
85
88
|
|
89
|
+
def include_methods
|
90
|
+
@methods.instance_eval <<-EOS, __FILE__, __LINE__ + 1
|
91
|
+
def inspect
|
92
|
+
'FastAttributes(#{@attributes.flat_map(&:first).join(', ')})'
|
93
|
+
end
|
94
|
+
EOS
|
95
|
+
@klass.send(:include, @methods)
|
96
|
+
end
|
97
|
+
|
86
98
|
def each_attribute
|
87
99
|
@attributes.each do |attributes, type|
|
88
100
|
attributes.each do |attribute|
|
@@ -72,6 +72,13 @@ describe FastAttributes do
|
|
72
72
|
expect(book.respond_to?(:finished)).to be(true)
|
73
73
|
end
|
74
74
|
|
75
|
+
it 'is possible to override getter method' do
|
76
|
+
toy = Toy.new
|
77
|
+
expect(toy.name).to eq(' toy!')
|
78
|
+
toy.name = 'bear'
|
79
|
+
expect(toy.name).to eq('bear toy!')
|
80
|
+
end
|
81
|
+
|
75
82
|
it 'generates setter methods' do
|
76
83
|
book = Book.new
|
77
84
|
expect(book.respond_to?(:title=)).to be(true)
|
@@ -84,6 +91,13 @@ describe FastAttributes do
|
|
84
91
|
expect(book.respond_to?(:finished=)).to be(true)
|
85
92
|
end
|
86
93
|
|
94
|
+
it 'is possible to override setter method' do
|
95
|
+
toy = Toy.new
|
96
|
+
expect(toy.price).to be(nil)
|
97
|
+
toy.price = 2
|
98
|
+
expect(toy.price).to eq(4)
|
99
|
+
end
|
100
|
+
|
87
101
|
it 'setter methods convert values to correct datatype' do
|
88
102
|
book = Book.new
|
89
103
|
book.title = 123
|
@@ -98,7 +112,7 @@ describe FastAttributes do
|
|
98
112
|
expect(book.title).to eq('123')
|
99
113
|
expect(book.name).to eq('456')
|
100
114
|
expect(book.pages).to be(250)
|
101
|
-
expect(book.price).to
|
115
|
+
expect(book.price).to eq(2.55)
|
102
116
|
expect(book.authors).to eq(%w[Jobs])
|
103
117
|
expect(book.published).to eq(Date.new(2014, 6, 21))
|
104
118
|
expect(book.sold).to eq(Time.new(2014, 6, 21, 20, 45, 15))
|
@@ -119,7 +133,7 @@ describe FastAttributes do
|
|
119
133
|
expect(book.title).to be(title)
|
120
134
|
expect(book.name).to be(name)
|
121
135
|
expect(book.pages).to be(pages)
|
122
|
-
expect(book.price).to
|
136
|
+
expect(book.price).to eq(price)
|
123
137
|
expect(book.authors).to be(authors)
|
124
138
|
expect(book.published).to be(published)
|
125
139
|
expect(book.sold).to be(sold)
|
@@ -176,6 +190,16 @@ describe FastAttributes do
|
|
176
190
|
expect(reader.name).to eq('104')
|
177
191
|
expect(reader.age).to be(23)
|
178
192
|
end
|
193
|
+
|
194
|
+
it 'is possible to override initialize method' do
|
195
|
+
window = Window.new
|
196
|
+
expect(window.height).to be(200)
|
197
|
+
expect(window.width).to be(80)
|
198
|
+
|
199
|
+
window = Window.new(height: 210, width: 100)
|
200
|
+
expect(window.height).to be(210)
|
201
|
+
expect(window.width).to be(100)
|
202
|
+
end
|
179
203
|
end
|
180
204
|
|
181
205
|
describe 'option attributes: true' do
|
@@ -187,6 +211,11 @@ describe FastAttributes do
|
|
187
211
|
expect(reader.attributes).to eq({'name' => nil, 'age' => nil})
|
188
212
|
end
|
189
213
|
|
214
|
+
it 'is possible to override attributes method' do
|
215
|
+
window = Window.new(height: 220, width: 100)
|
216
|
+
expect(window.attributes).to eq({'height' => 220, 'width' => 100, 'color' => 'white'})
|
217
|
+
end
|
218
|
+
|
190
219
|
it 'attributes method return all attributes with their values' do
|
191
220
|
publisher = Publisher.new
|
192
221
|
publisher.name = 101
|
data/spec/fixtures/classes.rb
CHANGED
@@ -36,3 +36,38 @@ class Reader
|
|
36
36
|
attribute :age, Integer
|
37
37
|
end
|
38
38
|
end
|
39
|
+
|
40
|
+
class Toy
|
41
|
+
extend FastAttributes
|
42
|
+
|
43
|
+
attribute :name, String
|
44
|
+
attribute :price, Float
|
45
|
+
|
46
|
+
def name
|
47
|
+
"#{super} toy!"
|
48
|
+
end
|
49
|
+
|
50
|
+
def price=(value)
|
51
|
+
super((value.to_f + 2).to_s)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class Window
|
56
|
+
extend FastAttributes
|
57
|
+
|
58
|
+
define_attributes initialize: true, attributes: true do
|
59
|
+
attribute :height, Integer
|
60
|
+
attribute :width, Integer
|
61
|
+
end
|
62
|
+
|
63
|
+
def initialize(attributes = {})
|
64
|
+
self.height = 200
|
65
|
+
self.width = 80
|
66
|
+
|
67
|
+
super(attributes)
|
68
|
+
end
|
69
|
+
|
70
|
+
def attributes
|
71
|
+
super.merge('color' => 'white')
|
72
|
+
end
|
73
|
+
end
|