attrio 0.7.1 → 0.7.2
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/README.md +24 -23
- data/lib/attrio/builders/writer_builder.rb +1 -1
- data/lib/attrio/types/array.rb +8 -4
- data/lib/attrio/types/base.rb +1 -1
- data/lib/attrio/types/boolean.rb +1 -1
- data/lib/attrio/types/date.rb +1 -1
- data/lib/attrio/types/date_time.rb +1 -1
- data/lib/attrio/types/float.rb +1 -1
- data/lib/attrio/types/integer.rb +1 -1
- data/lib/attrio/types/set.rb +1 -1
- data/lib/attrio/types/symbol.rb +1 -1
- data/lib/attrio/types/time.rb +1 -1
- data/lib/attrio/version.rb +1 -1
- 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: c37cd18841fadc28823ecaa640d6305070784769
|
4
|
+
data.tar.gz: fbff0c3dac7fba2297dc760ba7483110cd15343c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: abd5ad3981872e97d9d9c82702949583663265c4a20aef0c3844aa0793adaefb847a8e864e5884464c1a9ee960e56fb9d86eafba735f3dd0000d83fd117bed52
|
7
|
+
data.tar.gz: 5cc8f267baabd969c5d4d54a8eb0ee37260b542777f8a8a28987d63de7ddc3ab256f4ef9967d64e6bd7834d16a27b6bed23f25027d9ef1ddde7d1fe785099248
|
data/README.md
CHANGED
@@ -29,7 +29,7 @@ Include Attrio into your class and then use `#define_attributes` block to declar
|
|
29
29
|
```ruby
|
30
30
|
class User
|
31
31
|
include Attrio
|
32
|
-
|
32
|
+
|
33
33
|
define_attributes do
|
34
34
|
attr :name, String
|
35
35
|
attr :age, Integer
|
@@ -46,10 +46,10 @@ By default Attrio defines `#attributes` accessor which contains `Hash` with attr
|
|
46
46
|
* reader method visibility
|
47
47
|
* instance variable name
|
48
48
|
* additional options
|
49
|
-
|
49
|
+
|
50
50
|
```ruby
|
51
51
|
user = User.new
|
52
|
-
user.attributes
|
52
|
+
user.attributes
|
53
53
|
# => {
|
54
54
|
# :name => #<Attrio::Attribute:0x007fc44e8ca680 @object=#<User:0x007fc44e8b2b48>, @name="name", @type=String, @options={}, @writer_method_name="name=", @writer_visibility=:public, @instance_variable_name="@name", @reader_method_name="name", @reader_visibility=:public>,
|
55
55
|
# :age => #<Attrio::Attribute:0x007fc44e8d4c98 @object=#<User:0x007fc44e8b2b48>, @name="age", @type=Attrio::Types::Integer, @options={}, @writer_method_name="age=", @writer_visibility=:public, @instance_variable_name="@age", @reader_method_name="age", @reader_visibility=:public>,
|
@@ -71,17 +71,17 @@ Accessor name can be easily overridden by passing `:as` option to `define_attrib
|
|
71
71
|
```ruby
|
72
72
|
class User
|
73
73
|
include Attrio
|
74
|
-
|
74
|
+
|
75
75
|
define_attributes :as => 'api_attributes' do
|
76
76
|
attr :name, String
|
77
77
|
attr :age, Integer
|
78
78
|
attr :birthday, DateTime
|
79
79
|
end
|
80
|
-
|
80
|
+
|
81
81
|
define_attributes :as => 'settings' do
|
82
82
|
attr :receives_notifications, Boolean, :default => true
|
83
83
|
end
|
84
|
-
|
84
|
+
|
85
85
|
end
|
86
86
|
```
|
87
87
|
|
@@ -113,7 +113,7 @@ class Page
|
|
113
113
|
# default from a method name as symbol
|
114
114
|
attr :editor_title, String, :default => :default_editor_title
|
115
115
|
end
|
116
|
-
|
116
|
+
|
117
117
|
def initialize(attributes = {})
|
118
118
|
self.attributes = attributes
|
119
119
|
end
|
@@ -129,7 +129,7 @@ class Page
|
|
129
129
|
title
|
130
130
|
else
|
131
131
|
title.present? ? "UNPUBLISHED: #{title}" : "UNPUBLISHED"
|
132
|
-
end
|
132
|
+
end
|
133
133
|
end
|
134
134
|
end
|
135
135
|
```
|
@@ -142,7 +142,7 @@ p = Page.new
|
|
142
142
|
p.attributes[:editor_title].default?
|
143
143
|
=> true
|
144
144
|
p.editor_title = 'PUBLISHED'
|
145
|
-
=> "PUBLISHED"
|
145
|
+
=> "PUBLISHED"
|
146
146
|
p.attributes[:editor_title].default?
|
147
147
|
=> false
|
148
148
|
```
|
@@ -163,7 +163,7 @@ module MassAssignment
|
|
163
163
|
end
|
164
164
|
end
|
165
165
|
|
166
|
-
class City
|
166
|
+
class City
|
167
167
|
include Attrio
|
168
168
|
include MassAssignment
|
169
169
|
|
@@ -204,12 +204,12 @@ user.address.city.name
|
|
204
204
|
|
205
205
|
### Methods visibility
|
206
206
|
|
207
|
-
Don't want your accessors to be public? Visibility can be overridden easily.
|
207
|
+
Don't want your accessors to be public? Visibility can be overridden easily.
|
208
208
|
|
209
209
|
```ruby
|
210
210
|
class User
|
211
211
|
include Attrio
|
212
|
-
|
212
|
+
|
213
213
|
define_attributes do
|
214
214
|
attr :name, String, :writer => :protected
|
215
215
|
attr :secret_rating, Integer, :reader => :private
|
@@ -224,8 +224,8 @@ Any Ruby class can be passed as type to Attrio. If this class responds to `typec
|
|
224
224
|
```ruby
|
225
225
|
class Klass
|
226
226
|
include Attrio
|
227
|
-
|
228
|
-
define_attributes do
|
227
|
+
|
228
|
+
define_attributes do
|
229
229
|
attr :custom_attribute, CustomClass
|
230
230
|
end
|
231
231
|
end
|
@@ -240,10 +240,10 @@ By default boolean typecasts 'yes', '1', 1, 'true' as `TrueClass` and all other
|
|
240
240
|
```ruby
|
241
241
|
class Klass
|
242
242
|
include Attrio
|
243
|
-
|
243
|
+
|
244
244
|
define_attributes do
|
245
245
|
attr :boolean_attribute, Boolean
|
246
|
-
|
246
|
+
|
247
247
|
attr :custom_boolean_attribute, Boolean, :yes => ['ja', '1', 1]
|
248
248
|
# attr :custom_boolean_attribute, Boolean, :yes_values => ['ja', '1', 1]
|
249
249
|
# attr :custom_boolean_attribute, Boolean, :no => ['nein', '0', 0]
|
@@ -259,12 +259,12 @@ These three class have similar behaviour and options. By passing `:format` optio
|
|
259
259
|
```ruby
|
260
260
|
class Klass
|
261
261
|
include Attrio
|
262
|
-
|
262
|
+
|
263
263
|
define_attributes do
|
264
264
|
attr :date_attribute, Date
|
265
265
|
attr :time_attribute, Time
|
266
266
|
attr :date_time_attribute, DateTime
|
267
|
-
|
267
|
+
|
268
268
|
attr :custom_date_time_attribute, DateTime, :format => '%m/%d/%y-%H:%M:%S-%z'
|
269
269
|
end
|
270
270
|
end
|
@@ -277,7 +277,7 @@ Attribute will be typecasted using `to_f` method.
|
|
277
277
|
```ruby
|
278
278
|
class Klass
|
279
279
|
include Attrio
|
280
|
-
|
280
|
+
|
281
281
|
define_attributes do
|
282
282
|
attr :float_attribute, Float
|
283
283
|
end
|
@@ -293,7 +293,7 @@ Optional `:base` parameter can be passed, during the typecast attribute will be
|
|
293
293
|
```ruby
|
294
294
|
class Klass
|
295
295
|
include Attrio
|
296
|
-
|
296
|
+
|
297
297
|
define_attributes do
|
298
298
|
attr :integer_attribute, Integer
|
299
299
|
attr :custom_integer_attribute, Integer, :base => 2
|
@@ -305,12 +305,12 @@ end
|
|
305
305
|
|
306
306
|
Attribute will be typecasted using `to_sym` method.
|
307
307
|
|
308
|
-
If Optional `:underscore` parameter is passed, then attribute value will be downcased and underscored before calling `to_sym`.
|
308
|
+
If Optional `:underscore` parameter is passed, then attribute value will be downcased and underscored before calling `to_sym`.
|
309
309
|
|
310
310
|
```ruby
|
311
311
|
class Klass
|
312
312
|
include Attrio
|
313
|
-
|
313
|
+
|
314
314
|
define_attributes do
|
315
315
|
attr :symbol_attribute, Symbol
|
316
316
|
attr :custom_symbol_attribute, Symbol, :underscore => true
|
@@ -341,7 +341,7 @@ Attrio adds its own `#inspect` method when included to the class. This overridde
|
|
341
341
|
```ruby
|
342
342
|
class Klass
|
343
343
|
include Attrio
|
344
|
-
|
344
|
+
|
345
345
|
define_attributes :inspect => false do
|
346
346
|
attr :attribute, String
|
347
347
|
end
|
@@ -369,6 +369,7 @@ Contributors:
|
|
369
369
|
|
370
370
|
* [Igor Alexandrov](http://igor-alexandrov.github.com/)
|
371
371
|
* [Julia Egorova](https://github.com/vankiru)
|
372
|
+
* [Dmitry Radionov](https://github.com/Gikls)
|
372
373
|
|
373
374
|
## License
|
374
375
|
|
@@ -25,7 +25,7 @@ module Attrio
|
|
25
25
|
klass.send :define_method, options[:method_name] do |value|
|
26
26
|
if !value.nil?
|
27
27
|
value = if type.respond_to?(:typecast) && type.respond_to?(:typecasted?)
|
28
|
-
type.typecasted?(value) ? value : type.typecast(*[value, options])
|
28
|
+
type.typecasted?(value, options) ? value : type.typecast(*[value, options])
|
29
29
|
else
|
30
30
|
type == Hash && value.is_a?(Hash) ? value : type.new(value)
|
31
31
|
end
|
data/lib/attrio/types/array.rb
CHANGED
@@ -5,8 +5,7 @@ module Attrio
|
|
5
5
|
class Array < Base
|
6
6
|
def self.typecast(value, options = {})
|
7
7
|
begin
|
8
|
-
array = value.respond_to?(:split) ? value.split(options[:split]) : Attrio::Helpers.to_a(value)
|
9
|
-
|
8
|
+
array = (!value.is_a?(Array) && value.respond_to?(:split)) ? value.split(options[:split]) : Attrio::Helpers.to_a(value).flatten!
|
10
9
|
if options[:element].present?
|
11
10
|
type = Attrio::AttributesParser.cast_type(self.element_type(options[:element]))
|
12
11
|
options = self.element_options(options[:element])
|
@@ -26,8 +25,13 @@ module Attrio
|
|
26
25
|
end
|
27
26
|
end
|
28
27
|
|
29
|
-
def self.typecasted?(value)
|
30
|
-
|
28
|
+
def self.typecasted?(value, options = {})
|
29
|
+
if options[:element].present?
|
30
|
+
type = Attrio::AttributesParser.cast_type(self.element_type(options[:element]))
|
31
|
+
value.is_a?(::Array) && !value.any?{ |e| !e.is_a?(type) }
|
32
|
+
else
|
33
|
+
value.is_a?(::Array)
|
34
|
+
end
|
31
35
|
end
|
32
36
|
|
33
37
|
protected
|
data/lib/attrio/types/base.rb
CHANGED
data/lib/attrio/types/boolean.rb
CHANGED
data/lib/attrio/types/date.rb
CHANGED
data/lib/attrio/types/float.rb
CHANGED
data/lib/attrio/types/integer.rb
CHANGED
data/lib/attrio/types/set.rb
CHANGED
data/lib/attrio/types/symbol.rb
CHANGED
data/lib/attrio/types/time.rb
CHANGED
data/lib/attrio/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attrio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Alexandrov
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-03-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|