depository 0.1.2 → 0.1.3

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.
@@ -32,10 +32,18 @@ module Depository
32
32
 
33
33
  def save(model)
34
34
  if model.send(config.primary_key)
35
+ model.updated_at = Time.now if model.respond_to?(:updated_at=)
36
+
35
37
  db.where(config.primary_key => model.send(config.primary_key)).
36
- update(model.to_hash)
38
+ update(pack(model.to_hash))
37
39
  else
38
- model.send(:"#{config.primary_key}=", db.insert(model.to_hash))
40
+ time = Time.now
41
+
42
+ [:created_at=, :updated_at=].each do |stamp|
43
+ model.send(stamp, time) if model.respond_to?(stamp)
44
+ end
45
+
46
+ model.send(:"#{config.primary_key}=", db.insert(pack(model.to_hash)))
39
47
  end
40
48
 
41
49
  return model
@@ -71,11 +79,21 @@ module Depository
71
79
  when Array
72
80
  attrs.map(&method(:convert))
73
81
  when Hash
74
- config.model.new(attrs)
82
+ config.model.new(unpack(attrs))
75
83
  else
76
84
  raise UnknownConversionType, "unable to convert #{attrs.inspect}"
77
85
  end
78
86
  end
87
+
88
+ def pack(attrs)
89
+ config.packer.call(attrs)
90
+ attrs
91
+ end
92
+
93
+ def unpack(attrs)
94
+ config.unpacker.call(attrs)
95
+ attrs
96
+ end
79
97
  end
80
98
  end
81
99
  end
@@ -4,6 +4,7 @@ require 'depository/database'
4
4
  module Depository
5
5
  class CollectionConfig
6
6
  attr_protected :_model_block
7
+ attr_writer :packer, :unpacker
7
8
 
8
9
  def model(&block)
9
10
  if block_given?
@@ -13,6 +14,14 @@ module Depository
13
14
  end
14
15
  end
15
16
 
17
+ def packer
18
+ @packer ||= identity
19
+ end
20
+
21
+ def unpacker
22
+ @unpacker ||= identity
23
+ end
24
+
16
25
  def db(&block)
17
26
  if block_given?
18
27
  @_db_block = block
@@ -30,5 +39,11 @@ module Depository
30
39
  @_primary_key ||= :id
31
40
  end
32
41
  end
42
+
43
+ private
44
+
45
+ def identity
46
+ ->(i) { i }
47
+ end
33
48
  end
34
49
  end
@@ -47,6 +47,10 @@ module Depository
47
47
  end
48
48
  alias_method :eql?, :==
49
49
 
50
+ def inspect
51
+ all.inspect
52
+ end
53
+
50
54
  private
51
55
 
52
56
  def new(db)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: depository
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
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: 2014-03-11 00:00:00.000000000 Z
12
+ date: 2014-03-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sequel