simple_enum-persistence 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 +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +20 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/simple_enum/persistence/extension.rb +30 -0
- data/lib/simple_enum/persistence/hasher.rb +61 -0
- data/lib/simple_enum/persistence/version.rb +5 -0
- data/lib/simple_enum/persistence.rb +9 -0
- data/simple_enum-persistence.gemspec +37 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d7e18e945f72f1bd93194c4ce5b46cc97cbee284
|
4
|
+
data.tar.gz: f32b25307b11a3e490987be09a1fe0277d95be23
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8d4b9363873d8cafc6707c4ba7c5fad5b56d017d5d3232369faaa67358558817ebf0f4e3f7c2fb397a603edddd55d7faa2f52609f2e2ec446303624843a779f9
|
7
|
+
data.tar.gz: 1022f35a51fbac5aa3b7f6092ce83e1a64f4604e40f2ceb863da7513ff8da1743891611644f94beaefe64a317bbc8e8dceda170f3954fcfda1f6bbf40fc9dddf
|
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 Theo
|
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,20 @@
|
|
1
|
+
# SimpleEnum::Persistence
|
2
|
+
|
3
|
+
SimpleEnum::Multiple is extension of SimpleEnum, which brings data persistence support to SimpleEnum.
|
4
|
+
|
5
|
+
## ActiveRecord Quick start
|
6
|
+
|
7
|
+
Let's say we have a model Tag, we want to use it as enum data, just add this to a model:
|
8
|
+
```ruby
|
9
|
+
as_enum :tag, Tag, persistence: true
|
10
|
+
```
|
11
|
+
|
12
|
+
Then SimpleEnum will use `Tag.all` as enum data, `tag.name` as enum key and `tag.id` as enum value, if we want to use a field rather than `:name` as enum key:
|
13
|
+
```ruby
|
14
|
+
as_enum :tag, Tag, persistence: { key: :name, value: :id }
|
15
|
+
```
|
16
|
+
|
17
|
+
## License
|
18
|
+
|
19
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
20
|
+
|
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 "simple_enum/persistence"
|
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,30 @@
|
|
1
|
+
module SimpleEnum
|
2
|
+
module Persistence
|
3
|
+
module Extension
|
4
|
+
def self.included base
|
5
|
+
base.module_eval do
|
6
|
+
alias_method :as_enum_without_persistence, :as_enum
|
7
|
+
def as_enum name, values, options = {}
|
8
|
+
persistence = options.delete :persistence
|
9
|
+
if persistence
|
10
|
+
options[:with] ||= SimpleEnum.with.reject do |feature|
|
11
|
+
%i(attribute scope).include? feature
|
12
|
+
end
|
13
|
+
options[:map] = :persistence
|
14
|
+
|
15
|
+
values = [ values ]
|
16
|
+
values.push persistence if persistence.kind_of? Hash
|
17
|
+
end
|
18
|
+
|
19
|
+
as_enum_without_persistence name, values, options
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def generate_enum_persistence_extension_for enum, accessor
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
SimpleEnum.register_generator :persistence, SimpleEnum::Persistence::Extension
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module SimpleEnum
|
2
|
+
module Persistence
|
3
|
+
class Hasher < Hash
|
4
|
+
attr_reader :enum_klass, :enum_key, :enum_value
|
5
|
+
|
6
|
+
def initialize options
|
7
|
+
@enum_klass = options[0]
|
8
|
+
options = options[1] || Hash.new
|
9
|
+
@enum_key = options[:key] || :name
|
10
|
+
@enum_value = options[:value] || :id
|
11
|
+
|
12
|
+
unless enum_klass.method_defined? :update_enum_hashers
|
13
|
+
enum_klass.instance_eval do
|
14
|
+
def enum_hashers
|
15
|
+
@enum_hashers ||= []
|
16
|
+
end
|
17
|
+
|
18
|
+
def update_enum_hashers
|
19
|
+
records = all.to_a
|
20
|
+
enum_hashers.each do |hasher|
|
21
|
+
hasher.enum_replace records
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
enum_klass.class_eval do
|
27
|
+
def update_enum_hashers
|
28
|
+
self.class.update_enum_hashers
|
29
|
+
end
|
30
|
+
|
31
|
+
after_commit :update_enum_hashers
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
enum_klass.enum_hashers.push self
|
36
|
+
enum_replace enum_klass.all
|
37
|
+
end
|
38
|
+
|
39
|
+
def enum_replace records
|
40
|
+
hash = Hash.new
|
41
|
+
records.each do |record|
|
42
|
+
hash[record.send(enum_key)] = record.send(enum_value)
|
43
|
+
end
|
44
|
+
replace hash
|
45
|
+
end
|
46
|
+
|
47
|
+
# Do nothing when freeze, cause we have to motify the hash later
|
48
|
+
def freeze
|
49
|
+
self
|
50
|
+
end
|
51
|
+
|
52
|
+
class << self
|
53
|
+
alias_method :call, :new
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
SimpleEnum::Hasher::HASHERS.merge!({
|
58
|
+
persistence: Hasher
|
59
|
+
})
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_enum/persistence/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple_enum-persistence"
|
8
|
+
spec.version = SimpleEnum::Persistence::VERSION
|
9
|
+
spec.authors = ["Theo"]
|
10
|
+
spec.email = ["bbtfrr@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Persistence support for SimpleEnum.}
|
13
|
+
spec.description = %q{SimpleEnum::Multiple is extension of SimpleEnum, which brings data persistence support to SimpleEnum.}
|
14
|
+
spec.homepage = "https://github.com/bbtfr/simple_enum-persistence"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_runtime_dependency 'simple_enum', '~> 2.2'
|
31
|
+
|
32
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
33
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
34
|
+
spec.add_development_dependency 'activerecord', '>= 4.0.0'
|
35
|
+
spec.add_development_dependency 'mongoid', '>= 4.0.0'
|
36
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_enum-persistence
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Theo
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: simple_enum
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.2'
|
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: activerecord
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.0.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.0.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mongoid
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 4.0.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 4.0.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.14'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.14'
|
97
|
+
description: SimpleEnum::Multiple is extension of SimpleEnum, which brings data persistence
|
98
|
+
support to SimpleEnum.
|
99
|
+
email:
|
100
|
+
- bbtfrr@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- lib/simple_enum/persistence.rb
|
114
|
+
- lib/simple_enum/persistence/extension.rb
|
115
|
+
- lib/simple_enum/persistence/hasher.rb
|
116
|
+
- lib/simple_enum/persistence/version.rb
|
117
|
+
- simple_enum-persistence.gemspec
|
118
|
+
homepage: https://github.com/bbtfr/simple_enum-persistence
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata:
|
122
|
+
allowed_push_host: https://rubygems.org
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 2.4.6
|
140
|
+
signing_key:
|
141
|
+
specification_version: 4
|
142
|
+
summary: Persistence support for SimpleEnum.
|
143
|
+
test_files: []
|