rrod 1.0.0.alpha.3.1 → 1.0.0.alpha.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rrod/caster/nested_model.rb +3 -3
- data/lib/rrod/caster.rb +2 -2
- data/lib/rrod/model/attribute.rb +18 -3
- data/lib/rrod/model/attribute_methods.rb +1 -3
- data/lib/rrod/model/collection.rb +5 -4
- data/lib/rrod/model/persistence.rb +6 -0
- data/lib/rrod/model/schema.rb +15 -7
- data/lib/rrod/test_server/rspec.rb +4 -4
- data/lib/rrod/version.rb +1 -1
- data/rrod.gemspec +1 -1
- data/spec/rrod/model/attribute_spec.rb +7 -1
- data/spec/rrod/model/collection_spec.rb +12 -3
- data/spec/rrod/model/persistence_spec.rb +13 -0
- data/spec/rrod/model/schema_spec.rb +8 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73a47a1b2feaa5f8ca345afab9b483e5755d710b
|
4
|
+
data.tar.gz: 000a4429b93fe6a68df919e3a1295abc27a71a0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4267a72e45678a7f60eb7fbd756d7fd88bdfb552a38eb596dcf768d59aa99dece04a297151e31d6dfb70f59c5d231b1d635e2dec80278724d2c930b8ea6ba4d7
|
7
|
+
data.tar.gz: 74ef9e7c3c8d511e080fbc143c62ff6dc9813902cbe427bb6de738672d213ab0eb58f305fa6202f9ba6f33a815ddc9162917d387403e99d6474d084368ce40de
|
@@ -2,12 +2,12 @@ module Rrod
|
|
2
2
|
class Caster
|
3
3
|
module NestedModel
|
4
4
|
|
5
|
-
def rrod_cast(values)
|
5
|
+
def rrod_cast(values, model)
|
6
6
|
return if values.nil?
|
7
|
-
Rrod::Model::Collection.new(model, values)
|
7
|
+
Rrod::Model::Collection.new(model, model_class, values)
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def model_class
|
11
11
|
first
|
12
12
|
end
|
13
13
|
|
data/lib/rrod/caster.rb
CHANGED
@@ -5,14 +5,14 @@ module Rrod
|
|
5
5
|
@casting = casting
|
6
6
|
end
|
7
7
|
|
8
|
-
def rrod_cast(value)
|
8
|
+
def rrod_cast(value, instance=nil)
|
9
9
|
return if value.nil?
|
10
10
|
@casting.(value)
|
11
11
|
end
|
12
12
|
|
13
13
|
module Boolean
|
14
14
|
extend self
|
15
|
-
def rrod_cast(value)
|
15
|
+
def rrod_cast(value, instance=nil)
|
16
16
|
[nil, false, 'false', 0, '0'].include?(value) ? false : true
|
17
17
|
end
|
18
18
|
end
|
data/lib/rrod/model/attribute.rb
CHANGED
@@ -9,6 +9,10 @@ module Rrod
|
|
9
9
|
def self.writer_definition(name)
|
10
10
|
->(value) { write_attribute name, value }
|
11
11
|
end
|
12
|
+
|
13
|
+
def self.presence_definition(name)
|
14
|
+
-> { self[name].present? }
|
15
|
+
end
|
12
16
|
|
13
17
|
attr_accessor :model, :name, :type, :options, :default, :index
|
14
18
|
|
@@ -20,7 +24,7 @@ module Rrod
|
|
20
24
|
self.model = model
|
21
25
|
self.name = name.to_sym
|
22
26
|
self.type = type
|
23
|
-
self.default = options.delete(:default)
|
27
|
+
self.default = options.delete(:default) || nested_default
|
24
28
|
self.options = options
|
25
29
|
set_index if options.delete(:index)
|
26
30
|
end
|
@@ -33,13 +37,14 @@ module Rrod
|
|
33
37
|
def define
|
34
38
|
define_reader
|
35
39
|
define_writer
|
40
|
+
define_presence
|
36
41
|
apply_validators
|
37
42
|
self
|
38
43
|
end
|
39
44
|
|
40
|
-
def cast(value)
|
45
|
+
def cast(value, instance)
|
41
46
|
caster = type.respond_to?(:rrod_cast) ? type : rrod_caster
|
42
|
-
caster ? caster.rrod_cast(value) : value
|
47
|
+
caster ? caster.rrod_cast(value, instance) : value
|
43
48
|
end
|
44
49
|
|
45
50
|
private
|
@@ -57,6 +62,12 @@ module Rrod
|
|
57
62
|
type.is_a?(Array) and type.first.ancestors.include?(Rrod::Model)
|
58
63
|
end
|
59
64
|
|
65
|
+
def nested_default
|
66
|
+
return unless nested_model?
|
67
|
+
model_class = type.model_class
|
68
|
+
-> { Rrod::Model::Collection.new(self, model_class) }
|
69
|
+
end
|
70
|
+
|
60
71
|
def define_reader
|
61
72
|
model.send :define_method, name, self.class.reader_definition(name)
|
62
73
|
end
|
@@ -64,6 +75,10 @@ module Rrod
|
|
64
75
|
def define_writer
|
65
76
|
model.send :define_method, "#{name}=", self.class.writer_definition(name)
|
66
77
|
end
|
78
|
+
|
79
|
+
def define_presence
|
80
|
+
model.send :define_method, "#{name}?", self.class.presence_definition(name)
|
81
|
+
end
|
67
82
|
|
68
83
|
def set_index
|
69
84
|
@index = Index.new(self)
|
@@ -48,9 +48,7 @@ module Rrod
|
|
48
48
|
# @param [Symbol, String] the key of the attribute to write
|
49
49
|
# @param the value to write to attributes
|
50
50
|
def write_attribute(key, value)
|
51
|
-
@attributes[key.to_s] = self.class.cast_attribute(key, value)
|
52
|
-
attr._parent = self if attr.respond_to?(:_parent=)
|
53
|
-
}
|
51
|
+
@attributes[key.to_s] = self.class.cast_attribute(key, value, self)
|
54
52
|
end
|
55
53
|
alias :[]= :write_attribute
|
56
54
|
|
@@ -2,14 +2,15 @@ module Rrod
|
|
2
2
|
module Model
|
3
3
|
class Collection
|
4
4
|
include Enumerable
|
5
|
-
attr_accessor :model
|
5
|
+
attr_accessor :parent, :model
|
6
6
|
|
7
7
|
# explicitly declare the public interface to the underlying collection
|
8
|
-
COLLECTION_INTERFACE = %w[clear count each length size [] first last]
|
8
|
+
COLLECTION_INTERFACE = %w[clear count each length size [] first last empty?]
|
9
9
|
|
10
10
|
delegate(*COLLECTION_INTERFACE, to: :collection)
|
11
11
|
|
12
|
-
def initialize(model, collection=[])
|
12
|
+
def initialize(parent, model, collection=[])
|
13
|
+
self.parent = parent
|
13
14
|
self.model = model
|
14
15
|
self.collection = collection
|
15
16
|
end
|
@@ -34,7 +35,7 @@ module Rrod
|
|
34
35
|
end
|
35
36
|
|
36
37
|
def push(value)
|
37
|
-
instance = model.rrod_cast(value)
|
38
|
+
instance = model.rrod_cast(value, parent)
|
38
39
|
raise InvalidMemberTypeError.new unless model === instance
|
39
40
|
collection.push(instance)
|
40
41
|
end
|
data/lib/rrod/model/schema.rb
CHANGED
@@ -10,9 +10,9 @@ module Rrod
|
|
10
10
|
attributes[name.to_sym] = Attribute.new(self, name, type, options).define
|
11
11
|
end
|
12
12
|
|
13
|
-
def cast_attribute(key, value)
|
13
|
+
def cast_attribute(key, value, instance)
|
14
14
|
attribute = attributes[key.to_sym]
|
15
|
-
attribute ? attribute.cast(value) : value
|
15
|
+
attribute ? attribute.cast(value, instance) : value
|
16
16
|
end
|
17
17
|
|
18
18
|
def nested_in(parent)
|
@@ -23,11 +23,19 @@ module Rrod
|
|
23
23
|
attributes.any?
|
24
24
|
end
|
25
25
|
|
26
|
-
def rrod_cast(value)
|
27
|
-
return
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
def rrod_cast(value, parent=nil)
|
27
|
+
return if value.nil?
|
28
|
+
|
29
|
+
cast_model = case value
|
30
|
+
when Rrod::Model
|
31
|
+
value
|
32
|
+
when Hash
|
33
|
+
instantiate(nil, value)
|
34
|
+
else
|
35
|
+
raise UncastableObjectError.new("#{value.inspect} cannot be rrod_cast") unless Hash === value
|
36
|
+
end
|
37
|
+
|
38
|
+
cast_model.tap { |m| m._parent = parent }
|
31
39
|
end
|
32
40
|
|
33
41
|
end
|
@@ -13,12 +13,12 @@ module Rrod
|
|
13
13
|
end
|
14
14
|
|
15
15
|
::RSpec.configure do |config|
|
16
|
-
config.before(:each, :integration => true) do
|
16
|
+
config.before(:each, :integration => true) do |example|
|
17
17
|
if Rrod::TestServer.fatal
|
18
18
|
fail "Test server not working: #{Rrod::TestServer.fatal}"
|
19
19
|
end
|
20
20
|
|
21
|
-
if
|
21
|
+
if example.metadata[:test_server] == false
|
22
22
|
Rrod::TestServer.stop
|
23
23
|
else
|
24
24
|
Rrod::TestServer.create unless Rrod::TestServer.exist?
|
@@ -30,12 +30,12 @@ module Rrod
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
config.after(:each, :integration => true) do
|
33
|
+
config.after(:each, :integration => true) do |example|
|
34
34
|
# i really don't understand this...
|
35
35
|
# well the 'example' needs to be accessed another way, Rspec.current_example.
|
36
36
|
if !Rrod::TestServer.fatal &&
|
37
37
|
Rrod::TestServer.started? &&
|
38
|
-
|
38
|
+
example.metadata[:test_server] != false
|
39
39
|
Rrod::TestServer.drop
|
40
40
|
end
|
41
41
|
end
|
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.4'
|
data/rrod.gemspec
CHANGED
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.add_dependency "pry", "~> 0.9"
|
29
29
|
|
30
30
|
spec.add_development_dependency "bundler", "~> 1.3"
|
31
|
-
spec.add_development_dependency "rspec", "~>
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.1.0"
|
32
32
|
|
33
33
|
spec.add_development_dependency "rake"
|
34
34
|
end
|
@@ -73,12 +73,18 @@ describe Rrod::Model::Attribute do
|
|
73
73
|
expect(instance).to respond_to("#{name}=")
|
74
74
|
end
|
75
75
|
end
|
76
|
+
|
77
|
+
describe "presence" do
|
78
|
+
it "checks for presence" do
|
79
|
+
expect(instance).to respond_to("#{name}?")
|
80
|
+
end
|
81
|
+
end
|
76
82
|
end
|
77
83
|
|
78
84
|
describe "casting" do
|
79
85
|
it "will use the types rrod_cast method if available" do
|
80
86
|
type = Class.new {
|
81
|
-
def self.rrod_cast(value)
|
87
|
+
def self.rrod_cast(value, instance)
|
82
88
|
value.to_s.upcase
|
83
89
|
end
|
84
90
|
}
|
@@ -2,8 +2,12 @@ require 'spec_helper'
|
|
2
2
|
require 'support/models/person'
|
3
3
|
|
4
4
|
describe Rrod::Model::Collection do
|
5
|
+
let(:owner) { Person.new }
|
5
6
|
let(:array) { [Pet.new] }
|
6
|
-
let(:collection) {
|
7
|
+
let(:collection) {
|
8
|
+
owner.pets = array
|
9
|
+
owner.pets
|
10
|
+
}
|
7
11
|
|
8
12
|
describe "initialization" do
|
9
13
|
it "takes a collection" do
|
@@ -11,7 +15,7 @@ describe Rrod::Model::Collection do
|
|
11
15
|
end
|
12
16
|
|
13
17
|
it "defaults to an empty collection" do
|
14
|
-
expect(described_class.new(Pet).collection).to be_an Array
|
18
|
+
expect(described_class.new(owner, Pet).collection).to be_an Array
|
15
19
|
end
|
16
20
|
|
17
21
|
describe "#build" do
|
@@ -25,6 +29,11 @@ describe Rrod::Model::Collection do
|
|
25
29
|
object = collection.build(name: 'Molle')
|
26
30
|
expect(object).to be_a(Pet)
|
27
31
|
end
|
32
|
+
|
33
|
+
it "assigns the _parent to the built object" do
|
34
|
+
pet = collection.build(name: 'Molle')
|
35
|
+
expect(pet.owner).to eq(owner)
|
36
|
+
end
|
28
37
|
end
|
29
38
|
|
30
39
|
|
@@ -43,7 +52,7 @@ describe Rrod::Model::Collection do
|
|
43
52
|
end
|
44
53
|
|
45
54
|
it "clears the collection" do
|
46
|
-
collection = described_class.new(Pet)
|
55
|
+
collection = described_class.new(owner, Pet)
|
47
56
|
begin; collection.collection = array; rescue; end
|
48
57
|
expect(collection.collection).to be_empty
|
49
58
|
end
|
@@ -39,5 +39,18 @@ describe Rrod::Model::Persistence, integration: true do
|
|
39
39
|
expect(instance.id).not_to be_nil
|
40
40
|
end
|
41
41
|
end
|
42
|
+
|
43
|
+
describe "update models" do
|
44
|
+
before :each do
|
45
|
+
instance.save
|
46
|
+
end
|
47
|
+
|
48
|
+
it "updates attributes" do
|
49
|
+
instance.update wheels: 6
|
50
|
+
expect(instance.wheels).to eq 6
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
end
|
42
55
|
end
|
43
56
|
end
|
@@ -64,6 +64,14 @@ describe Rrod::Model::Schema do
|
|
64
64
|
expect(instance.pets.first.owner).to eq(instance)
|
65
65
|
end
|
66
66
|
|
67
|
+
it "returns an empty collection when referencing an empty association" do
|
68
|
+
expect(instance.pets).to be_empty
|
69
|
+
end
|
70
|
+
|
71
|
+
it "allows building a nested model on an empty collection" do
|
72
|
+
expect { instance.pets.build(name: 'Molle') }.not_to raise_error
|
73
|
+
end
|
74
|
+
|
67
75
|
end
|
68
76
|
|
69
77
|
it "raises an UncastableObjectError if object is not castable" 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.4
|
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-01-
|
11
|
+
date: 2015-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: riak-client
|
@@ -114,14 +114,14 @@ dependencies:
|
|
114
114
|
requirements:
|
115
115
|
- - "~>"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version:
|
117
|
+
version: 3.1.0
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
122
|
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version:
|
124
|
+
version: 3.1.0
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: rake
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|