sequel_record_store 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 43111fe5ad988d19f8c6b8d21cd58260f8030d4d
4
+ data.tar.gz: ecc5990de1b8871de6f6e431b95adeed1bfa9c20
5
+ SHA512:
6
+ metadata.gz: f847d22b3c76f57e4a13cca6c40d65bc77bb8fe86b81b58c9629fff22afa98644db494f5e7c2bdd9dbf3d1d7aa29d84416c81b1111433411b686df0f7d02613a
7
+ data.tar.gz: 44e5d01b203c82923c1690e1185fbbef99c4b2dcd4305d36b0978d1ef19ffcc89749b0d433d42028ab665823d7efe83fffc1d365d32dd543e0e0f64f2ff00c06
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in record_store.gemspec
4
+ gemspec
@@ -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.
@@ -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 'sequel_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 `SequelRecordStore` 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 < SequelRecordStore
27
+ def self.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
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,122 @@
1
+ class SequelRecordStore
2
+ VERSION = "0.4.0"
3
+
4
+ class << self
5
+ def database
6
+ raise "You must setup your database"
7
+ end
8
+
9
+ def dataset_name
10
+ @dataset_name ||= name.sub(/Store$/,'').tableize.to_sym
11
+ end
12
+
13
+ def dataset
14
+ database[dataset_name]
15
+ end
16
+
17
+ def get(id)
18
+ dataset.where(id: id).first
19
+ end
20
+ alias_method :[], :get
21
+
22
+ def put(record)
23
+ new(record).put
24
+ end
25
+ alias_method :<<, :put
26
+
27
+ def type_column_map(database)
28
+ @type_column_map ||= database.schema(dataset_name).each_with_object({}) do |(column,metadata),map|
29
+ map[metadata[:db_type]] ||= []
30
+ map[metadata[:db_type]] << column
31
+ end
32
+ end
33
+
34
+ def validations
35
+ @validations ||= []
36
+ end
37
+
38
+ def required_attributes(*record)
39
+ record.each do |attribute|
40
+ validations << proc do
41
+ errors << "#{attribute.to_s.titleize} is required" unless @record[attribute].present?
42
+ end
43
+ end
44
+ end
45
+
46
+ def required_foreign_key(attribute, table)
47
+ validations << proc do
48
+ if @record[attribute].blank?
49
+ errors << "#{attribute} is required"
50
+ elsif database[table].where(id: @record[attribute]).get(:id).blank?
51
+ errors << "no #{table.to_s.singularize} with id=#{@record[attribute]}"
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ def initialize(record)
58
+ @record = record
59
+ end
60
+
61
+ def put
62
+ typecast
63
+ transform
64
+ return { errors: 'no record provided' } if @record.blank?
65
+ validate
66
+ return { errors: errors } if errors.present?
67
+
68
+ save
69
+ end
70
+
71
+ def save
72
+ if exists?
73
+ update
74
+ else
75
+ insert
76
+ end
77
+ @record
78
+ end
79
+
80
+ def database
81
+ self.class.database
82
+ end
83
+
84
+ def dataset
85
+ self.class.dataset
86
+ end
87
+
88
+ def errors
89
+ @errors ||= []
90
+ end
91
+
92
+ def typecast
93
+ if database.database_type == :postgres
94
+ Array(self.class.type_column_map(database)['json']).each do |json_column|
95
+ if @record[json_column]
96
+ @record[json_column] = Sequel.pg_json(@record[json_column])
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ def transform
103
+ end
104
+
105
+ def validate
106
+ self.class.validations.each do |validation|
107
+ instance_eval(&validation)
108
+ end
109
+ end
110
+
111
+ def exists?
112
+ !dataset.select(1).where(id: @record[:id]).empty?
113
+ end
114
+
115
+ def insert
116
+ @record[:id] = dataset.insert(@record.except(:errors))
117
+ end
118
+
119
+ def update
120
+ dataset.where(id: @record[:id]).update(@record.except(:id, :errors))
121
+ end
122
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "sequel_record_store"
5
+ spec.version = "0.4.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/sequel_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: sequel_record_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Barry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-12 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/sequel_record_store.rb
57
+ - record_store.gemspec
58
+ homepage: http://github.com/pjb3/sequel_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.5.1
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Easily store records in your database with Sequel
82
+ test_files: []