depository 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZWUzMGFlMDcyYWIxYzU0NGJhZjk2YWFkNTk4YWZmYTI4ZDJhOTIxYg==
5
+ data.tar.gz: !binary |-
6
+ MDQyMGYxODljMGVlOGIwYjFlNWJiNTVmNjcxZjFiNWUzNjM2YmNmNg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ODU5MTNkNDU3NDYwYzQyYjc0MTFhYTJhMWFlNzU0ZDc4YjE0OWRlYzMxMzZh
10
+ ZGNlODcyMTFkMmEyYmIwNjE5YjBiNzRkMWFkMWU5ZDQ4NjdiZTcyOTBlMDQ3
11
+ ZmQ5NGQ2NjA4ZDQ4NWJhMWI2ODk2NmQ1ZThiNzUxYmJiYzQ4MmQ=
12
+ data.tar.gz: !binary |-
13
+ OTNhNzE3NzhiOGFiODYzMTg0YWZkNjIzNTQ5ODQzYjk4YmU2ZjQxMjQyMjk0
14
+ YTVjZDYzZTM2MGNiNjY0NWFlYzUwYTViNDdkMTVmYzFiOGY4OGVmMzE3MzRk
15
+ ZDQ1OGY0NmM3NWYzOGIxYjcxZDllMjAwMDM5YzM4NzdjNzY3MTk=
@@ -0,0 +1,78 @@
1
+ require 'attr_protected'
2
+ require 'forwardable'
3
+
4
+ require 'depository/sequel'
5
+
6
+ module Depository
7
+ RecordNotFound = Class.new(StandardError)
8
+ UnknownConversionType = Class.new(StandardError)
9
+
10
+ class Collection
11
+ class << self
12
+ attr_protected :_model_block, :_model_class, :_db_name
13
+
14
+ extend Forwardable
15
+
16
+ def_delegators :db, *DatasetMethods
17
+
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_name)
27
+ self._db_name = db_name
28
+ end
29
+
30
+ def db
31
+ Depository::Database.db_for(self)
32
+ end
33
+
34
+ def primary_key
35
+ @_primary_key ||= :id
36
+ end
37
+
38
+ def primary_key=(key)
39
+ @_primary_key = key
40
+ end
41
+
42
+ def save(model)
43
+ if model.send(primary_key)
44
+ update(model.to_hash)
45
+ else
46
+ model.send(:"#{primary_key}=", insert(model.to_hash))
47
+ return model
48
+ end
49
+ end
50
+
51
+ def find(key)
52
+ result = where(primary_key => key).first
53
+ return result if !result.nil?
54
+ raise RecordNotFound,
55
+ "no record found in collection `#{_db_name}' with id `#{key}'"
56
+ end
57
+
58
+ def delete(key)
59
+ db.where(primary_key => key).delete
60
+ end
61
+
62
+ def count
63
+ db.raw.size
64
+ end
65
+
66
+ def convert(attrs)
67
+ case attrs
68
+ when Array
69
+ attrs.map(&method(:convert))
70
+ when Hash
71
+ _model.new(attrs)
72
+ else
73
+ raise UnknownConversionType, "unable to convert #{attrs.inspect}"
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,19 @@
1
+ require 'attr_protected'
2
+
3
+ require 'depository/result'
4
+
5
+ module Depository
6
+ class Database
7
+ class << self
8
+ attr_protected :db
9
+
10
+ def use_db(db)
11
+ self.db = db
12
+ end
13
+
14
+ def db_for(collection)
15
+ Result.new(self.db[collection._db_name], collection)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,63 @@
1
+ require 'attr_protected'
2
+
3
+ module Depository
4
+ class Model
5
+ class << self
6
+ attr_protected :_fields, :_defaults
7
+
8
+ def fields(*fields)
9
+ attr_accessor *fields
10
+ self._fields = fields
11
+ end
12
+
13
+ def _fields
14
+ @_fields ||= []
15
+ end
16
+
17
+ def defaults(defaults)
18
+ self._defaults = defaults
19
+ end
20
+
21
+ def _defaults
22
+ @_defaults ||= {}
23
+ end
24
+ end
25
+
26
+ def initialize(attrs)
27
+ set(attrs)
28
+ _defaults.each { |key, val| set(key, val) if !get(key) }
29
+ end
30
+
31
+ def to_hash
32
+ _fields.reduce({}) do |acc, field|
33
+ acc.merge(field => get(field))
34
+ end.reject { |key, val| val.nil? }
35
+ end
36
+
37
+ def get(attr)
38
+ instance_variable_get(:"@#{attr}")
39
+ end
40
+
41
+ def set(attr, value = nil)
42
+ if attr.is_a?(Hash)
43
+ attr.each { |key, value| set(key, value) }
44
+ else
45
+ public_send(:"#{attr}=", value)
46
+ end
47
+ end
48
+
49
+ def ==(other)
50
+ other.is_a?(self.class) && other.to_hash == to_hash
51
+ end
52
+
53
+ private
54
+
55
+ def _fields
56
+ self.class._fields
57
+ end
58
+
59
+ def _defaults
60
+ self.class._defaults
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,56 @@
1
+ require 'attr_protected'
2
+ require 'forwardable'
3
+
4
+ require 'depository/sequel'
5
+
6
+ module Depository
7
+ class Result
8
+ include Enumerable
9
+ extend Forwardable
10
+
11
+ attr_protected :db, :collection
12
+
13
+ def_delegators :db, *[:update, :insert, :delete, :max, :min]
14
+
15
+ def initialize(db, collection)
16
+ self.db = db
17
+ self.collection = collection
18
+ end
19
+
20
+ def each(&blk)
21
+ raw.each { |result| blk.call(collection.convert(result)) }
22
+ end
23
+
24
+ def raw
25
+ db.all
26
+ end
27
+
28
+ def all
29
+ collection.convert(raw)
30
+ end
31
+
32
+ # db overrides for Enumerable-clashing methods
33
+ def select(*args, &block) new(db.select(*args, &block)) end
34
+ def group_by(*args, &block) new(db.group_by(*args, &block)) end
35
+ def grep(*args, &block) new(db.grep(*args, &block)) end
36
+
37
+ def method_missing(*args, &blk)
38
+ DatasetMethods.include?(args[0]) ? new(db.send(*args, &blk)) : super
39
+ end
40
+
41
+ def respond_to?(method)
42
+ DatasetMethods.include?(method) || super
43
+ end
44
+
45
+ def ==(other)
46
+ all == other
47
+ end
48
+ alias_method :eql?, :==
49
+
50
+ private
51
+
52
+ def new(db)
53
+ self.class.new(db, collection)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,17 @@
1
+ module Depository
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,
15
+ :insert, :update, :delete, :max, :min]
16
+
17
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: depository
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Pratt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.7'
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 4.7.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '4.7'
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 4.7.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: attr_protected
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: '1.0'
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: 1.0.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ~>
48
+ - !ruby/object:Gem::Version
49
+ version: '1.0'
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: 1.0.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: rake
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: '10.1'
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: 10.1.1
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '10.1'
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: 10.1.1
73
+ - !ruby/object:Gem::Dependency
74
+ name: rspec
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ version: '2.14'
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.14.1
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '2.14'
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: 2.14.1
93
+ description: wraps Sequel to provide a non-ORM data-mapping interface
94
+ email: brian@8thlight.com
95
+ executables: []
96
+ extensions: []
97
+ extra_rdoc_files: []
98
+ files:
99
+ - lib/depository/collection.rb
100
+ - lib/depository/database.rb
101
+ - lib/depository/model.rb
102
+ - lib/depository/result.rb
103
+ - lib/depository/sequel.rb
104
+ homepage: http://8thlight.com
105
+ licenses:
106
+ - WTFPL
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: simple interface for persistence built on Sequel
128
+ test_files: []