ar_interval 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1b7fca6f5e3918ffc5b5037428c347ea57365e318752813695ad3e8ec488c202
4
+ data.tar.gz: 9b42b1468790a554798dc30153ede4aa9b296e8766f269b6b8172408ed7eec1b
5
+ SHA512:
6
+ metadata.gz: 7411be40988916a3b52972a456c4c1d3757179cf58972bcb37535337e07ca903726fa1ade90aac2d5cc46eba52364d348190a03967832b476e5f825f870e1467
7
+ data.tar.gz: e03797416b55f2c45ac25a5f1846511e2e676ed57049d064ba35e22117cfbc28de95abbd5ab48ac54f90b9e46fda6f2cb988453ce8e2c58d3b76e3723d99a50e
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ test/support/config/database.yml
10
+ tags
11
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ar_interval.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
8
+ gem "pry", "~> 0.13.1"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Caleb Buxton
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,75 @@
1
+ Support for Postgres Interval Types for ActiveRecord. Currently in [Prerelease](#prerelease)
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'ar_interval'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ar_interval
18
+
19
+ ## Usage
20
+
21
+ ### Standalone ActiveRecord
22
+
23
+ ```ruby
24
+ require 'ar_interval'
25
+
26
+ class Occurence < ActiveRecord::Base
27
+ attribute :column_name, :interval
28
+ end
29
+ ```
30
+
31
+ #### Invalid Format as a Validation error
32
+
33
+ ```ruby
34
+ require 'ar_interval'
35
+
36
+ class Occurence < ActiveRecord::Base
37
+ include ArInterval::Validations
38
+
39
+ attribute :column_name, :interval
40
+ end
41
+ ```
42
+
43
+ ### Rails
44
+
45
+ TODO
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cpb/ar_interval.
56
+
57
+ ## Roadmap
58
+
59
+ ### 1.0
60
+
61
+ - Default behaviour for Postgres Databases
62
+ ```ruby
63
+ create_table :table_name do |t|
64
+ t.interval :column_name
65
+ end
66
+ ```
67
+
68
+ ### Prerelease
69
+
70
+ - Manually registered `ActiveRecord::Type`
71
+ - Optional `ActiveSupport::Concern` providing format validations for `:interval` fields
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,85 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => ['db:drop','db:create','db:migrate', :test]
11
+
12
+ require 'pg'
13
+ require 'active_record'
14
+ require 'yaml'
15
+
16
+ file 'test/support/config/database.yml' do |task,**kargs|
17
+ FileUtils.cp(task.name + '.example',task.name)
18
+ end
19
+
20
+ $:.unshift('./test/support/models')
21
+ task :console => :database_connection do
22
+ require 'pry'
23
+ require 'occurence'
24
+
25
+ binding.pry
26
+ end
27
+
28
+ desc "Establish Database Connection"
29
+ task :database_connection => 'test/support/config/database.yml' do
30
+ connection_details = YAML::load(File.open('test/support/config/database.yml'))
31
+ ActiveRecord::Base.establish_connection(connection_details)
32
+ end
33
+
34
+ namespace :db do
35
+
36
+ task :admin_connection => 'test/support/config/database.yml' do
37
+ connection_details = YAML::load(File.open('test/support/config/database.yml'))
38
+ ActiveRecord::Base.establish_connection(connection_details.merge({
39
+ 'database'=> 'postgres',
40
+ 'schema_search_path'=> 'public'
41
+ }))
42
+ end
43
+
44
+ desc "Migrate the db"
45
+ task :migrate => :database_connection do
46
+ ActiveRecord::Migrator.migrations_paths = "test/support/db/migrations"
47
+ ActiveRecord::Tasks::DatabaseTasks.migrate
48
+ end
49
+
50
+ desc "Create the db"
51
+ task :create => ['test/support/config/database.yml', :admin_connection] do
52
+ connection_details = YAML::load(File.open('test/support/config/database.yml'))
53
+ ActiveRecord::Base.connection.create_database(connection_details.fetch('database'))
54
+ end
55
+
56
+ desc "drop the db"
57
+ task :drop => ['test/support/config/database.yml', :admin_connection] do
58
+ connection_details = YAML::load(File.open('test/support/config/database.yml'))
59
+ ActiveRecord::Base.connection.drop_database(connection_details.fetch('database'))
60
+ end
61
+ end
62
+
63
+ namespace :g do
64
+ desc "Generate migration"
65
+ task :migration do
66
+ name = ARGV[1] || raise("Specify name: rake g:migration your_migration")
67
+ timestamp = Time.now.strftime("%Y%m%d%H%M%S")
68
+ path = File.expand_path("../test/support/db/migrations/#{timestamp}_#{name}.rb", __FILE__)
69
+ migration_class = name.split("_").map(&:capitalize).join
70
+
71
+ File.open(path, 'w') do |file|
72
+ file.write <<~EOF
73
+ class #{migration_class} < ActiveRecord::Migration[5.2]
74
+ def self.up
75
+ end
76
+ def self.down
77
+ end
78
+ end
79
+ EOF
80
+ end
81
+
82
+ puts "Migration #{path} created"
83
+ abort # needed stop other tasks
84
+ end
85
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/ar_interval/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ar_interval"
5
+ spec.version = ArInterval::VERSION
6
+ spec.authors = ["Caleb Buxton"]
7
+ spec.email = ["me@cpb.ca"]
8
+
9
+ spec.summary = %q{Tools for Interval columns}
10
+ spec.description = spec.summary + " for ActiveRecord"
11
+ spec.homepage = "https://github.com/cpb/ar_interval/"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = spec.homepage + "/blob/main/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "activerecord", "~> 5.2"
29
+ spec.add_dependency "pg", "~> 1.2.3"
30
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ar_interval"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ require "active_record/type"
2
+
3
+ require "ar_interval/version"
4
+ require "ar_interval/type"
5
+ require "ar_interval/validations"
6
+
7
+ module ArInterval
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
11
+
@@ -0,0 +1,13 @@
1
+ module ArInterval
2
+ class Type < ActiveRecord::Type::Value
3
+ def cast(value)
4
+ ActiveSupport::Duration.parse(value)
5
+ end
6
+
7
+ def serialize(value)
8
+ value.iso8601
9
+ end
10
+
11
+ ActiveRecord::Type.register(:interval, self)
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ require "active_support/concern"
2
+
3
+ module ArInterval
4
+ module Validations
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :interval_fields
9
+ self.interval_fields = []
10
+ validate :validate_interval_fields
11
+ end
12
+
13
+ class_methods do
14
+ def attribute(name, cast_type = ActiveRecord::Attributes::Type::Value, **options)
15
+ case cast_type when :interval, ArInterval::Type then
16
+ self.interval_fields = [*self.interval_fields, name]
17
+ end
18
+
19
+ super
20
+ end
21
+ end
22
+
23
+ private def validate_interval_fields
24
+ interval_fields.each do |field|
25
+ field_value = read_attribute_before_type_cast(field)
26
+ begin
27
+ ActiveSupport::Duration.parse(field_value)
28
+ rescue ActiveSupport::Duration::ISO8601Parser::ParsingError => e
29
+ errors.add(field, e.message)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module ArInterval
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_interval
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Caleb Buxton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.2.3
41
+ description: Tools for Interval columns for ActiveRecord
42
+ email:
43
+ - me@cpb.ca
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - ar_interval.gemspec
54
+ - bin/console
55
+ - bin/setup
56
+ - lib/ar_interval.rb
57
+ - lib/ar_interval/type.rb
58
+ - lib/ar_interval/validations.rb
59
+ - lib/ar_interval/version.rb
60
+ homepage: https://github.com/cpb/ar_interval/
61
+ licenses:
62
+ - MIT
63
+ metadata:
64
+ homepage_uri: https://github.com/cpb/ar_interval/
65
+ source_code_uri: https://github.com/cpb/ar_interval/
66
+ changelog_uri: https://github.com/cpb/ar_interval//blob/main/CHANGELOG.md
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.3.0
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.1.4
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Tools for Interval columns
86
+ test_files: []