persisto 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 854eb9c0d54aeae8dcf2bfd02600d7179ffb2a4a
4
+ data.tar.gz: d4a9b75be85a8a1ee46d6457d1ea94a3e133bd9b
5
+ SHA512:
6
+ metadata.gz: 5463555b437aaa7d413af00acc8011c8bd340ec82046fad2ef51970eb0f67e9a0994cbf8fd783daae40dbf841f3d0fa9e2b9abc5e630be575204d4bf90b9f43e
7
+ data.tar.gz: c6fd95a9b10148fd528cf19d21b4f2772bc77559a35f05bf6a77507f6db5f097e3250202e031a79ae21c2c04714cf116e03c3b6c65e21959b2aca5f04c59104c
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in persisto.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :minitest, :all_on_start => false do
5
+ # with Minitest::Spec
6
+ watch(%r{^spec/(.*)_spec\.rb})
7
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
8
+ watch(%r{^spec/spec_helper\.rb}) { 'spec' }
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Erik Lott
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Persisto
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'persisto'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install persisto
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ module Persisto
2
+ class AbstractAdaptor
3
+ def select qo
4
+ raise NotImplementedError
5
+ end
6
+
7
+ def insert qo
8
+ raise NotImplementedError
9
+ end
10
+
11
+ def update qo
12
+ raise NotImplementedError
13
+ end
14
+
15
+ def delete qo
16
+ raise NotImplementedError
17
+ end
18
+
19
+ def transaction &block
20
+ raise NotImplementedError
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,50 @@
1
+ require 'sequel'
2
+ require 'persisto/adaptors/abstract_adaptor'
3
+
4
+ module Persisto
5
+ class Pg < AbstractAdaptor
6
+ def self.connect *args
7
+ new Sequel.connect(*args)
8
+ end
9
+
10
+ attr_reader :database
11
+
12
+ def initialize database
13
+ @database = database
14
+ end
15
+
16
+ def select qo
17
+ all(qo)
18
+ end
19
+
20
+ def insert qo
21
+ single_value(qo)
22
+ end
23
+
24
+ def update qo
25
+ single_value(qo)
26
+ end
27
+
28
+ def delete qo
29
+ single_value(qo)
30
+ end
31
+
32
+ def transaction &block
33
+ database.transaction &block
34
+ end
35
+
36
+ private
37
+
38
+ def all qo
39
+ fetch(qo).all
40
+ end
41
+
42
+ def single_value qo
43
+ fetch(qo).single_value
44
+ end
45
+
46
+ def fetch qo
47
+ database.fetch(qo.query)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,7 @@
1
+ module Persisto
2
+ class PersistoError < StandardError; end
3
+ class StoreError < PersistoError; end
4
+ class RepositoryError < PersistoError; end
5
+ class QueryError < PersistoError; end
6
+ class EntityNotFound < PersistoError; end
7
+ end
@@ -0,0 +1,11 @@
1
+ module Persisto
2
+ module Mapper
3
+ def parse data={}
4
+ raise NotImplementedError
5
+ end
6
+
7
+ def dump_store store_name, entity
8
+ send("dump_#{store_name}", entity)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ require 'sequel'
2
+ require 'persisto/exceptions'
3
+
4
+ module Persisto
5
+ module Query
6
+ attr_reader :store
7
+
8
+ def query
9
+ raise NotImplementedError
10
+ end
11
+
12
+ private
13
+
14
+ def lit *args
15
+ Sequel.lit(*args)
16
+ end
17
+
18
+ def dump *args
19
+ raise(QueryError, 'store not assigned') unless store
20
+ store.dump(*args)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,43 @@
1
+ require 'persisto/exceptions'
2
+
3
+ module Persisto
4
+ module Repository
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def set_mapper val
11
+ @mapper = val
12
+ end
13
+
14
+ def mapper
15
+ @mapper || raise(::Persisto::RepositoryError, 'repository mapper not assigned')
16
+ end
17
+
18
+ def store_registry
19
+ @store_registry ||= {}
20
+ end
21
+
22
+ def register_store store_name, store_class
23
+ store_registry[store_name] = store_class
24
+ define_method(store_name){ store(store_name) }
25
+ end
26
+ end
27
+
28
+ def initialize adaptor
29
+ @adaptor = adaptor
30
+ @stores = {}
31
+ end
32
+
33
+ private
34
+
35
+ def adaptor
36
+ @adaptor
37
+ end
38
+
39
+ def store store_name
40
+ @stores[store_name] ||= self.class.store_registry[store_name].new(adaptor, self.class.mapper)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,74 @@
1
+ require 'persisto/exceptions'
2
+
3
+ module Persisto
4
+ module Store
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def set_table_name val
11
+ @table_name = val
12
+ end
13
+
14
+ def table_name
15
+ @table_name || raise(StoreError, 'table name not assigned')
16
+ end
17
+ end
18
+
19
+ attr_reader :adaptor, :mapper
20
+
21
+ def initialize adaptor, mapper
22
+ @adaptor = adaptor
23
+ @mapper = mapper
24
+ end
25
+
26
+ def all query_object
27
+ assign_to_query_object(query_object)
28
+ parse(adaptor.select(query_object))
29
+ end
30
+
31
+ def first query_object
32
+ assign_to_query_object(query_object)
33
+ parse(adaptor.select(query_object)).first
34
+ end
35
+
36
+ def first! query_object
37
+ assign_to_query_object(query_object)
38
+ parse(adaptor.select(query_object)).first || raise(EntityNotFound, "#{query_object.class.name}: entity not found")
39
+ end
40
+
41
+ def insert query_object
42
+ assign_to_query_object(query_object)
43
+ adaptor.insert(query_object)
44
+ end
45
+
46
+ def update query_object
47
+ assign_to_query_object(query_object)
48
+ adaptor.update(query_object)
49
+ end
50
+
51
+ def delete query_object
52
+ assign_to_query_object(query_object)
53
+ adaptor.delete(query_object)
54
+ end
55
+
56
+ def transaction &block
57
+ adaptor.transaction &block
58
+ end
59
+
60
+ def dump entity
61
+ mapper.dump_store(self.class.table_name, entity)
62
+ end
63
+
64
+ def parse data
65
+ [*data].map{|d| mapper.parse(d)}
66
+ end
67
+
68
+ private
69
+
70
+ def assign_to_query_object query_object
71
+ query_object.store = self
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module Persisto
2
+ VERSION = "0.0.1"
3
+ end
data/lib/persisto.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "persisto/version"
2
+
3
+ module Persisto
4
+ # Your code goes here...
5
+ end
data/persisto.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'persisto/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "persisto"
8
+ spec.version = Persisto::VERSION
9
+ spec.authors = ["Erik Lott"]
10
+ spec.email = ["erik.lott@kodio.com"]
11
+ spec.description = %q{Kodio lightweight data access layer for ruby}
12
+ spec.summary = %q{Kodio lightweight data access layer for ruby}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "sequel", "~> 4.3.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "mocha", "~> 0.14.0"
26
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+ require 'persisto/repository'
3
+
4
+ describe Persisto::Repository do
5
+ let(:mapper) { stub }
6
+ let(:adaptor){ stub }
7
+ let(:repo_class){ Class.new{ include Persisto::Repository } }
8
+ let(:repo){ repo_class.new(adaptor) }
9
+
10
+ before { repo_class.set_mapper(mapper) }
11
+
12
+ describe '::set_mapper' do
13
+ it 'assigns mapper' do
14
+ repo_class.set_mapper('blah')
15
+ repo_class.mapper.must_equal 'blah'
16
+ end
17
+ end
18
+
19
+ describe '::mapper' do
20
+ context 'when mapper assigned' do
21
+ before { repo_class.set_mapper(mapper) }
22
+
23
+ it 'returns mapper' do
24
+ repo_class.mapper.must_equal mapper
25
+ end
26
+ end
27
+
28
+ context 'when mapper not assigned' do
29
+ before { repo_class.set_mapper(nil) }
30
+
31
+ it 'raises error' do
32
+ proc{ repo_class.mapper }.must_raise Persisto::RepositoryError
33
+ end
34
+ end
35
+ end
36
+
37
+ describe '::register_store' do
38
+ let(:store_class){stub}
39
+
40
+ before { repo_class.register_store :mystore, store_class }
41
+
42
+ it 'adds store to registry' do
43
+ repo_class.store_registry.must_equal({:mystore => store_class})
44
+ end
45
+
46
+ it 'adds attr_reader for store' do
47
+ repo.respond_to?(:mystore).must_equal true
48
+ end
49
+ end
50
+
51
+ describe 'store method' do
52
+ context 'when store initialize' do
53
+ it 'returns store' do
54
+ repo.instance_variable_set(:@stores, {:blah => 'blah'})
55
+ repo.send(:store, :blah).must_equal 'blah'
56
+ end
57
+ end
58
+
59
+ context 'when store not initialized' do
60
+ let(:mystore_class){ stub }
61
+ let(:store){ stub }
62
+ before { repo_class.register_store :mystore, mystore_class }
63
+
64
+ it 'initializes store' do
65
+ mystore_class.expects(:new).with(adaptor, mapper).returns(store)
66
+ repo.mystore
67
+ end
68
+
69
+ it 'returns store' do
70
+ mystore_class.expects(:new).with(adaptor, mapper).returns(store)
71
+ repo.mystore.must_equal store
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,6 @@
1
+ require 'minitest/autorun'
2
+ require 'mocha/setup'
3
+
4
+ def context *args, &block
5
+ describe *args, &block
6
+ end
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+ require 'persisto/store'
3
+
4
+ describe Persisto::Store do
5
+ let(:mapper) { stub }
6
+ let(:adaptor){ stub }
7
+ let(:store_class){ Class.new{ include Persisto::Store } }
8
+ let(:store){ store_class.new(adaptor, mapper) }
9
+
10
+ describe '::table_name' do
11
+ let(:table_name) {'blah'}
12
+
13
+ before { store_class.set_table_name(table_name) }
14
+
15
+ it 'returns table name' do
16
+ store_class.table_name.must_equal table_name
17
+ end
18
+ end
19
+
20
+ describe '::set_table_name' do
21
+ it 'sets table name class var' do
22
+ store_class.set_table_name('blah')
23
+ store_class.table_name.must_equal('blah')
24
+ end
25
+ end
26
+
27
+ describe '#all' do
28
+ let(:query_object){stub}
29
+ let(:data){stub}
30
+ let(:entity){stub}
31
+
32
+ it 'returns list of entities' do
33
+ query_object.expects(:store=).with(store)
34
+ adaptor.expects(:select).with(query_object).returns([data])
35
+ mapper.expects(:parse).with(data).returns(entity)
36
+ store.all(query_object).must_equal [entity]
37
+ end
38
+ end
39
+
40
+ describe '#first' do
41
+ let(:query_object){stub}
42
+ let(:data){stub}
43
+ let(:entity){stub}
44
+
45
+ it 'returns first entity' do
46
+ query_object.expects(:store=).with(store)
47
+ adaptor.expects(:select).with(query_object).returns([data])
48
+ mapper.expects(:parse).with(data).returns(entity)
49
+ store.first(query_object).must_equal entity
50
+ end
51
+ end
52
+
53
+ describe '#first!' do
54
+ let(:query_object){stub}
55
+ let(:data){stub}
56
+ let(:entity){stub}
57
+
58
+ context 'when records found in database' do
59
+ it 'returns first entity' do
60
+ query_object.expects(:store=).with(store)
61
+ adaptor.expects(:select).with(query_object).returns([data])
62
+ mapper.expects(:parse).with(data).returns(entity)
63
+ store.first(query_object).must_equal entity
64
+ end
65
+ end
66
+
67
+ context 'when records not found in database' do
68
+ it 'returns first entity' do
69
+ query_object.expects(:store=).with(store)
70
+ adaptor.expects(:select).with(query_object).returns([])
71
+ proc{ store.first!(query_object) }.must_raise(Persisto::EntityNotFound)
72
+ end
73
+ end
74
+ end
75
+
76
+ describe '#insert' do
77
+ let(:query_object){stub}
78
+ let(:primary_key){123}
79
+
80
+ it 'inserts query into adaptor' do
81
+ query_object.expects(:store=).with(store)
82
+ adaptor.expects(:insert).with(query_object).returns(primary_key)
83
+ store.insert(query_object).must_equal primary_key
84
+ end
85
+ end
86
+
87
+ describe '#update' do
88
+ let(:query_object){stub}
89
+ let(:changed_rows){1}
90
+
91
+ it 'updates query into adaptor' do
92
+ query_object.expects(:store=).with(store)
93
+ adaptor.expects(:update).with(query_object).returns(changed_rows)
94
+ store.update(query_object).must_equal changed_rows
95
+ end
96
+ end
97
+
98
+ describe '#delete' do
99
+ let(:query_object){stub}
100
+ let(:changed_rows){1}
101
+
102
+ it 'deletes query into adaptor' do
103
+ query_object.expects(:store=).with(store)
104
+ adaptor.expects(:delete).with(query_object).returns(changed_rows)
105
+ store.delete(query_object).must_equal changed_rows
106
+ end
107
+ end
108
+
109
+ describe '#transaction' do
110
+ it 'yields to adaptor transaction method' do
111
+ adaptor.expects(:transaction).yields
112
+ store.transaction
113
+ end
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: persisto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Erik Lott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-20 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.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.14.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.14.0
69
+ description: Kodio lightweight data access layer for ruby
70
+ email:
71
+ - erik.lott@kodio.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - Guardfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/persisto.rb
83
+ - lib/persisto/adaptors/abstract_adaptor.rb
84
+ - lib/persisto/adaptors/pg.rb
85
+ - lib/persisto/exceptions.rb
86
+ - lib/persisto/mapper.rb
87
+ - lib/persisto/query.rb
88
+ - lib/persisto/repository.rb
89
+ - lib/persisto/store.rb
90
+ - lib/persisto/version.rb
91
+ - persisto.gemspec
92
+ - spec/repository_spec.rb
93
+ - spec/spec_helper.rb
94
+ - spec/store_spec.rb
95
+ homepage: ''
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.0.7
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Kodio lightweight data access layer for ruby
119
+ test_files:
120
+ - spec/repository_spec.rb
121
+ - spec/spec_helper.rb
122
+ - spec/store_spec.rb
123
+ has_rdoc: