rrod 1.0.0.alpha.5 → 1.0.0.alpha.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rrod/model/attribute.rb +8 -6
- data/lib/rrod/model/attribute_methods.rb +6 -2
- data/lib/rrod/version.rb +1 -1
- data/spec/rrod/model/attribute_spec.rb +6 -30
- data/spec/rrod/model/schema_spec.rb +20 -0
- data/spec/support/models/person.rb +7 -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: 0c796e27be01b2f1ae14658a4566e5a1628dad0e
|
4
|
+
data.tar.gz: 98d98dae17e3b05f9f56f00b3acee8aee1820501
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1753e525a127b61eee4791ef384d0c26a948d688ed06ff59c6c9b1172dde99ae773143c354acd11c3569dad7f23a7eb498291c6aaf33b63af21a5ad2aa792c3a
|
7
|
+
data.tar.gz: cf1565f5cd0515bad8c890501749b6be59ea78446e1e0a048a3baee316ae08a78e485374f1d3581ef47f7c187e8b59543c9bb5666f3d991b227c8958925bccfa
|
data/lib/rrod/model/attribute.rb
CHANGED
@@ -29,15 +29,11 @@ module Rrod
|
|
29
29
|
set_index if options.delete(:index)
|
30
30
|
end
|
31
31
|
|
32
|
-
# @return [Object] default value or result of call on default value
|
33
|
-
def default(instance)
|
34
|
-
@default.respond_to?(:call) ? instance.instance_exec(&@default) : @default
|
35
|
-
end
|
36
|
-
|
37
32
|
def define
|
38
33
|
define_reader
|
39
34
|
define_writer
|
40
35
|
define_presence
|
36
|
+
define_default
|
41
37
|
apply_validators
|
42
38
|
self
|
43
39
|
end
|
@@ -79,7 +75,13 @@ module Rrod
|
|
79
75
|
def define_presence
|
80
76
|
model.send :define_method, "#{name}?", self.class.presence_definition(name)
|
81
77
|
end
|
82
|
-
|
78
|
+
|
79
|
+
def define_default
|
80
|
+
value = default unless default.respond_to?(:call)
|
81
|
+
method = default.respond_to?(:call) ? default : -> {value}
|
82
|
+
model.send :define_method, "#{name}_default", method
|
83
|
+
end
|
84
|
+
|
83
85
|
def set_index
|
84
86
|
@index = Index.new(self)
|
85
87
|
end
|
@@ -39,8 +39,7 @@ module Rrod
|
|
39
39
|
# @param [Symbol, String] the key of the attribute to read
|
40
40
|
# @return the attribute at the given key
|
41
41
|
def read_attribute(key)
|
42
|
-
@attributes[key.to_s] ||
|
43
|
-
write_attribute(key, self.class.attributes[key.to_sym].try(:default, self))
|
42
|
+
@attributes[key.to_s] || write_attribute(key, read_default(key))
|
44
43
|
end
|
45
44
|
alias :[] :read_attribute
|
46
45
|
|
@@ -52,6 +51,11 @@ module Rrod
|
|
52
51
|
end
|
53
52
|
alias :[]= :write_attribute
|
54
53
|
|
54
|
+
def read_default(key)
|
55
|
+
method = "#{key}_default"
|
56
|
+
send(method) if respond_to?(method)
|
57
|
+
end
|
58
|
+
|
55
59
|
private
|
56
60
|
|
57
61
|
def magic_methods=(keys)
|
data/lib/rrod/version.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
Rrod = Module.new unless defined? Rrod
|
2
|
-
Rrod::VERSION = '1.0.0.alpha.
|
2
|
+
Rrod::VERSION = '1.0.0.alpha.6'
|
@@ -29,36 +29,6 @@ describe Rrod::Model::Attribute do
|
|
29
29
|
expect(attribute.options).to eq(options)
|
30
30
|
end
|
31
31
|
|
32
|
-
describe "defaults" do
|
33
|
-
let(:default) { attribute.default(instance) }
|
34
|
-
|
35
|
-
context "when a value" do
|
36
|
-
let(:options) { {default: 'SOO fluffy!'} }
|
37
|
-
|
38
|
-
it "can provide a default value" do
|
39
|
-
expect(default).to eq 'SOO fluffy!'
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
context "when a proc" do
|
44
|
-
let(:options) { {default: -> { 'alligator' }} }
|
45
|
-
|
46
|
-
it "can provide a default value if a proc" do
|
47
|
-
expect(default).to eq 'alligator'
|
48
|
-
end
|
49
|
-
|
50
|
-
context "given an instance" do
|
51
|
-
let(:model) { Class.new { include Rrod::Model; attr_accessor :foo } }
|
52
|
-
let(:options) { {default: -> { foo.upcase }} }
|
53
|
-
|
54
|
-
it "evaluates in the context of the given instance" do
|
55
|
-
instance.foo = 'whoah'
|
56
|
-
expect(default).to eq('WHOAH')
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
32
|
describe "defining attribute methods" do
|
63
33
|
before(:each) { attribute.define }
|
64
34
|
|
@@ -79,6 +49,12 @@ describe Rrod::Model::Attribute do
|
|
79
49
|
expect(instance).to respond_to("#{name}?")
|
80
50
|
end
|
81
51
|
end
|
52
|
+
|
53
|
+
describe "default" do
|
54
|
+
it "adds the default" do
|
55
|
+
expect(instance).to respond_to("#{name}_default")
|
56
|
+
end
|
57
|
+
end
|
82
58
|
end
|
83
59
|
|
84
60
|
describe "casting" do
|
@@ -14,6 +14,26 @@ describe Rrod::Model::Schema do
|
|
14
14
|
expect(instance).to respond_to(:name=)
|
15
15
|
end
|
16
16
|
|
17
|
+
it "defines a default for the attribute if provided as a scalar" do
|
18
|
+
expect(instance.likes_pets_default).to be true
|
19
|
+
end
|
20
|
+
|
21
|
+
it "defines a default for the attribute if provided as a proc" do
|
22
|
+
expect(instance.comments).to eq([instance.build_comment])
|
23
|
+
end
|
24
|
+
|
25
|
+
context "subclassing" do
|
26
|
+
let(:model) { Class.new(Person) }
|
27
|
+
|
28
|
+
it "provides defaults" do
|
29
|
+
expect(instance.likes_pets_default).to be true
|
30
|
+
end
|
31
|
+
|
32
|
+
it "provides defaults to readers" do
|
33
|
+
expect(instance.likes_pets).to be true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
17
37
|
it "will properly typecast when writing an attribute" do
|
18
38
|
instance.gender = 'female'
|
19
39
|
expect(instance.gender).to be :female
|
@@ -40,12 +40,19 @@ class Person
|
|
40
40
|
attribute :age, Integer, numericality: {min: 10}
|
41
41
|
attribute :gender, Symbol
|
42
42
|
|
43
|
+
attribute :likes_pets, Boolean, default: true
|
44
|
+
attribute :comments, Array, default: -> { [build_comment] }
|
45
|
+
|
43
46
|
attribute :address, Address
|
44
47
|
|
45
48
|
attribute :pets, [Pet]
|
46
49
|
|
47
50
|
validates_length_of :pets, minimum: 1
|
48
51
|
|
52
|
+
def build_comment
|
53
|
+
{name: 'Adam', text: 'What kind of comment is this really?'}
|
54
|
+
end
|
55
|
+
|
49
56
|
private
|
50
57
|
|
51
58
|
def stuffs
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rrod
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.alpha.
|
4
|
+
version: 1.0.0.alpha.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Hunter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: riak-client
|