data_broker 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b0175ebf0ddbaaa2239700fd8788b63cc94e88d
4
- data.tar.gz: 07e61c9c970e8973e4e7a19e3e25433f34080744
3
+ metadata.gz: d3e930d1eecce8f3489041add31efa05f55fe37f
4
+ data.tar.gz: 6498d2b77f56db76ee2e8c2877ec7fe2a00726c9
5
5
  SHA512:
6
- metadata.gz: 13702b8bbb4250440aaa12ffa59ce5fc043dfeefce8bf0b296f4d1f90d798ad88bdb2f4e0cee1dce2159606fd35dee49da153e307289db1f30894181f2f3da5a
7
- data.tar.gz: 5222376d572602c9b9fe73a8e71d59129c2ee91ba4993dd0e17351b3db7123142351c99ddeb14cca5be6ae5b512839333f501563d66fcb9facc672f7d1d5a4c1
6
+ metadata.gz: 4dbeaa7a41acd547750e23dc2bb82265c147be4910915fee5761e5b4b34010ef68c1cde031c8465118f6914504e78b630e9546549bdb44cce3199fcafeb18bc8
7
+ data.tar.gz: 2d762759043a9a4b27d0dd97122c99c0275ff719861dc34ad7e18febda376bd1be31791f0780da4d19fdafe42173fcc9cc04b7f78a9931da0a83f77936773a71
@@ -13,28 +13,57 @@ module DataBroker
13
13
  def self.included(descendant)
14
14
  super
15
15
  descendant.extend(ClassMethods)
16
+ descendant.send(:include, InstanceMethods)
16
17
  end
17
18
 
18
19
  module ClassMethods
19
- attr_accessor :mapping, :children, :parents
20
+ attr_accessor :mapping, :children, :parents, :record, :model
20
21
 
21
- def children
22
- @children || {}
22
+ def record
23
+ construct_instance.record
23
24
  end
24
25
 
25
- def parents
26
- @parents || {}
26
+ def model
27
+ construct_instance.model
27
28
  end
28
29
 
29
- [:record, :model].each do |attr|
30
- ivar = :"@#{attr}"
31
- define_method(:"#{attr}=") do |value|
32
- instance_variable_set(ivar, value)
33
- end
30
+ def all(eager_load: false)
31
+ construct_instance.all(:eager_load => eager_load)
32
+ end
34
33
 
35
- define_method(:"#{attr}") do
36
- instance_variable_get(ivar) || infer(attr)
37
- end
34
+ def find(id, eager_load: true)
35
+ construct_instance.find(id, :eager_load => eager_load)
36
+ end
37
+
38
+ def where(eager_load: true, **conds)
39
+ construct_instance.where(:eager_load => true, **conds)
40
+ end
41
+
42
+ def save(model, eager_load: true)
43
+ construct_instance.save(model, :eager_load => eager_load)
44
+ end
45
+
46
+ def destroy(model, eager_load: false)
47
+ construct_instance.destroy(model, :eager_load => eager_load)
48
+ end
49
+
50
+ def construct_instance
51
+ self.new(
52
+ :mapping => mapping, :parents => parents, :children => children,
53
+ :record => @record, :model => @model
54
+ )
55
+ end
56
+ end
57
+
58
+ module InstanceMethods
59
+ attr_reader :mapping, :parents, :children, :record, :model
60
+
61
+ def initialize(mapping:, parents:, children:, record: nil, model: nil)
62
+ self.mapping = mapping
63
+ self.parents = parents || {}
64
+ self.children = children || {}
65
+ self.record = record || infer(:record)
66
+ self.model = model || infer(:model)
38
67
  end
39
68
 
40
69
  def all(eager_load: false)
@@ -100,6 +129,8 @@ module DataBroker
100
129
 
101
130
  private
102
131
 
132
+ attr_writer :mapping, :parents, :children, :record, :model
133
+
103
134
  def load_parents!(obj, record)
104
135
  self.parents.each do |model_ref, record_ref|
105
136
  parent_record = record.send(record_ref)
@@ -114,7 +145,7 @@ module DataBroker
114
145
  @load_parent ||= {}
115
146
  @load_parent[parent_record] ||= -> {
116
147
  mapper = mapper_for(model)
117
- mapper.records_to_objects(parent_record, :eager_load => false).first
148
+ mapper.construct_instance.records_to_objects(parent_record, :eager_load => false).first
118
149
  }.call
119
150
  end
120
151
 
@@ -152,7 +183,7 @@ module DataBroker
152
183
  end
153
184
 
154
185
  def inferred_basename
155
- @inferred_basename ||= self.to_s.underscore.sub(/_mapper$/, "")
186
+ @inferred_basename ||= self.class.to_s.underscore.sub(/_mapper$/, "")
156
187
  .singularize.camelize
157
188
  end
158
189
 
@@ -1,3 +1,3 @@
1
1
  module DataBroker
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
data/spec/mapper_spec.rb CHANGED
@@ -91,8 +91,8 @@ describe DataBroker::Mapper do
91
91
 
92
92
  it "eager loads if requested" do
93
93
  3.times { mapper.record.create }
94
- expect(mapper).to receive(:load_parents!).exactly(3).times
95
- expect(mapper).to receive(:load_children!).once
94
+ expect_any_instance_of(mapper).to receive(:load_parents!).exactly(3).times
95
+ expect_any_instance_of(mapper).to receive(:load_children!).once
96
96
 
97
97
  mapper.all(:eager_load => true)
98
98
  end
@@ -268,7 +268,7 @@ describe DataBroker::Mapper do
268
268
  end
269
269
 
270
270
  it "eager loads if requests" do
271
- expect(mapper).to receive(:load_children!).once
271
+ expect_any_instance_of(mapper).to receive(:load_children!).once
272
272
  model = mapper.save(mapper.model.new)
273
273
  mapper.destroy(model, :eager_load => true)
274
274
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_broker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Emmons
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-16 00:00:00.000000000 Z
11
+ date: 2014-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus