depository 0.1.5 → 0.1.6

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.
@@ -11,6 +11,7 @@ module Depository
11
11
  attr_protected :db, :collection
12
12
 
13
13
  def_delegators :db, *[:update, :insert, :delete, :max, :min, :count]
14
+ def_delegators :all, :inspect, :empty?
14
15
 
15
16
  def initialize(db, collection)
16
17
  self.db = db
@@ -39,12 +39,16 @@ module Depository
39
39
  def normalize(attrs)
40
40
  attrs = attrs.dup
41
41
 
42
- Depository::Database.db.schema(config.db).reject { |column, opts|
43
- attrs[column].nil? || opts[:type] != :integer
42
+ columns.select { |column, opts|
43
+ !attrs[column].nil? && opts[:type] == :integer
44
44
  }.each { |column, opts| attrs[column] = attrs[column].to_i }
45
45
 
46
46
  attrs
47
47
  end
48
+
49
+ def columns
50
+ Depository::Database.db.schema(config.db)
51
+ end
48
52
  end
49
53
 
50
54
  class Unpacker
@@ -56,9 +60,37 @@ module Depository
56
60
  end
57
61
 
58
62
  def call
63
+ columns.select { |column, opts|
64
+ opts[:type] == :datetime && ![nil, 0].include?(attrs[column])
65
+ }.each { |column, opts| attrs[column] = drop_usecs(attrs[column]) }
66
+
67
+ columns.select { |column, opts|
68
+ opts[:type] == :date && ![nil, 0].include?(attrs[column])
69
+ }.each { |column, opts|
70
+ attrs[column] = Date.parse(attrs[column]) if attrs[column].is_a?(String)
71
+ }
72
+
73
+ columns.select { |column, opts|
74
+ !attrs[column].nil? && opts[:type] == :boolean
75
+ }.each { |column, opts| attrs[column] = [1, true].include?(attrs[column]) }
76
+
59
77
  config.unpacker.call(attrs)
60
78
  attrs
61
79
  end
80
+
81
+ def drop_usecs(time)
82
+ if time.is_a?(String)
83
+ Time.at(Time.parse(time).to_i)
84
+ elsif time.is_a?(DateTime)
85
+ Time.at(time.to_time.to_i)
86
+ else
87
+ Time.at(time.to_i)
88
+ end
89
+ end
90
+
91
+ def columns
92
+ Depository::Database.db.schema(config.db)
93
+ end
62
94
  end
63
95
 
64
96
  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.5
4
+ version: 0.1.6
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-12 00:00:00.000000000 Z
12
+ date: 2014-03-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sequel