rrod 1.0.0.alpha.6 → 1.0.0.alpha.7
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 +7 -0
- data/lib/rrod/model/schema.rb +17 -5
- data/lib/rrod/version.rb +1 -1
- data/spec/rrod/model/attribute_spec.rb +10 -0
- data/spec/rrod/model/schema_spec.rb +11 -1
- data/spec/support/models/person.rb +1 -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: f7c4fed6f4b07652c23c34383bad82edc55ef84f
|
4
|
+
data.tar.gz: ded28382a99937aecdf3d0f02718abc0f2dfb65f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 70db13dd62184794148611c3fa4a1258ae1f560d52cb0cad10b6c677b1f4320bf6886cfb63d1a28c204d40c5549c4d01128633821a12ce2fb55672030d5e7369
|
7
|
+
data.tar.gz: eef5ddd4e356cdaf0e07d9e785d62c5bcf27260d3256146e4f7be73b2dfb976e0fdbe9f2ea8f01b0073de175209dac32ad4d521bf0d079e157dd7eaa96323bf6
|
data/lib/rrod/model/attribute.rb
CHANGED
@@ -30,6 +30,7 @@ module Rrod
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def define
|
33
|
+
define_attribute
|
33
34
|
define_reader
|
34
35
|
define_writer
|
35
36
|
define_presence
|
@@ -64,6 +65,12 @@ module Rrod
|
|
64
65
|
-> { Rrod::Model::Collection.new(self, model_class) }
|
65
66
|
end
|
66
67
|
|
68
|
+
def define_attribute
|
69
|
+
_self = self # i am a shadow of self
|
70
|
+
model.define_singleton_method(
|
71
|
+
"#{Rrod::Model::Schema::RROD_ATTRIBUTE_PREFIX}#{name}") { _self }
|
72
|
+
end
|
73
|
+
|
67
74
|
def define_reader
|
68
75
|
model.send :define_method, name, self.class.reader_definition(name)
|
69
76
|
end
|
data/lib/rrod/model/schema.rb
CHANGED
@@ -2,25 +2,37 @@ module Rrod
|
|
2
2
|
module Model
|
3
3
|
module Schema
|
4
4
|
|
5
|
+
RROD_ATTRIBUTE_PREFIX = "rrod_attribute_".freeze
|
6
|
+
|
5
7
|
def attributes
|
6
|
-
@attributes ||= {}
|
8
|
+
@attributes ||= public_methods.map(&:to_s).reduce({}) { |acc, method|
|
9
|
+
next acc unless method.starts_with?(RROD_ATTRIBUTE_PREFIX)
|
10
|
+
key = method[RROD_ATTRIBUTE_PREFIX.length..-1]
|
11
|
+
acc.tap { |hash| hash[key] = send(method) }
|
12
|
+
}.with_indifferent_access
|
7
13
|
end
|
8
14
|
|
9
15
|
def attribute(name, type, options={})
|
10
|
-
|
16
|
+
using_schema!
|
17
|
+
Attribute.new(self, name, type, options).define
|
11
18
|
end
|
12
19
|
|
13
20
|
def cast_attribute(key, value, instance)
|
14
|
-
attribute = attributes[key
|
21
|
+
attribute = attributes[key]
|
15
22
|
attribute ? attribute.cast(value, instance) : value
|
16
23
|
end
|
17
24
|
|
18
25
|
def nested_in(parent)
|
19
|
-
define_method(parent) {
|
26
|
+
define_method(parent) { _parent }
|
27
|
+
end
|
28
|
+
|
29
|
+
def using_schema!
|
30
|
+
return if schema?
|
31
|
+
instance_eval 'def schema?; true; end'
|
20
32
|
end
|
21
33
|
|
22
34
|
def schema?
|
23
|
-
|
35
|
+
false
|
24
36
|
end
|
25
37
|
|
26
38
|
def rrod_cast(value, parent=nil)
|
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.7'
|
@@ -55,6 +55,16 @@ describe Rrod::Model::Attribute do
|
|
55
55
|
expect(instance).to respond_to("#{name}_default")
|
56
56
|
end
|
57
57
|
end
|
58
|
+
|
59
|
+
describe "attribute definitions" do
|
60
|
+
it "adds the attribute definition to to model class" do
|
61
|
+
expect(model).to respond_to("rrod_attribute_#{name}")
|
62
|
+
end
|
63
|
+
|
64
|
+
it "allows access to the attribute from the class" do
|
65
|
+
expect(model.send("rrod_attribute_#{name}")).to eq attribute
|
66
|
+
end
|
67
|
+
end
|
58
68
|
end
|
59
69
|
|
60
70
|
describe "casting" do
|
@@ -32,6 +32,16 @@ describe Rrod::Model::Schema do
|
|
32
32
|
it "provides defaults to readers" do
|
33
33
|
expect(instance.likes_pets).to be true
|
34
34
|
end
|
35
|
+
|
36
|
+
it "has a schema if its parent does" do
|
37
|
+
expect(model.schema?).to be true
|
38
|
+
end
|
39
|
+
|
40
|
+
it "nests inside its parent properly" do
|
41
|
+
pet = Pet.new(name: 'Molle')
|
42
|
+
instance.pets = [pet]
|
43
|
+
expect(instance.pets.first.owner).to be_a(Person)
|
44
|
+
end
|
35
45
|
end
|
36
46
|
|
37
47
|
it "will properly typecast when writing an attribute" do
|
@@ -40,7 +50,7 @@ describe Rrod::Model::Schema do
|
|
40
50
|
end
|
41
51
|
|
42
52
|
it "is using a schema if an attribute is declared" do
|
43
|
-
expect(Person.schema?).to
|
53
|
+
expect(Person.schema?).to be true
|
44
54
|
end
|
45
55
|
|
46
56
|
describe "associations", integration: true do
|
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.7
|
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-
|
11
|
+
date: 2015-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: riak-client
|