sequel-mapper-toucan-fork 0.2.2
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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/sequel/mapper.rb +126 -0
- data/lib/sequel/mapper/struct.rb +16 -0
- data/lib/sequel/mapper/version.rb +5 -0
- data/sequel-mapper.gemspec +27 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d85bef57f0bd23c481ed01fb3cab3d1a8caa0001
|
4
|
+
data.tar.gz: 4cb26fd2cc61bf3b62c1e9802c1fb7238663ac12
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b0eede67dfb662040e8ef4cc11ad2864cadb23da23f6968ddf6094d87964c482cf07d9ae6dc9062be99ead57fb58a03934fa98188031e04b78a53ae0a5a91d6d
|
7
|
+
data.tar.gz: 7ced32814481782c0ec7b1dc49fa5c14ad8eb90a1ef7107707887dbb0b2a0fb2958de50cb742d83be1416c78ecdeea5730bbd7ff3ced1f4acef646aded9a9de3
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.3
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Steve England
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Sequel::Mapper
|
2
|
+
|
3
|
+
A super simple [datamapper](http://martinfowler.com/eaaCatalog/dataMapper.html)
|
4
|
+
built on top of Sequel. For creating
|
5
|
+
[repository](http://martinfowler.com/eaaCatalog/repository.html) like objects.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'sequel-mapper'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install sequel-mapper
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'sequel/mapper'
|
25
|
+
|
26
|
+
class Account
|
27
|
+
def initialize(attrs={})
|
28
|
+
#some way to covert attrs to @attibutes
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Accounts
|
33
|
+
include Sequel::Mapper
|
34
|
+
dataset :accounts
|
35
|
+
end
|
36
|
+
|
37
|
+
DB = Sequel.connect
|
38
|
+
|
39
|
+
accounts = Accounts.new(DB)
|
40
|
+
|
41
|
+
accounts.all #=> all accounts
|
42
|
+
|
43
|
+
# CRUD
|
44
|
+
|
45
|
+
account = Account.new(email: 'dave@example.com')
|
46
|
+
|
47
|
+
accounts.create(account)
|
48
|
+
|
49
|
+
accounts.find(1)
|
50
|
+
|
51
|
+
accounts.update(account)
|
52
|
+
|
53
|
+
accounts.delete(account)
|
54
|
+
```
|
55
|
+
|
56
|
+
TODO: Improve usage instructions :)
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
1. Fork it ( https://github.com/stengland/sequel-mapper/fork )
|
61
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
62
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
63
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
64
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sequel/mapper"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
require "sequel/mapper/version"
|
2
|
+
require "sequel/mapper/struct"
|
3
|
+
require 'sequel/core'
|
4
|
+
require 'forwardable'
|
5
|
+
|
6
|
+
module Sequel
|
7
|
+
module Mapper
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def initialize(options)
|
11
|
+
if options.is_a? Sequel::Dataset
|
12
|
+
@db = options.db
|
13
|
+
ds = options
|
14
|
+
elsif options.is_a? Sequel::Database
|
15
|
+
@db = options
|
16
|
+
raise ArgumentError, 'no dataset defined' if self.class._dataset.nil?
|
17
|
+
ds = db[self.class._dataset]
|
18
|
+
else
|
19
|
+
raise ArgumentError, 'no database or dataset'
|
20
|
+
end
|
21
|
+
@dataset = ds.with_row_proc method(:data_to_object)
|
22
|
+
@model = self.class._model || Sequel::Mapper::Struct.new(*dataset.columns)
|
23
|
+
@primary_key = self.class._primary_key || :id
|
24
|
+
end
|
25
|
+
|
26
|
+
attr_reader :dataset
|
27
|
+
|
28
|
+
def create(object)
|
29
|
+
id = dataset.insert object_to_data(object)
|
30
|
+
object.send("#{primary_key}=", id) unless object.public_send(primary_key)
|
31
|
+
end
|
32
|
+
|
33
|
+
def find(key)
|
34
|
+
find_object(key: key).first
|
35
|
+
end
|
36
|
+
alias :[] :find
|
37
|
+
|
38
|
+
def update(object)
|
39
|
+
find_object(object).update(object_to_data(object))
|
40
|
+
end
|
41
|
+
|
42
|
+
def delete(object)
|
43
|
+
find_object(object).delete
|
44
|
+
end
|
45
|
+
|
46
|
+
def persist(object)
|
47
|
+
if find_object(object).nil?
|
48
|
+
create(object)
|
49
|
+
else
|
50
|
+
update(object)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def_delegators :dataset, :count, :all, :each, :map, :first, :last, :empty?
|
55
|
+
alias :size :count
|
56
|
+
|
57
|
+
%w{where order grep limit}.each do |sc|
|
58
|
+
define_method sc do |*args|
|
59
|
+
scope dataset.public_send(sc, *args)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def graph(*args)
|
64
|
+
scope dataset.extension(:graph_each).graph(*args)
|
65
|
+
end
|
66
|
+
|
67
|
+
def with(mapper_class, join, options = {})
|
68
|
+
graph(dataset_for_mapper_class(mapper_class), join, options)
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
attr_reader :db, :model, :primary_key
|
74
|
+
|
75
|
+
def object_to_data(object)
|
76
|
+
dataset.columns.each_with_object({}) do |column, data|
|
77
|
+
if object.respond_to?(column)
|
78
|
+
value = object.public_send(column)
|
79
|
+
data[column] = value unless value.nil?
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def data_to_object(data)
|
85
|
+
model.new data
|
86
|
+
end
|
87
|
+
|
88
|
+
def find_object(object=nil, key: object.public_send(primary_key))
|
89
|
+
dataset.where(pk_with_table => key) if key
|
90
|
+
end
|
91
|
+
|
92
|
+
def pk_with_table
|
93
|
+
"#{dataset.first_source_table}__#{primary_key}".to_sym
|
94
|
+
end
|
95
|
+
|
96
|
+
def scope(dataset)
|
97
|
+
self.class.new(dataset)
|
98
|
+
end
|
99
|
+
|
100
|
+
def dataset_for_mapper_class(mapper_class)
|
101
|
+
mapper_class.new(db).dataset
|
102
|
+
end
|
103
|
+
|
104
|
+
module ClassMethods
|
105
|
+
def model(model)
|
106
|
+
@_model = model
|
107
|
+
end
|
108
|
+
|
109
|
+
def primary_key(primary_key)
|
110
|
+
@_primary_key = primary_key
|
111
|
+
end
|
112
|
+
alias :key :primary_key
|
113
|
+
|
114
|
+
def dataset(dataset)
|
115
|
+
@_dataset = dataset
|
116
|
+
end
|
117
|
+
|
118
|
+
attr_reader :_model, :_primary_key, :_dataset
|
119
|
+
end
|
120
|
+
|
121
|
+
def self.included(receiver)
|
122
|
+
receiver.extend ClassMethods
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Mapper
|
3
|
+
class Struct < ::Struct
|
4
|
+
def initialize(args={})
|
5
|
+
args.each { |k,v| self[k] = v if members.include?(k.to_sym) }
|
6
|
+
end
|
7
|
+
|
8
|
+
def slice(*keys)
|
9
|
+
keys.reduce({}) do |hash, key|
|
10
|
+
hash[key] = self[key]
|
11
|
+
hash
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sequel/mapper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sequel-mapper-toucan-fork"
|
8
|
+
spec.version = Sequel::Mapper::VERSION
|
9
|
+
spec.authors = ["Steve England"]
|
10
|
+
spec.email = ["stephen.england@gmail.com"]
|
11
|
+
spec.summary = 'Super simple object mapper/repo built on Sequel'
|
12
|
+
|
13
|
+
spec.description = %q{Super simpler object mapper built on Sequel (http://sequel.jeremyevans.net/) and using the repository pattern http://martinfowler.com/eaaCatalog/repository.html}
|
14
|
+
spec.homepage = "https://github.com/stengland/sequel-mapper"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency('sequel')
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "sqlite3"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-mapper-toucan-fork
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve England
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-13 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: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '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.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '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'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Super simpler object mapper built on Sequel (http://sequel.jeremyevans.net/)
|
84
|
+
and using the repository pattern http://martinfowler.com/eaaCatalog/repository.html
|
85
|
+
email:
|
86
|
+
- stephen.england@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".ruby-version"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- lib/sequel/mapper.rb
|
102
|
+
- lib/sequel/mapper/struct.rb
|
103
|
+
- lib/sequel/mapper/version.rb
|
104
|
+
- sequel-mapper.gemspec
|
105
|
+
homepage: https://github.com/stengland/sequel-mapper
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.6.11
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Super simple object mapper/repo built on Sequel
|
129
|
+
test_files: []
|