sequel-mapper 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- 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/struct.rb +16 -0
- data/lib/sequel/mapper/version.rb +5 -0
- data/lib/sequel/mapper.rb +115 -0
- data/sequel-mapper.gemspec +27 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6edaf8e9b7ad1333d4b8e8c5a3e1054a93c92964
|
4
|
+
data.tar.gz: 1387ac5097367c251de7fa5f4a0664711778b858
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d23475d709643bac1a17226f9f3fb443b42271aa36a16d3f96c06a95dde1013aa7f251becf5573ebf93104f995be46fe266b7812637f16db39d07f824c8be5e3
|
7
|
+
data.tar.gz: 16fadee8e33c1dfed0d332af1dd63ae811edddd67415eda9a382d44d2a7b79e7abc2e131b004f250a968ccd35fe8f6c8bbcba7baa147dd8204e51a89743716c8
|
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,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,115 @@
|
|
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
|
+
@dataset = options.clone
|
14
|
+
elsif options.is_a? Sequel::Database
|
15
|
+
@db = options
|
16
|
+
raise ArgumentError 'no dataset defined' if self.class._dataset.nil?
|
17
|
+
@dataset = db[self.class._dataset]
|
18
|
+
else
|
19
|
+
raise ArgumentError 'no database or dataset'
|
20
|
+
end
|
21
|
+
@dataset.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 create_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 perist(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
|
55
|
+
|
56
|
+
%w{where order grep limit}.each do |sc|
|
57
|
+
define_method sc do |*args|
|
58
|
+
scope dataset.public_send(sc, *args)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def graph(*args)
|
63
|
+
scope dataset.extension(:graph_each).graph(*args)
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
attr_reader :db, :model, :primary_key
|
69
|
+
|
70
|
+
def object_to_data(object)
|
71
|
+
dataset.columns.reduce({}) do |hash, column|
|
72
|
+
hash[column] = object.public_send(column) if object.respond_to?(column)
|
73
|
+
hash
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def create_data(object)
|
78
|
+
object_to_data(object).delete_if { |k, v| v.to_s.strip == '' }
|
79
|
+
end
|
80
|
+
|
81
|
+
def data_to_object(data)
|
82
|
+
model.new data
|
83
|
+
end
|
84
|
+
|
85
|
+
def find_object(object=nil, key: object.public_send(primary_key))
|
86
|
+
dataset.where(primary_key => key) if key
|
87
|
+
end
|
88
|
+
|
89
|
+
def scope(dataset)
|
90
|
+
self.class.new(dataset)
|
91
|
+
end
|
92
|
+
|
93
|
+
module ClassMethods
|
94
|
+
def model(model)
|
95
|
+
@_model = model
|
96
|
+
end
|
97
|
+
|
98
|
+
def primary_key(primary_key)
|
99
|
+
@_primary_key = primary_key
|
100
|
+
end
|
101
|
+
alias :key :primary_key
|
102
|
+
|
103
|
+
def dataset(dataset)
|
104
|
+
@_dataset = dataset
|
105
|
+
end
|
106
|
+
|
107
|
+
attr_reader :_model, :_primary_key, :_dataset
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.included(receiver)
|
111
|
+
receiver.extend ClassMethods
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
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"
|
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
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve England
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-01 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.4.5.1
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Super simple object mapper/repo built on Sequel
|
129
|
+
test_files: []
|