record_store 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/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +73 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/record_store.rb +91 -0
- data/record_store.gemspec +20 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ed2b5bff4fe20d7492d9d7962db5ac19d01db361
|
4
|
+
data.tar.gz: e31babad47cd7164102dea6969af1d5246cb72d7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9a580a20dd9a4bb281fdb963e54f4ca02f6b9c6185b98b4e3e31bf4659ee9a63c8e92cbc78325b010cd87ab6a5ab1ff78c24af0b60b08d67b6ddfdad4a47130f
|
7
|
+
data.tar.gz: de785c0265a5bdfd1f45ba70ca169a9b65ee3fa43192854abe9662cd7139f353a4e224f23847bbdc979381a01a1c1ac5db7715b5ea74e7916c5c0748cbb72600
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Paul Barry
|
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,73 @@
|
|
1
|
+
# Record Store
|
2
|
+
|
3
|
+
Store records in your database using Sequel
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'record_store'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install record_store
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
If you have an application where you are working with records as hashes and you would like store them in your database using Sequel, this gem can help make that happen. To use Record Store, you should subclass the `RecordStore` class at least once in your application. If for example you already have a reference to a Sequel database stored in a constant called `DB`, then you would do something lke this:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Store < RecordStore
|
27
|
+
def database
|
28
|
+
DB
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Then for each table you want to store records in, you subclass that class:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class UserStore < Store
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
You can now store records in the `users` table like this:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
UserStore << { first_name: 'Paul', last_name: 'Barry' }
|
44
|
+
```
|
45
|
+
|
46
|
+
You can also add validations to your store, like this:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
class UserStore < Store
|
50
|
+
required_attributes :first_name, :last_name
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
If you try to store a record without values for those attributes, you will get an error:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
UserStore << {}
|
58
|
+
# => { errors: ["First Name is required", "Last Name is required"]}
|
59
|
+
```
|
60
|
+
|
61
|
+
## Development
|
62
|
+
|
63
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
64
|
+
|
65
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
1. Fork it ( https://github.com/[my-github-username]/record_store/fork )
|
70
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
72
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
73
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "record_store"
|
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
data/lib/record_store.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
class RecordStore
|
2
|
+
VERSION = "0.1.0"
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def put(record)
|
6
|
+
new(record).put
|
7
|
+
end
|
8
|
+
alias_method :<<, :put
|
9
|
+
|
10
|
+
def dataset_name
|
11
|
+
@dataset_name ||= name.sub(/Store$/,'').tableize.to_sym
|
12
|
+
end
|
13
|
+
|
14
|
+
def validations
|
15
|
+
@validations ||= []
|
16
|
+
end
|
17
|
+
|
18
|
+
def required_attributes(*record)
|
19
|
+
record.each do |attribute|
|
20
|
+
validations << proc do
|
21
|
+
errors << "#{attribute.to_s.titleize} is required" unless @record[attribute].present?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def required_foreign_key(attribute, table)
|
27
|
+
validations << proc do
|
28
|
+
if @record[attribute].blank?
|
29
|
+
errors << "#{attribute} is required"
|
30
|
+
elsif database[table].where(id: @record[attribute]).get(:id).blank?
|
31
|
+
errors << "no #{table.to_s.singularize} with id=#{@record[attribute]}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize(record)
|
38
|
+
@record = record
|
39
|
+
end
|
40
|
+
|
41
|
+
def put
|
42
|
+
transform
|
43
|
+
return { errors: 'no record provided' } if @record.blank?
|
44
|
+
validate
|
45
|
+
return { errors: errors } if errors.present?
|
46
|
+
|
47
|
+
save
|
48
|
+
end
|
49
|
+
|
50
|
+
def save
|
51
|
+
if exists?
|
52
|
+
update
|
53
|
+
else
|
54
|
+
insert
|
55
|
+
end
|
56
|
+
@record
|
57
|
+
end
|
58
|
+
|
59
|
+
def database
|
60
|
+
raise "You must setup your database"
|
61
|
+
end
|
62
|
+
|
63
|
+
def dataset
|
64
|
+
database[self.class.dataset_name]
|
65
|
+
end
|
66
|
+
|
67
|
+
def errors
|
68
|
+
@errors ||= []
|
69
|
+
end
|
70
|
+
|
71
|
+
def transform
|
72
|
+
end
|
73
|
+
|
74
|
+
def validate
|
75
|
+
self.class.validations.each do |validation|
|
76
|
+
instance_eval(&validation)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def exists?
|
81
|
+
!dataset.select(1).where(id: @record[:id]).empty?
|
82
|
+
end
|
83
|
+
|
84
|
+
def insert
|
85
|
+
dataset.insert(@record)
|
86
|
+
end
|
87
|
+
|
88
|
+
def update
|
89
|
+
dataset.where(id: @record[:id]).update(@record.except(:id))
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "record_store"
|
5
|
+
spec.version = "0.1.0"
|
6
|
+
spec.authors = ["Paul Barry"]
|
7
|
+
spec.email = ["mail@paulbarry.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Easily store records in your database with Sequel}
|
10
|
+
spec.homepage = "http://github.com/pjb3/record_store"
|
11
|
+
spec.license = "MIT"
|
12
|
+
|
13
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
14
|
+
spec.bindir = "exe"
|
15
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
16
|
+
spec.require_paths = ["lib"]
|
17
|
+
|
18
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
19
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: record_store
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Barry
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- mail@paulbarry.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/console
|
55
|
+
- bin/setup
|
56
|
+
- lib/record_store.rb
|
57
|
+
- record_store.gemspec
|
58
|
+
homepage: http://github.com/pjb3/record_store
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.4.5
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Easily store records in your database with Sequel
|
82
|
+
test_files: []
|