bank 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dcd4669440a5807e03aa5ced145d885fb80327aa
4
+ data.tar.gz: 4feeba3b5ba82f62ee5298ea925908eca3502887
5
+ SHA512:
6
+ metadata.gz: a4272e7927e06b10a2d2da64f5e71faeda152b084f84a4d6dfa40982f3c46b3661c60cf55def5b69a4ec68622aded7e00841c469de2975256b7d28da3509d13d
7
+ data.tar.gz: edde837f718323d43c472572544218318106f8e220161d85050a74166b6e20e8b2118e5d8a899534146d065d60a4aff33df05b27f8b015b3a4573c46e9f89de4
@@ -9,86 +9,85 @@ module Bank
9
9
  RecordNotFound = Class.new(RuntimeError)
10
10
  UnknownConversionType = Class.new(RuntimeError)
11
11
 
12
- class Collection
13
- class << self
14
- extend Forwardable
12
+ module Collection
13
+ extend Forwardable
15
14
 
16
- def_delegators :db, *DatasetMethods
15
+ def_delegators :db, *DatasetMethods
17
16
 
18
- def config
19
- @_config ||= CollectionConfig.new
20
- end
21
-
22
- def db
23
- Result.new(config.db, self)
24
- end
17
+ def initialize(&block)
18
+ instance_exec &block
19
+ end
25
20
 
26
- def scope(name, blk)
27
- define_singleton_method(name) { |*args| instance_exec *args, &blk }
28
- end
21
+ def config
22
+ @_config ||= CollectionConfig.new
23
+ end
29
24
 
30
- def find_by(key, value)
31
- where(key => value).first
32
- end
25
+ def db
26
+ Result.new(config.db, self)
27
+ end
33
28
 
34
- def save(model)
35
- pkey = config.primary_key
29
+ def find_by(key, value)
30
+ where(key => value).first
31
+ end
36
32
 
37
- if new?(model)
38
- model.send(:"#{pkey}=", db.insert(Serialize.pack(config, model)))
39
- else
40
- db.where(pkey => model.send(pkey)).
41
- update(Serialize.pack(config, model))
42
- end
33
+ def save(model)
34
+ pkey = config.primary_key
43
35
 
44
- return model
36
+ if new?(model)
37
+ model.send(:"#{pkey}=", db.insert(Serialize.pack(config, model)))
38
+ else
39
+ db.where(pkey => model.send(pkey)).
40
+ update(Serialize.pack(config, model))
45
41
  end
46
42
 
47
- def create(attrs)
48
- save(config.model.new(attrs))
49
- end
43
+ return model
44
+ end
50
45
 
51
- def update(*args, &blk)
52
- model = find(*args)
46
+ def create(attrs)
47
+ save(config.model.new(attrs))
48
+ end
53
49
 
54
- blk.call(model)
55
- save(model)
56
- model
57
- end
50
+ def update(*args, &blk)
51
+ model = find(*args)
58
52
 
59
- def find(key)
60
- result = key.nil? ? nil : where(config.primary_key => key).first
53
+ blk.call(model)
54
+ save(model)
55
+ model
56
+ end
61
57
 
62
- raise RecordNotFound,
63
- "no record found in collection with id `#{key.inspect}'" if result.nil?
58
+ def find(key)
59
+ result = key.nil? ? nil : where(config.primary_key => key).first
64
60
 
65
- return result
66
- end
61
+ raise RecordNotFound,
62
+ "no record found in collection with id `#{key.inspect}'" if result.nil?
67
63
 
68
- def delete(key)
69
- db.where(config.primary_key => key).delete
70
- end
64
+ return result
65
+ end
71
66
 
72
- def convert(attrs)
73
- case attrs
74
- when Array
75
- attrs.map(&method(:convert))
76
- when Hash
77
- config.model.new(Serialize.unpack(config, attrs))
78
- else
79
- raise UnknownConversionType, "unable to convert #{attrs.inspect}"
80
- end
81
- end
67
+ def delete(key)
68
+ db.where(config.primary_key => key).delete
69
+ end
82
70
 
83
- def join_as(table, *args, &blk)
84
- select(*config.model._fields.map { |f| :"#{table}__#{f}"}).join(*args, &blk)
71
+ def convert(attrs)
72
+ case attrs
73
+ when Array
74
+ attrs.map(&method(:convert))
75
+ when Hash
76
+ config.model.new(Serialize.unpack(config, attrs))
77
+ else
78
+ raise UnknownConversionType, "unable to convert #{attrs.inspect}"
85
79
  end
80
+ end
86
81
 
87
- private
82
+ def join_select(table, *args, &blk)
83
+ select(*config.model._fields.map { |f| :"#{table}__#{f}"}).
84
+ join(*args, &blk)
85
+ end
88
86
 
89
- def new?(model)
90
- model.send(config.primary_key).nil?
91
- end
87
+ private
88
+
89
+ def new?(model)
90
+ model.send(config.primary_key).nil?
92
91
  end
93
92
  end
94
93
  end
@@ -1,25 +1,24 @@
1
1
  require 'attr_protected'
2
- require 'bank/database'
3
2
 
4
3
  module Bank
5
4
  class CollectionConfig
6
- attr_protected :_model_block
7
- attr_writer :packer, :unpacker
5
+ attr_protected :_model_block, :_packers, :_unpackers
6
+
7
+ IDENTITY = ->(i) { i }
8
8
 
9
9
  def model(&block)
10
- if block_given?
11
- self._model_block = block
12
- else
13
- @_model ||= _model_block.call
14
- end
10
+ block_given? ? self._model_block = block : @_model ||= _model_block.call
15
11
  end
16
12
 
17
- def packer
18
- @packer ||= identity
13
+ def packer(blk) packers << blk end
14
+ def unpacker(blk) unpackers << blk end
15
+
16
+ def packers
17
+ @_packers ||= Bank.config.default_packers
19
18
  end
20
19
 
21
- def unpacker
22
- @unpacker ||= identity
20
+ def unpackers
21
+ @_unpackers ||= Bank.config.default_unpackers
23
22
  end
24
23
 
25
24
  def db(&block)
@@ -28,22 +27,20 @@ module Bank
28
27
  @_db = nil
29
28
  else
30
29
  value = @_db_block.call
31
- @_db ||= value.is_a?(Symbol) ? Database[value] : value
30
+ @_db ||= value.is_a?(Symbol) ? Bank[value] : value
32
31
  end
33
32
  end
34
33
 
35
34
  def primary_key(value = nil)
36
- if value
37
- @_primary_key = value
38
- else
39
- @_primary_key ||= :id
40
- end
35
+ value ? @_primary_key = value : @_primary_key ||= :id
41
36
  end
42
37
 
43
- private
38
+ def columns
39
+ @_columns ||= Bank.db.schema(db)
40
+ end
44
41
 
45
- def identity
46
- ->(i) { i }
42
+ def columns_of_type(type)
43
+ columns.select { |col, opts| opts[:type] == type }
47
44
  end
48
45
  end
49
46
  end
@@ -0,0 +1,19 @@
1
+ module Bank
2
+ class Config
3
+ def default_packers
4
+ [
5
+ Serialize::PackTimeStamps,
6
+ Serialize::PackIntegers,
7
+ Serialize::PackBooleans
8
+ ]
9
+ end
10
+
11
+ def default_unpackers
12
+ [
13
+ Serialize::UnpackTime,
14
+ Serialize::UnpackDate,
15
+ Serialize::UnpackBoolean
16
+ ]
17
+ end
18
+ end
19
+ end
data/lib/bank/model.rb CHANGED
@@ -1,31 +1,35 @@
1
1
  require 'attr_protected'
2
2
 
3
3
  module Bank
4
- class Model
5
- class << self
6
- attr_protected :_fields, :_defaults
4
+ module Model
5
+ class Config
6
+ attr_protected :_fields, :_defaults, :_model
7
7
 
8
- def fields(*fields)
9
- attr_accessor *fields
10
- self._fields = fields
8
+ def initialize(model)
9
+ self._model = model
10
+ self._fields = []
11
+ self._defaults = {}
11
12
  end
12
13
 
13
- def _fields
14
- @_fields ||= []
14
+ def fields(*fields)
15
+ self._fields = fields
16
+ _model.send(:attr_accessor, *fields)
15
17
  end
16
18
 
17
- def defaults(defaults)
19
+ def defaults(defaults = {})
18
20
  self._defaults = defaults
19
21
  end
22
+ end
20
23
 
21
- def _defaults
22
- @_defaults ||= {}
24
+ def self.included(base)
25
+ base.define_singleton_method(:config) do
26
+ @_config ||= Bank::Model::Config.new(self)
23
27
  end
24
28
  end
25
29
 
26
30
  def initialize(attrs = {})
27
31
  set(attrs)
28
- _defaults.each { |key, val| set(key, val) if !get(key) }
32
+ _defaults.reject { |key, _| get(key) }.each { |key, val| set(key, val) }
29
33
  end
30
34
 
31
35
  def to_hash
@@ -52,14 +56,14 @@ module Bank
52
56
  other.is_a?(self.class) && other.to_hash == to_hash
53
57
  end
54
58
 
55
- private
59
+ private
56
60
 
57
61
  def _fields
58
- self.class._fields
62
+ self.class.config._fields
59
63
  end
60
64
 
61
65
  def _defaults
62
- self.class._defaults
66
+ self.class.config._defaults
63
67
  end
64
68
  end
65
69
  end
data/lib/bank/result.rb CHANGED
@@ -11,7 +11,7 @@ module Bank
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
+ def_delegators :all, :inspect, :empty?, :==, :eql?
15
15
 
16
16
  def initialize(db, collection)
17
17
  self.db = db
@@ -43,15 +43,6 @@ module Bank
43
43
  DatasetMethods.include?(method) || super
44
44
  end
45
45
 
46
- def ==(other)
47
- self.all == other
48
- end
49
- alias_method :eql?, :==
50
-
51
- def inspect
52
- all.inspect
53
- end
54
-
55
46
  private
56
47
 
57
48
  def new(db)
data/lib/bank/sequel.rb CHANGED
@@ -1,17 +1,73 @@
1
1
  module Bank
2
2
 
3
- DatasetMethods = [:select, :select_all, :select_append, :select_group,
4
- :select_more, :join, :left_join, :right_join, :full_join,
5
- :natural_join, :natural_left_join, :natural_right_join,
6
- :natural_full_join, :cross_join, :inner_join, :left_outer_join,
7
- :right_outer_join, :full_outer_join, :join_table, :where, :filter,
8
- :exclude, :exclude_where, :and, :or, :grep, :invert, :unfiltered,
9
- :group, :group_by, :group_and_count, :select_group, :ungrouped,
10
- :having, :exclude_having, :invert, :unfiltered, :order, :order_by,
11
- :order_append, :order_prepend, :order_more, :reverse, :reverse_order,
12
- :unordered, :limit, :offset, :unlimited, :union, :intersect, :except,
13
- :for_update, :lock_style, :with, :with_recursive, :clone, :distinct,
14
- :naked, :qualify, :server, :with_sql, :count,
15
- :insert, :update, :delete, :max, :min]
3
+ DatasetMethods = [
4
+ :select,
5
+ :select_all,
6
+ :select_append,
7
+ :select_group,
8
+ :select_more,
9
+ :join,
10
+ :left_join,
11
+ :right_join,
12
+ :full_join,
13
+ :natural_join,
14
+ :natural_left_join,
15
+ :natural_right_join,
16
+ :natural_full_join,
17
+ :cross_join,
18
+ :inner_join,
19
+ :left_outer_join,
20
+ :right_outer_join,
21
+ :full_outer_join,
22
+ :join_table,
23
+ :where,
24
+ :filter,
25
+ :exclude,
26
+ :exclude_where,
27
+ :and,
28
+ :or,
29
+ :grep,
30
+ :invert,
31
+ :unfiltered,
32
+ :group,
33
+ :group_by,
34
+ :group_and_count,
35
+ :select_group,
36
+ :ungrouped,
37
+ :having,
38
+ :exclude_having,
39
+ :invert,
40
+ :unfiltered,
41
+ :order,
42
+ :order_by,
43
+ :order_append,
44
+ :order_prepend,
45
+ :order_more,
46
+ :reverse,
47
+ :reverse_order,
48
+ :unordered,
49
+ :limit,
50
+ :offset,
51
+ :unlimited,
52
+ :union,
53
+ :intersect,
54
+ :except,
55
+ :for_update,
56
+ :lock_style,
57
+ :with,
58
+ :with_recursive,
59
+ :clone,
60
+ :distinct,
61
+ :naked,
62
+ :qualify,
63
+ :server,
64
+ :with_sql,
65
+ :count,
66
+ :insert,
67
+ :update,
68
+ :delete,
69
+ :max,
70
+ :min
71
+ ]
16
72
 
17
73
  end
@@ -1,101 +1,62 @@
1
1
  module Bank
2
2
  module Serialize
3
- def self.pack(*args)
4
- Packer.new(*args).call
5
- end
3
+ def self.pack(config, model)
4
+ attrs = model.to_hash
6
5
 
7
- def self.unpack(*args)
8
- Unpacker.new(*args).call
6
+ config.packers.each { |p| p.call(attrs, config) }
7
+ model.set(Serialize.unpack(config, attrs.dup))
8
+ attrs
9
9
  end
10
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
- columns.select { |column, opts|
43
- !attrs[column].nil? && opts[:type] == :integer
44
- }.each { |column, opts| attrs[column] = attrs[column].to_i }
45
-
46
- columns.select { |column, opts|
47
- !attrs[column].nil? && opts[:type] == :boolean
48
- }.each { |column, opts| attrs[column] = attrs[column] ? 1 : 0 }
49
-
50
- attrs
51
- end
52
-
53
- def columns
54
- @columns ||= Database.db.schema(config.db)
55
- end
11
+ def self.unpack(config, attrs)
12
+ config.unpackers.each {|p| p.call(attrs, config) }
13
+ attrs
56
14
  end
57
15
 
58
- class Unpacker
59
- attr_protected :config, :attrs
60
-
61
- def initialize(config, attrs)
62
- self.config = config
63
- self.attrs = attrs
64
- end
65
-
66
- def call
67
- columns.select { |column, opts|
68
- opts[:type] == :datetime && ![nil, 0].include?(attrs[column])
69
- }.each { |column, opts| attrs[column] = drop_usecs(attrs[column]) }
16
+ PackTimeStamps = ->(attrs, config) do
17
+ now, model = Time.now, config.model.new
70
18
 
71
- columns.select { |column, opts|
72
- opts[:type] == :date && ![nil, 0].include?(attrs[column])
73
- }.each { |column, opts|
74
- attrs[column] = Date.parse(attrs[column]) if attrs[column].is_a?(String)
75
- }
19
+ attrs[:updated_at] = now if model.respond_to?(:updated_at=)
20
+ attrs[:created_at] = now if model.respond_to?(:created_at=) &&
21
+ !model.send(config.primary_key)
22
+ end
76
23
 
77
- columns.select { |column, opts|
78
- !attrs[column].nil? && opts[:type] == :boolean
79
- }.each { |column, opts| attrs[column] = [1, true].include?(attrs[column]) }
24
+ PackIntegers = ->(attrs, config) do
25
+ config.columns_of_type(:integer).each { |column, opts|
26
+ attrs[column] = attrs[column].to_i if attrs[column]
27
+ }
28
+ end
80
29
 
81
- config.unpacker.call(attrs)
82
- attrs
83
- end
30
+ PackBooleans = ->(attrs, config) do
31
+ config.columns_of_type(:boolean).each { |column, opts|
32
+ attrs[column] = attrs[column] ? 1 : 0
33
+ }
34
+ end
84
35
 
85
- def drop_usecs(time)
86
- if time.is_a?(String)
87
- Time.at(Time.parse(time).to_i)
88
- elsif time.is_a?(DateTime)
89
- Time.at(time.to_time.to_i)
90
- else
91
- Time.at(time.to_i)
92
- end
93
- end
36
+ UnpackTime = ->(attrs, config) do
37
+ config.columns_of_type(:datetime).each { |column, opts|
38
+ time = attrs[column]
39
+ attrs[column] = case time
40
+ when String
41
+ Time.at(Time.parse(time).to_i)
42
+ when DateTime
43
+ Time.at(time.to_time.to_i)
44
+ else
45
+ Time.at(time.to_i)
46
+ end
47
+ }
48
+ end
94
49
 
95
- def columns
96
- @columns ||= Database.db.schema(config.db)
97
- end
50
+ UnpackDate = ->(attrs, config) do
51
+ config.columns_of_type(:date).each { |col, _|
52
+ attrs[col] = Date.parse(attrs[col]) if attrs[col].is_a?(String)
53
+ }
98
54
  end
99
55
 
56
+ UnpackBoolean = ->(attrs, config) do
57
+ config.columns.select { |col, opts|
58
+ !attrs[col].nil? && opts[:type] == :boolean
59
+ }.each { |col, opts| attrs[col] = [1, true].include?(attrs[col]) }
60
+ end
100
61
  end
101
62
  end
metadata CHANGED
@@ -1,102 +1,93 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bank
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
5
- prerelease:
4
+ version: 0.1.9
6
5
  platform: ruby
7
6
  authors:
8
7
  - Brian Pratt
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-04-25 00:00:00.000000000 Z
11
+ date: 2015-01-31 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: sequel
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '4.7'
22
- - - ! '>='
20
+ - - ">="
23
21
  - !ruby/object:Gem::Version
24
22
  version: 4.7.0
25
23
  type: :runtime
26
24
  prerelease: false
27
25
  version_requirements: !ruby/object:Gem::Requirement
28
- none: false
29
26
  requirements:
30
- - - ~>
27
+ - - "~>"
31
28
  - !ruby/object:Gem::Version
32
29
  version: '4.7'
33
- - - ! '>='
30
+ - - ">="
34
31
  - !ruby/object:Gem::Version
35
32
  version: 4.7.0
36
33
  - !ruby/object:Gem::Dependency
37
34
  name: attr_protected
38
35
  requirement: !ruby/object:Gem::Requirement
39
- none: false
40
36
  requirements:
41
- - - ~>
37
+ - - "~>"
42
38
  - !ruby/object:Gem::Version
43
39
  version: '1.0'
44
- - - ! '>='
40
+ - - ">="
45
41
  - !ruby/object:Gem::Version
46
42
  version: 1.0.0
47
43
  type: :runtime
48
44
  prerelease: false
49
45
  version_requirements: !ruby/object:Gem::Requirement
50
- none: false
51
46
  requirements:
52
- - - ~>
47
+ - - "~>"
53
48
  - !ruby/object:Gem::Version
54
49
  version: '1.0'
55
- - - ! '>='
50
+ - - ">="
56
51
  - !ruby/object:Gem::Version
57
52
  version: 1.0.0
58
53
  - !ruby/object:Gem::Dependency
59
54
  name: rake
60
55
  requirement: !ruby/object:Gem::Requirement
61
- none: false
62
56
  requirements:
63
- - - ~>
57
+ - - "~>"
64
58
  - !ruby/object:Gem::Version
65
59
  version: '10.1'
66
- - - ! '>='
60
+ - - ">="
67
61
  - !ruby/object:Gem::Version
68
62
  version: 10.1.1
69
63
  type: :development
70
64
  prerelease: false
71
65
  version_requirements: !ruby/object:Gem::Requirement
72
- none: false
73
66
  requirements:
74
- - - ~>
67
+ - - "~>"
75
68
  - !ruby/object:Gem::Version
76
69
  version: '10.1'
77
- - - ! '>='
70
+ - - ">="
78
71
  - !ruby/object:Gem::Version
79
72
  version: 10.1.1
80
73
  - !ruby/object:Gem::Dependency
81
74
  name: rspec
82
75
  requirement: !ruby/object:Gem::Requirement
83
- none: false
84
76
  requirements:
85
- - - ~>
77
+ - - "~>"
86
78
  - !ruby/object:Gem::Version
87
79
  version: '2.14'
88
- - - ! '>='
80
+ - - ">="
89
81
  - !ruby/object:Gem::Version
90
82
  version: 2.14.1
91
83
  type: :development
92
84
  prerelease: false
93
85
  version_requirements: !ruby/object:Gem::Requirement
94
- none: false
95
86
  requirements:
96
- - - ~>
87
+ - - "~>"
97
88
  - !ruby/object:Gem::Version
98
89
  version: '2.14'
99
- - - ! '>='
90
+ - - ">="
100
91
  - !ruby/object:Gem::Version
101
92
  version: 2.14.1
102
93
  description: wraps Sequel to provide a non-ORM data-mapping interface
@@ -107,7 +98,7 @@ extra_rdoc_files: []
107
98
  files:
108
99
  - lib/bank/collection.rb
109
100
  - lib/bank/collection_config.rb
110
- - lib/bank/database.rb
101
+ - lib/bank/config.rb
111
102
  - lib/bank/model.rb
112
103
  - lib/bank/result.rb
113
104
  - lib/bank/sequel.rb
@@ -115,26 +106,25 @@ files:
115
106
  homepage: https://github.com/pratt121/bank
116
107
  licenses:
117
108
  - WTFPL
109
+ metadata: {}
118
110
  post_install_message:
119
111
  rdoc_options: []
120
112
  require_paths:
121
113
  - lib
122
114
  required_ruby_version: !ruby/object:Gem::Requirement
123
- none: false
124
115
  requirements:
125
- - - ! '>='
116
+ - - ">="
126
117
  - !ruby/object:Gem::Version
127
118
  version: '0'
128
119
  required_rubygems_version: !ruby/object:Gem::Requirement
129
- none: false
130
120
  requirements:
131
- - - ! '>='
121
+ - - ">="
132
122
  - !ruby/object:Gem::Version
133
123
  version: '0'
134
124
  requirements: []
135
125
  rubyforge_project:
136
- rubygems_version: 1.8.23.2
126
+ rubygems_version: 2.2.2
137
127
  signing_key:
138
- specification_version: 3
128
+ specification_version: 4
139
129
  summary: simple interface for persistence built on Sequel
140
130
  test_files: []
data/lib/bank/database.rb DELETED
@@ -1,23 +0,0 @@
1
- require 'attr_protected'
2
-
3
- require 'bank/result'
4
-
5
- module Bank
6
- class Database
7
- class << self
8
- attr_protected :db
9
-
10
- def self.db
11
- db.dup
12
- end
13
-
14
- def use_db(db)
15
- self.db = db
16
- end
17
-
18
- def [](name)
19
- self.db[name]
20
- end
21
- end
22
- end
23
- end