activerecord-sti-enum 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/Gemfile +4 -0
- data/README.md +37 -0
- data/Rakefile +1 -0
- data/activerecord-sti-enum.gemspec +23 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/activerecord/sti/enum.rb +59 -0
- data/lib/activerecord/sti/enum/version.rb +7 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f160cf314f06a22cdb7192d98c787b6f8edd179c
|
4
|
+
data.tar.gz: aec41977a488f8a2c9bd693e89159b96291a43fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1232a835a58ee6f5a9c4ab03d924b8dd88dd0c59565b681b1026e89b609eec4874e478961f95bd1ef9e39bc19974835d58b170ca23f7802038ddb6f26d6fd40e
|
7
|
+
data.tar.gz: afa3a6a35d03540ab691bd9898c1c78e1137684d20f26d95b3b804fdf2b4cc6e72ca18901fefc119193bc1398ba92b194e06c36ffa9eee20a8a96c69cbcfafc0
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# ActiveRecord::Sti::Enum
|
2
|
+
|
3
|
+
Automatically creates enums of STI.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'activerecord-sti-enum'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install activerecord-sti-enum
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Just include ActiveRecord::Sti::Enum to parent model of STI.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
include ActiveRecord::Sti::Enum
|
27
|
+
```
|
28
|
+
|
29
|
+
## Development
|
30
|
+
|
31
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
32
|
+
|
33
|
+
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).
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/wata-gh/activerecord-sti-enum.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'activerecord/sti/enum/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activerecord-sti-enum"
|
8
|
+
spec.version = Activerecord::Sti::Enum::VERSION
|
9
|
+
spec.authors = ["wata"]
|
10
|
+
spec.email = ["wata.gm@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{automatically creates enum of sti.}
|
13
|
+
spec.description = %q{automatically creates enum of sti by including.}
|
14
|
+
spec.homepage = "https://github.com/wata-gh/activerecord-sti-enum"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "activerecord/sti/enum"
|
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,59 @@
|
|
1
|
+
require "activerecord/sti/enum/version"
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Sti
|
5
|
+
module Enum
|
6
|
+
def self.included(klass)
|
7
|
+
klass.extend ClassMethods
|
8
|
+
klass.class_eval do
|
9
|
+
def self.inherited(child)
|
10
|
+
begin
|
11
|
+
self.class_eval do
|
12
|
+
add_enum({self.inheritance_column => {child.to_s.underscore.to_sym => child.to_s}})
|
13
|
+
end
|
14
|
+
ensure
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
Dir.glob('./app/models/*.rb').each do |f|
|
20
|
+
require File.basename(f)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
def add_enum definitions
|
26
|
+
klass = self
|
27
|
+
definitions.each do |name, values|
|
28
|
+
enum_values = defined_enums[name.to_s] ||= ActiveSupport::HashWithIndifferentAccess.new
|
29
|
+
name = name.to_sym
|
30
|
+
|
31
|
+
klass.singleton_class.send(:define_method, name.to_s.pluralize) { enum_values }
|
32
|
+
|
33
|
+
_enum_methods_module.module_eval do
|
34
|
+
pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
|
35
|
+
pairs.each do |value, i|
|
36
|
+
enum_values[value] = i
|
37
|
+
|
38
|
+
define_method("#{value}?") { self[name] == i }
|
39
|
+
define_method("#{value}!") { update! name => value }
|
40
|
+
|
41
|
+
klass.scope value, -> { klass.where name => i }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
defined_enums[name.to_s] = enum_values
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def _enum_methods_module
|
51
|
+
@_enum_methods_module ||= begin
|
52
|
+
mod = Module.new
|
53
|
+
include mod
|
54
|
+
mod
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-sti-enum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- wata
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-06 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
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: automatically creates enum of sti by including.
|
42
|
+
email:
|
43
|
+
- wata.gm@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- activerecord-sti-enum.gemspec
|
53
|
+
- bin/console
|
54
|
+
- bin/setup
|
55
|
+
- lib/activerecord/sti/enum.rb
|
56
|
+
- lib/activerecord/sti/enum/version.rb
|
57
|
+
homepage: https://github.com/wata-gh/activerecord-sti-enum
|
58
|
+
licenses: []
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.4.5.1
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: automatically creates enum of sti.
|
80
|
+
test_files: []
|