depository 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ require 'forwardable'
3
3
 
4
4
  require 'depository/sequel'
5
5
  require 'depository/collection_config'
6
+ require 'depository/serialize'
6
7
 
7
8
  module Depository
8
9
  RecordNotFound = Class.new(RuntimeError)
@@ -31,19 +32,13 @@ module Depository
31
32
  end
32
33
 
33
34
  def save(model)
34
- if model.send(config.primary_key)
35
- model.updated_at = Time.now if model.respond_to?(:updated_at=)
35
+ pkey = config.primary_key
36
36
 
37
- db.where(config.primary_key => model.send(config.primary_key)).
38
- update(pack(model.to_hash))
37
+ if new?(model)
38
+ model.send(:"#{pkey}=", db.insert(Serialize.pack(config, model)))
39
39
  else
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)))
40
+ db.where(pkey => model.send(pkey)).
41
+ update(Serialize.pack(config, model))
47
42
  end
48
43
 
49
44
  return model
@@ -79,20 +74,16 @@ module Depository
79
74
  when Array
80
75
  attrs.map(&method(:convert))
81
76
  when Hash
82
- config.model.new(unpack(attrs))
77
+ config.model.new(Serialize.unpack(config, attrs))
83
78
  else
84
79
  raise UnknownConversionType, "unable to convert #{attrs.inspect}"
85
80
  end
86
81
  end
87
82
 
88
- def pack(attrs)
89
- config.packer.call(attrs)
90
- attrs
91
- end
83
+ private
92
84
 
93
- def unpack(attrs)
94
- config.unpacker.call(attrs)
95
- attrs
85
+ def new?(model)
86
+ model.send(config.primary_key).nil?
96
87
  end
97
88
  end
98
89
  end
@@ -0,0 +1,67 @@
1
+ module Depository
2
+ module Serialize
3
+ def self.pack(*args)
4
+ Packer.new(*args).call
5
+ end
6
+
7
+ def self.unpack(*args)
8
+ Unpacker.new(*args).call
9
+ end
10
+
11
+ class Packer
12
+ attr_protected :config, :model
13
+
14
+ def initialize(config, model)
15
+ self.config = config
16
+ self.model = model
17
+ end
18
+
19
+ def call
20
+ attrs = normalize(model.to_hash)
21
+
22
+ config.packer.call(attrs)
23
+ set_timestamps(attrs, model)
24
+
25
+ model.set(Serialize.unpack(config, attrs.dup))
26
+ attrs
27
+ end
28
+
29
+ private
30
+
31
+ def set_timestamps(attrs, model)
32
+ now = Time.now
33
+
34
+ attrs[:updated_at] = now if model.respond_to?(:updated_at=)
35
+ attrs[:created_at] = now if model.respond_to?(:created_at=) &&
36
+ !model.send(config.primary_key)
37
+ end
38
+
39
+ def normalize(attrs)
40
+ attrs = attrs.dup
41
+
42
+ Depository::Database.db.schema(config.db).reject { |column, opts|
43
+ attrs[column].nil?
44
+ }.each do |column, opts|
45
+ attrs[column] = attrs[column].to_i if opts[:type] == :integer
46
+ end
47
+
48
+ attrs
49
+ end
50
+ end
51
+
52
+ class Unpacker
53
+ attr_protected :config, :attrs
54
+
55
+ def initialize(config, attrs)
56
+ self.config = config
57
+ self.attrs = attrs
58
+ end
59
+
60
+ def call
61
+ config.unpacker.call(attrs)
62
+ attrs
63
+ end
64
+ end
65
+
66
+ end
67
+ end
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.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -111,6 +111,7 @@ files:
111
111
  - lib/depository/model.rb
112
112
  - lib/depository/result.rb
113
113
  - lib/depository/sequel.rb
114
+ - lib/depository/serialize.rb
114
115
  homepage: https://github.com/pratt121/depository
115
116
  licenses:
116
117
  - WTFPL