depository 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,61 +2,68 @@ require 'attr_protected'
2
2
  require 'forwardable'
3
3
 
4
4
  require 'depository/sequel'
5
+ require 'depository/collection_config'
5
6
 
6
7
  module Depository
7
- RecordNotFound = Class.new(StandardError)
8
- UnknownConversionType = Class.new(StandardError)
8
+ RecordNotFound = Class.new(RuntimeError)
9
+ UnknownConversionType = Class.new(RuntimeError)
9
10
 
10
11
  class Collection
11
12
  class << self
12
- attr_protected :_model_block, :_model_class, :_db_name
13
-
14
13
  extend Forwardable
15
14
 
16
15
  def_delegators :db, *DatasetMethods
17
16
 
18
- def use_model(&block)
19
- self._model_block = block
20
- end
21
-
22
- def _model
23
- self._model_class ||= _model_block.call
24
- end
25
-
26
- def use_db(db)
27
- db.is_a?(Symbol) ? self._db_name = db : @_db = Result.new(db, self)
17
+ def config
18
+ @_config ||= CollectionConfig.new
28
19
  end
29
20
 
30
21
  def db
31
- @_db ||= Result.new(Database[_db_name], self)
22
+ Result.new(config.db, self)
32
23
  end
33
24
 
34
- def primary_key
35
- @_primary_key ||= :id
25
+ def scope(name, blk)
26
+ define_singleton_method(name) { |*args| instance_exec *args, &blk }
36
27
  end
37
28
 
38
- def primary_key=(key)
39
- @_primary_key = key
29
+ def find_by(key, value)
30
+ where(key => value).first
40
31
  end
41
32
 
42
33
  def save(model)
43
- if model.send(primary_key)
44
- update(model.to_hash)
34
+ if model.send(config.primary_key)
35
+ db.where(config.primary_key => model.send(config.primary_key)).
36
+ update(model.to_hash)
45
37
  else
46
- model.send(:"#{primary_key}=", insert(model.to_hash))
47
- return model
38
+ model.send(:"#{config.primary_key}=", db.insert(model.to_hash))
48
39
  end
40
+
41
+ return model
42
+ end
43
+
44
+ def create(attrs)
45
+ save(config.model.new(attrs))
46
+ end
47
+
48
+ def update(*args, &blk)
49
+ model = find(*args)
50
+
51
+ blk.call(model)
52
+ save(model)
53
+ model
49
54
  end
50
55
 
51
56
  def find(key)
52
- result = where(primary_key => key).first
53
- return result if !result.nil?
57
+ result = key.nil? ? nil : where(config.primary_key => key).first
58
+
54
59
  raise RecordNotFound,
55
- "no record found in collection `#{_db_name}' with id `#{key}'"
60
+ "no record found in collection with id `#{key.inspect}'" if result.nil?
61
+
62
+ return result
56
63
  end
57
64
 
58
65
  def delete(key)
59
- db.where(primary_key => key).delete
66
+ db.where(config.primary_key => key).delete
60
67
  end
61
68
 
62
69
  def convert(attrs)
@@ -64,7 +71,7 @@ module Depository
64
71
  when Array
65
72
  attrs.map(&method(:convert))
66
73
  when Hash
67
- _model.new(attrs)
74
+ config.model.new(attrs)
68
75
  else
69
76
  raise UnknownConversionType, "unable to convert #{attrs.inspect}"
70
77
  end
@@ -0,0 +1,34 @@
1
+ require 'attr_protected'
2
+ require 'depository/database'
3
+
4
+ module Depository
5
+ class CollectionConfig
6
+ attr_protected :_model_block
7
+
8
+ def model(&block)
9
+ if block_given?
10
+ self._model_block = block
11
+ else
12
+ @_model ||= _model_block.call
13
+ end
14
+ end
15
+
16
+ def db(&block)
17
+ if block_given?
18
+ @_db_block = block
19
+ @_db = nil
20
+ else
21
+ value = @_db_block.call
22
+ @_db ||= value.is_a?(Symbol) ? Database[value] : value
23
+ end
24
+ end
25
+
26
+ def primary_key(value = nil)
27
+ if value
28
+ @_primary_key = value
29
+ else
30
+ @_primary_key ||= :id
31
+ end
32
+ end
33
+ end
34
+ end
@@ -11,10 +11,6 @@ module Depository
11
11
  self.db = db
12
12
  end
13
13
 
14
- def db_for(collection)
15
- Result.new(self.db[collection._db_name], collection)
16
- end
17
-
18
14
  def [](name)
19
15
  self.db[name]
20
16
  end
@@ -23,7 +23,7 @@ module Depository
23
23
  end
24
24
  end
25
25
 
26
- def initialize(attrs)
26
+ def initialize(attrs = {})
27
27
  set(attrs)
28
28
  _defaults.each { |key, val| set(key, val) if !get(key) }
29
29
  end
@@ -31,7 +31,7 @@ module Depository
31
31
  def to_hash
32
32
  _fields.reduce({}) do |acc, field|
33
33
  acc.merge(field => get(field))
34
- end.reject { |key, val| val.nil? }
34
+ end
35
35
  end
36
36
 
37
37
  def get(attr)
@@ -44,13 +44,15 @@ module Depository
44
44
  else
45
45
  public_send(:"#{attr}=", value)
46
46
  end
47
+
48
+ return self
47
49
  end
48
50
 
49
51
  def ==(other)
50
52
  other.is_a?(self.class) && other.to_hash == to_hash
51
53
  end
52
54
 
53
- private
55
+ private
54
56
 
55
57
  def _fields
56
58
  self.class._fields
@@ -43,7 +43,7 @@ module Depository
43
43
  end
44
44
 
45
45
  def ==(other)
46
- all == other
46
+ self.all == other
47
47
  end
48
48
  alias_method :eql?, :==
49
49
 
metadata CHANGED
@@ -1,18 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: depository
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Brian Pratt
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-03-06 00:00:00.000000000 Z
12
+ date: 2014-03-11 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: sequel
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - ~>
18
20
  - !ruby/object:Gem::Version
@@ -23,6 +25,7 @@ dependencies:
23
25
  type: :runtime
24
26
  prerelease: false
25
27
  version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
26
29
  requirements:
27
30
  - - ~>
28
31
  - !ruby/object:Gem::Version
@@ -33,6 +36,7 @@ dependencies:
33
36
  - !ruby/object:Gem::Dependency
34
37
  name: attr_protected
35
38
  requirement: !ruby/object:Gem::Requirement
39
+ none: false
36
40
  requirements:
37
41
  - - ~>
38
42
  - !ruby/object:Gem::Version
@@ -43,6 +47,7 @@ dependencies:
43
47
  type: :runtime
44
48
  prerelease: false
45
49
  version_requirements: !ruby/object:Gem::Requirement
50
+ none: false
46
51
  requirements:
47
52
  - - ~>
48
53
  - !ruby/object:Gem::Version
@@ -53,6 +58,7 @@ dependencies:
53
58
  - !ruby/object:Gem::Dependency
54
59
  name: rake
55
60
  requirement: !ruby/object:Gem::Requirement
61
+ none: false
56
62
  requirements:
57
63
  - - ~>
58
64
  - !ruby/object:Gem::Version
@@ -63,6 +69,7 @@ dependencies:
63
69
  type: :development
64
70
  prerelease: false
65
71
  version_requirements: !ruby/object:Gem::Requirement
72
+ none: false
66
73
  requirements:
67
74
  - - ~>
68
75
  - !ruby/object:Gem::Version
@@ -73,6 +80,7 @@ dependencies:
73
80
  - !ruby/object:Gem::Dependency
74
81
  name: rspec
75
82
  requirement: !ruby/object:Gem::Requirement
83
+ none: false
76
84
  requirements:
77
85
  - - ~>
78
86
  - !ruby/object:Gem::Version
@@ -83,6 +91,7 @@ dependencies:
83
91
  type: :development
84
92
  prerelease: false
85
93
  version_requirements: !ruby/object:Gem::Requirement
94
+ none: false
86
95
  requirements:
87
96
  - - ~>
88
97
  - !ruby/object:Gem::Version
@@ -97,6 +106,7 @@ extensions: []
97
106
  extra_rdoc_files: []
98
107
  files:
99
108
  - lib/depository/collection.rb
109
+ - lib/depository/collection_config.rb
100
110
  - lib/depository/database.rb
101
111
  - lib/depository/model.rb
102
112
  - lib/depository/result.rb
@@ -104,25 +114,26 @@ files:
104
114
  homepage: https://github.com/pratt121/depository
105
115
  licenses:
106
116
  - WTFPL
107
- metadata: {}
108
117
  post_install_message:
109
118
  rdoc_options: []
110
119
  require_paths:
111
120
  - lib
112
121
  required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
113
123
  requirements:
114
124
  - - ! '>='
115
125
  - !ruby/object:Gem::Version
116
126
  version: '0'
117
127
  required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
118
129
  requirements:
119
130
  - - ! '>='
120
131
  - !ruby/object:Gem::Version
121
132
  version: '0'
122
133
  requirements: []
123
134
  rubyforge_project:
124
- rubygems_version: 2.2.1
135
+ rubygems_version: 1.8.23.2
125
136
  signing_key:
126
- specification_version: 4
137
+ specification_version: 3
127
138
  summary: simple interface for persistence built on Sequel
128
139
  test_files: []
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- Nzc3Y2Q3NzJmZDAxMjI4N2RmZmRlYjJlOGQ3NjVkZmZjNGE2ZjFjZA==
5
- data.tar.gz: !binary |-
6
- ZmM5M2QxMGM3NTdlY2NiMDg4N2EzN2FjOTVhNmUxNjZlYTIyY2Q3OQ==
7
- SHA512:
8
- metadata.gz: !binary |-
9
- YWQ5MjZjYTdkMGZjYTk0MjY0MGMwOWFlOTBjMTVmNTIyOTg0ZmRjODIwNzky
10
- YWI1ODBiMzk3MGNjYWJjMmNiNDQ3ODRjYjBhMDkxNDM1MjQ4NTVjOWU3MGIw
11
- MTIyYjAzMjFiNzc4NGYxZmE3MDcwOTMwZGI4YTQ1NzM1ODg5MzM=
12
- data.tar.gz: !binary |-
13
- MjljZDc0MGI0OWJlMGUxM2U1MTJiYWY1MGMwZDkxYzE5NzExOWFkOTcyYWUz
14
- NTU0MTdlODcwNWM1MmM0NTQ5ZGJiODA2ODc3NzdhZTljNmFjMTdlYTU5YWI3
15
- MWMzZmYwOGVlYTc5MDE5MDM1ODIwMGE0ZWUyZDE2ZmQzOTNjZGM=