poro_repository 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,18 +34,15 @@ class PoroRepository
34
34
  end
35
35
  end
36
36
 
37
- def load_record type, id
37
+ def load_record type, id, options={}
38
+ should_remember_record = options.has_key?(:remember) ? options[:remember] : @remember
39
+ should_load_boundary_records = options.has_key?(:load_boundary_records) ? options[:load_boundary_records] : true
38
40
  record = previous_instantiated type, id
39
41
  return record unless record.nil?
40
42
  data = read_if_exists(record_path(type, id))
41
43
  data && deserialise(data).tap do |record|
42
- record.instance_variables.each do |inst_var|
43
- if (token = record.instance_variable_get(inst_var)).is_a? BoundaryToken
44
- object = load_record token.original_type, token.original_id
45
- record.instance_variable_set inst_var, object
46
- end
47
- end
48
- remember_record record if @remember
44
+ load_boundary_records record, options if should_load_boundary_records
45
+ remember_record record if should_remember_record
49
46
  end
50
47
  end
51
48
 
@@ -72,14 +69,32 @@ class PoroRepository
72
69
  forget_record record if @remember
73
70
  end
74
71
 
75
- def load_all type
72
+ def load_all type, load_record_options={}
76
73
  all_ids_for_type(type).collect do |id|
77
- load_record type, id
74
+ load_record type, id, load_record_options
75
+ end
76
+ end
77
+
78
+ # @param type [String]
79
+ # @param field [Symbol]
80
+ # @return [Array]
81
+ def pluck type, field
82
+ load_all(type, :remember => false, :load_boundary_records => false).collect do |record|
83
+ record.send(field)
78
84
  end
79
85
  end
80
86
 
81
87
  private
82
88
 
89
+ def load_boundary_records record, load_record_options
90
+ record.instance_variables.each do |inst_var|
91
+ if (token = record.instance_variable_get(inst_var)).is_a? BoundaryToken
92
+ object = load_record token.original_type, token.original_id, load_record_options
93
+ record.instance_variable_set inst_var, object
94
+ end
95
+ end
96
+ end
97
+
83
98
  def open_for_write path, &block
84
99
  FileUtils.mkdir_p File.dirname(path)
85
100
  File.open path, 'w', &block
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "poro_repository"
6
- s.version = "0.1.2"
6
+ s.version = "0.2.0"
7
7
  s.authors = ["Joel Plane"]
8
8
  s.email = ["joel.plane@gmail.com"]
9
9
  s.homepage = "https://github.com/joelplane/poro_repository"
@@ -225,4 +225,50 @@ describe PoroRepository do
225
225
 
226
226
  end
227
227
 
228
+ describe "#pluck" do
229
+
230
+ def new_repo_instance
231
+ PoroRepository.new("/tmp/test-poro-repository").tap do |repo|
232
+ repo.boundary :Invoice, :@contact
233
+ end
234
+ end
235
+
236
+ subject do
237
+ new_repo_instance
238
+ end
239
+
240
+ let(:contact) do
241
+ TestObject.new.tap do |o|
242
+ o.id = '234'
243
+ o.type = 'Contact'
244
+ o.name = 'John Smith'
245
+ end
246
+ end
247
+
248
+ let(:invoice) do
249
+ TestObject.new.tap do |o|
250
+ o.id = '345'
251
+ o.type = 'Invoice'
252
+ o.contact = contact
253
+ end
254
+ end
255
+
256
+ # note: will not work if the specified field is a boundary.
257
+ it "extracts just the specified field" do
258
+ record_id = subject.save_record invoice
259
+ # Using a new instance of repo so we can call #previous_instantiated()
260
+ # to confirm that the contact record wasn't loaded along with the invoice.
261
+ # We must use a new repo instance to do this, since the contact record
262
+ # is already in memory in the first repo instance since that instance was
263
+ # used to save the Contact in the first place.
264
+ repo = new_repo_instance
265
+ repo.pluck('Invoice', :id).should == ['345']
266
+ # it shouldn't instantiate boundary objects
267
+ repo.send(:previous_instantiated, 'Contact', '234').should be_nil
268
+ # it shouldn't remember the records it instantiated, since they will be
269
+ # imcomplete without having loaded boundary objects.
270
+ repo.send(:previous_instantiated, 'Invoice', '345').should be_nil
271
+ end
272
+ end
273
+
228
274
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poro_repository
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-20 00:00:00.000000000 Z
12
+ date: 2013-11-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec