fast_attributes 0.8.0 → 0.9.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 +20 -0
- data/README.md +24 -0
- data/lib/fast_attributes/builder.rb +8 -3
- data/lib/fast_attributes/version.rb +1 -1
- data/spec/fast_attributes_spec.rb +23 -0
- data/spec/fixtures/classes.rb +51 -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: 9e1112d8c8ba22bbf3f7f044f1ddf9982058c55a
|
4
|
+
data.tar.gz: 94833e4f76a0d946ccb026f1efea6b23f6d2361f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8bfec4938e8e042dfb76413b4b90abf81815066f4348eac9539c02aed03af821436e9c841401baf6bcafe62212d9d0ca31288d92099b24e19193f9d29ad4ad9
|
7
|
+
data.tar.gz: e41f6cf82b500978d3c467c49aa213381138481fc02da1e479a992ed4b53361f1fd585148dceb38b3c77f8fd2d6f47b36446e91a0a5b924927257f12104129f9
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,23 @@
|
|
1
|
+
**0.9.0 (September 15, 2016)**
|
2
|
+
* Attributes via accessors [Phil Schalm](https://github.com/pnomolos)
|
3
|
+
```ruby
|
4
|
+
class Book
|
5
|
+
extend FastAttributes
|
6
|
+
|
7
|
+
define_attributes attributes: :accessors do
|
8
|
+
attribute :author, String
|
9
|
+
end
|
10
|
+
|
11
|
+
def author
|
12
|
+
@author || "No author set"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
book = Book.new
|
17
|
+
book.attributes
|
18
|
+
{"author" => "No author set"}
|
19
|
+
```
|
20
|
+
|
1
21
|
**0.8.0 (September 15, 2016)**
|
2
22
|
* Support of default values [Phil Schalm](https://github.com/pnomolos)
|
3
23
|
```ruby
|
data/README.md
CHANGED
@@ -126,6 +126,30 @@ book.attributes
|
|
126
126
|
{"author" => "Some String"}
|
127
127
|
```
|
128
128
|
|
129
|
+
## Attributes via accessors
|
130
|
+
|
131
|
+
Sometimes you want `attributes` to return the value of your accessors, rather than the instance variables.
|
132
|
+
This is slower than using ivars directly, so use `attributes: :accessors`:
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
class Book
|
136
|
+
extend FastAttributes
|
137
|
+
|
138
|
+
define_attributes attributes: :accessors do
|
139
|
+
attribute :author, String
|
140
|
+
end
|
141
|
+
|
142
|
+
def author
|
143
|
+
@author || "No author set"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
book = Book.new
|
148
|
+
book.attributes
|
149
|
+
{"author" => "No author set"}
|
150
|
+
```
|
151
|
+
|
152
|
+
|
129
153
|
## Custom Type
|
130
154
|
It's easy to add a custom attribute type.
|
131
155
|
```ruby
|
@@ -34,7 +34,7 @@ module FastAttributes
|
|
34
34
|
end
|
35
35
|
|
36
36
|
if @options[:attributes]
|
37
|
-
compile_attributes
|
37
|
+
compile_attributes(@options[:attributes])
|
38
38
|
end
|
39
39
|
|
40
40
|
include_methods
|
@@ -81,10 +81,15 @@ module FastAttributes
|
|
81
81
|
EOS
|
82
82
|
end
|
83
83
|
|
84
|
-
def compile_attributes
|
84
|
+
def compile_attributes(mode)
|
85
85
|
attributes = @attributes.flat_map(&:first)
|
86
|
+
prefix = case mode
|
87
|
+
when :accessors then ''
|
88
|
+
else '@'
|
89
|
+
end
|
90
|
+
|
86
91
|
attributes = attributes.map do |attribute|
|
87
|
-
"'#{attribute}' =>
|
92
|
+
"'#{attribute}' => #{prefix}#{attribute}"
|
88
93
|
end
|
89
94
|
|
90
95
|
@methods.module_eval <<-EOS, __FILE__, __LINE__ + 1
|
@@ -363,6 +363,29 @@ describe FastAttributes do
|
|
363
363
|
expect(reader.attributes).to eq({'name' => '102', 'age' => 25})
|
364
364
|
end
|
365
365
|
end
|
366
|
+
|
367
|
+
describe 'option attributes: :accessors' do
|
368
|
+
it 'doesn\'t interfere when you don\'t use the option' do
|
369
|
+
klass = AttributesWithoutAccessors.new
|
370
|
+
expect(klass.attributes).to eq({'title' => nil, 'pages' => nil, 'color' => 'white'})
|
371
|
+
end
|
372
|
+
|
373
|
+
it "is returns the values of accessors, not the ivars" do
|
374
|
+
klass = AttributesWithAccessors.new(pages: 10, title: 'Something')
|
375
|
+
expect(klass.attributes['pages']).to be(20)
|
376
|
+
expect(klass.attributes['title']).to eq('A Longer Title: Something')
|
377
|
+
end
|
378
|
+
|
379
|
+
it 'is possible to override attributes method' do
|
380
|
+
klass = AttributesWithAccessors.new(pages: 10, title: 'Something')
|
381
|
+
expect(klass.attributes).to eq({'pages' => 20, 'title' => 'A Longer Title: Something', 'color' => 'white'})
|
382
|
+
end
|
383
|
+
|
384
|
+
it 'works with default attributes' do
|
385
|
+
klass = AttributesWithAccessorsAndDefaults.new
|
386
|
+
expect(klass.attributes).to eq({'pages' => 20, 'title' => 'a title'})
|
387
|
+
end
|
388
|
+
end
|
366
389
|
end
|
367
390
|
|
368
391
|
describe "default attributes" do
|
data/spec/fixtures/classes.rb
CHANGED
@@ -114,6 +114,57 @@ class DefaultLenientAttributes
|
|
114
114
|
attribute :active, :boolean
|
115
115
|
end
|
116
116
|
|
117
|
+
class AttributesWithoutAccessors
|
118
|
+
extend FastAttributes
|
119
|
+
|
120
|
+
define_attributes initialize: true, attributes: true do
|
121
|
+
attribute :title, :string
|
122
|
+
attribute :pages, :integer
|
123
|
+
end
|
124
|
+
|
125
|
+
def title
|
126
|
+
"Some title"
|
127
|
+
end
|
128
|
+
|
129
|
+
def attributes
|
130
|
+
super.merge('color' => 'white')
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
class AttributesWithAccessors
|
135
|
+
extend FastAttributes
|
136
|
+
|
137
|
+
define_attributes initialize: true, attributes: :accessors do
|
138
|
+
attribute :title, :string
|
139
|
+
attribute :pages, :integer
|
140
|
+
end
|
141
|
+
|
142
|
+
def attributes
|
143
|
+
super.merge('color' => 'white')
|
144
|
+
end
|
145
|
+
|
146
|
+
def pages
|
147
|
+
@pages + 10
|
148
|
+
end
|
149
|
+
|
150
|
+
def title
|
151
|
+
"A Longer Title: #{@title}"
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
class AttributesWithAccessorsAndDefaults
|
156
|
+
extend FastAttributes
|
157
|
+
|
158
|
+
define_attributes initialize: true, attributes: :accessors do
|
159
|
+
attribute :title, String, default: "a title"
|
160
|
+
attribute :pages, Integer, default: 10
|
161
|
+
end
|
162
|
+
|
163
|
+
def pages
|
164
|
+
@pages + 10
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
117
168
|
class ClassWithDefaults
|
118
169
|
extend FastAttributes
|
119
170
|
|