attribrutal 0.0.10 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/attribrutal/coercion.rb +7 -3
- data/lib/attribrutal/model.rb +8 -2
- data/lib/attribrutal/version.rb +1 -1
- data/spec/attribrutal_spec.rb +15 -2
- data/spec/spec_helper.rb +1 -0
- data/spec/support/models/typed_collections.rb +7 -0
- metadata +15 -27
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7b00666ccfeb72731d0ec6b9693c9ede08f5503b
|
4
|
+
data.tar.gz: 5697a572c868ea84e8d5eb177bb2549c76a6d509
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cf798b0c0e5e2c03fbdd88e5b3903e3c205e20041a0f4024abc92e701c820c0492dc0c6b16550100c1eb96f03dae505240923db5afd933fe17780932f9c3a420
|
7
|
+
data.tar.gz: 9a36ffeb06c84ff163b2845b7ef24533f3987290a795a97f84c127b6cd2ccef694da3827608e25367a4ba9510d23fcd0d71bd960115b42b9e03750dbd1da41bd
|
data/lib/attribrutal/coercion.rb
CHANGED
@@ -32,14 +32,18 @@ module Attribrutal
|
|
32
32
|
end
|
33
33
|
|
34
34
|
class Array
|
35
|
-
|
35
|
+
|
36
|
+
def initialize(subtype)
|
37
|
+
@subtype = subtype
|
38
|
+
end
|
39
|
+
|
40
|
+
def coerce(arg, default = nil)
|
36
41
|
return default unless arg
|
37
42
|
arg.collect {|member| @subtype.coerce(member) }
|
38
43
|
end
|
39
44
|
|
40
45
|
def self.[] (subtype)
|
41
|
-
|
42
|
-
self
|
46
|
+
new(subtype)
|
43
47
|
end
|
44
48
|
end
|
45
49
|
|
data/lib/attribrutal/model.rb
CHANGED
@@ -32,7 +32,13 @@ module Attribrutal
|
|
32
32
|
|
33
33
|
def attribute (sym, coercer=nil, attrs = {})
|
34
34
|
|
35
|
-
|
35
|
+
if coercer == Attribrutal::Type::Boolean
|
36
|
+
superclass.send :define_method, "#{sym}?" do
|
37
|
+
send(sym)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
superclass.send :define_method, sym do
|
36
42
|
default_value = case attrs[:default].class.name
|
37
43
|
when "NilClass", "TrueClass", "FalseClass", "Numeric", "Fixnum", "Symbol"
|
38
44
|
attrs[:default]
|
@@ -48,7 +54,7 @@ module Attribrutal
|
|
48
54
|
end
|
49
55
|
end
|
50
56
|
|
51
|
-
define_method
|
57
|
+
superclass.send :define_method, "#{sym}=".to_sym do |value|
|
52
58
|
raw_attributes[sym] = value
|
53
59
|
end
|
54
60
|
|
data/lib/attribrutal/version.rb
CHANGED
data/spec/attribrutal_spec.rb
CHANGED
@@ -2,8 +2,10 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe "Attribrutal#attributes" do
|
4
4
|
|
5
|
-
let (:bar)
|
6
|
-
let (:bar_with_defaults)
|
5
|
+
let (:bar) { Bar.new foo: nil, bar: 10, baz: { alpha: 10, beta: "20" } }
|
6
|
+
let (:bar_with_defaults) { Bar.new }
|
7
|
+
let (:typed_collections) { TypedCollections.new integers: ["1","2","3"], strings: [true, 10, 1.0] }
|
8
|
+
let (:typed_collections_with_defaults) { TypedCollections.new }
|
7
9
|
|
8
10
|
it "records defined attributes" do
|
9
11
|
Bar.attributes.keys.should == [:foo, :bar, :baz]
|
@@ -33,6 +35,13 @@ describe "Attribrutal#attributes" do
|
|
33
35
|
attributes[:baz].beta.should == 20
|
34
36
|
end
|
35
37
|
|
38
|
+
it "supports array of type coercion" do
|
39
|
+
typed_collections.integers.should == [1, 2 ,3]
|
40
|
+
typed_collections.strings.should == ["true", "10", "1.0"]
|
41
|
+
typed_collections_with_defaults.integers.should == [42, 42, 42]
|
42
|
+
typed_collections_with_defaults.strings.should == ['larry', 'curly', 'moe']
|
43
|
+
end
|
44
|
+
|
36
45
|
it "supports defaults" do
|
37
46
|
bar_with_defaults.bar.should == "bar"
|
38
47
|
bar_with_defaults.baz.class.should == Baz
|
@@ -40,6 +49,10 @@ describe "Attribrutal#attributes" do
|
|
40
49
|
bar_with_defaults.baz.beta.should == 100
|
41
50
|
end
|
42
51
|
|
52
|
+
it "defines symbol? if boolean" do
|
53
|
+
bar.foo?.should == false
|
54
|
+
end
|
55
|
+
|
43
56
|
it "is fast enough" do
|
44
57
|
initialization_time = 1000 * Benchmark.realtime do
|
45
58
|
1000.times { Bar.new }
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,7 @@
|
|
1
|
+
class TypedCollections
|
2
|
+
include Attribrutal::Model
|
3
|
+
|
4
|
+
attribute :strings, Attribrutal::Type::Array[Attribrutal::Type::String], default: ["larry", "curly", "moe"]
|
5
|
+
attribute :integers, Attribrutal::Type::Array[Attribrutal::Type::Integer], default: [42, 42, 42]
|
6
|
+
|
7
|
+
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribrutal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.12
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Bukowskis
|
@@ -14,49 +13,43 @@ dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rspec
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ">="
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
description: ''
|
@@ -65,7 +58,7 @@ executables: []
|
|
65
58
|
extensions: []
|
66
59
|
extra_rdoc_files: []
|
67
60
|
files:
|
68
|
-
- .gitignore
|
61
|
+
- ".gitignore"
|
69
62
|
- Gemfile
|
70
63
|
- Gemfile.lock
|
71
64
|
- LICENSE.txt
|
@@ -81,36 +74,30 @@ files:
|
|
81
74
|
- spec/support/models/bar.rb
|
82
75
|
- spec/support/models/baz.rb
|
83
76
|
- spec/support/models/coercer.rb
|
77
|
+
- spec/support/models/typed_collections.rb
|
84
78
|
homepage: ''
|
85
79
|
licenses:
|
86
80
|
- MIT
|
81
|
+
metadata: {}
|
87
82
|
post_install_message:
|
88
83
|
rdoc_options: []
|
89
84
|
require_paths:
|
90
85
|
- lib
|
91
86
|
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
-
none: false
|
93
87
|
requirements:
|
94
|
-
- -
|
88
|
+
- - ">="
|
95
89
|
- !ruby/object:Gem::Version
|
96
90
|
version: '0'
|
97
|
-
segments:
|
98
|
-
- 0
|
99
|
-
hash: 4418677033310836668
|
100
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
-
none: false
|
102
92
|
requirements:
|
103
|
-
- -
|
93
|
+
- - ">="
|
104
94
|
- !ruby/object:Gem::Version
|
105
95
|
version: '0'
|
106
|
-
segments:
|
107
|
-
- 0
|
108
|
-
hash: 4418677033310836668
|
109
96
|
requirements: []
|
110
97
|
rubyforge_project:
|
111
|
-
rubygems_version:
|
98
|
+
rubygems_version: 2.2.2
|
112
99
|
signing_key:
|
113
|
-
specification_version:
|
100
|
+
specification_version: 4
|
114
101
|
summary: Lazy attribute coercion
|
115
102
|
test_files:
|
116
103
|
- spec/attribrutal_spec.rb
|
@@ -118,3 +105,4 @@ test_files:
|
|
118
105
|
- spec/support/models/bar.rb
|
119
106
|
- spec/support/models/baz.rb
|
120
107
|
- spec/support/models/coercer.rb
|
108
|
+
- spec/support/models/typed_collections.rb
|