simple_enum-persistence 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/README.md +1 -1
- data/lib/simple_enum/persistence/hasher.rb +42 -5
- data/lib/simple_enum/persistence/version.rb +1 -1
- metadata +17 -17
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YmM0YTYwZDZmMzI4OWFiZDU0NjgwZTA3NGZlZWMzOTUyODhlMTA2Ng==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NmIwNTZmYmVjNDcwMGQ2NDZiYzEyNDIyZDA1MWViYjFjZTMyZDBhYg==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
Y2U5MzYwYmQwYWY3Mzk5NjEwZGQ0M2YwMWU1OTNkMDBjY2MxZTViYjY1ZGNh
|
10
|
+
MGU5MWQ3Y2NiZGRkYWE5ZmE0ZDA3YjJmYmFjZGU0Zjk0Njc2MjU4Y2RlYmVi
|
11
|
+
YzJkOTljZTExNWQ0ZjUxMjdjZmE0ZTg1NjUyZjE4ZGU3MGE5MjE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NTRkMWFkOTZhMGJhYTkzMjk0NGUyNmY3YjgzOGI3ZmE1NTJmYmZjZGVjMjEz
|
14
|
+
OGZiMTAxY2IyZWZmOWM1NzAzMTkzODJjYmVkZGRkZTYxYmI1YTAzOWMxODM0
|
15
|
+
NTcwM2YyZGU1ZjE3OWE4NWFmMWEwOWM4ZjRjNzQ4ZmNhYTgyNGI=
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
[![Gem](https://img.shields.io/gem/v/simple_enum-persistence.svg)](https://rubygems.org/gems/simple_enum-persistence)
|
5
5
|
|
6
6
|
SimpleEnum::Persistence is extension of SimpleEnum, which brings data persistence support to SimpleEnum.
|
7
|
-
Sometimes, we need to use one of our
|
7
|
+
Sometimes, we need to use one of our Model as enum data source, in order to add/remove enum type dynamically, SimpleEnum::Persistence is built for this!
|
8
8
|
|
9
9
|
## Why not ActiveRecord association?
|
10
10
|
|
@@ -1,7 +1,9 @@
|
|
1
1
|
module SimpleEnum
|
2
2
|
module Persistence
|
3
|
-
class Hasher
|
4
|
-
|
3
|
+
class Hasher
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
attr_reader :enum_klass, :enum_key, :enum_value, :enum_mapping
|
5
7
|
|
6
8
|
def initialize options
|
7
9
|
@enum_klass = options[0]
|
@@ -37,11 +39,46 @@ module SimpleEnum
|
|
37
39
|
end
|
38
40
|
|
39
41
|
def enum_replace records
|
40
|
-
|
42
|
+
@enum_mapping = Array.new
|
41
43
|
records.each do |record|
|
42
|
-
|
44
|
+
key = record.send(enum_key)
|
45
|
+
value = record.send(enum_value)
|
46
|
+
@enum_mapping.push([key, value])
|
43
47
|
end
|
44
|
-
|
48
|
+
end
|
49
|
+
|
50
|
+
def each_pair &block
|
51
|
+
@enum_mapping.each &block
|
52
|
+
end
|
53
|
+
alias_method :each, :each_pair
|
54
|
+
|
55
|
+
def key? key
|
56
|
+
@enum_mapping.any? { |k, v| k == key }
|
57
|
+
end
|
58
|
+
|
59
|
+
def value? value
|
60
|
+
@enum_mapping.any? { |k, v| v == value }
|
61
|
+
end
|
62
|
+
|
63
|
+
def key value
|
64
|
+
@enum_mapping.find { |k, v| v == value }[0]
|
65
|
+
end
|
66
|
+
|
67
|
+
def value key
|
68
|
+
@enum_mapping.find { |k, v| k == key }[1]
|
69
|
+
end
|
70
|
+
alias_method :[], :value
|
71
|
+
|
72
|
+
def keys
|
73
|
+
@enum_mapping.map { |k, v| k }
|
74
|
+
end
|
75
|
+
|
76
|
+
def values
|
77
|
+
@enum_mapping.map { |k, v| v }
|
78
|
+
end
|
79
|
+
|
80
|
+
def values_at *keys
|
81
|
+
@enum_mapping.select { |k, v| keys.include? k }.map { |k, v| v }
|
45
82
|
end
|
46
83
|
|
47
84
|
# Do nothing when freeze, cause we have to motify the hash later
|
metadata
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_enum-persistence
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Theo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple_enum
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '2.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: activerecord
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ! '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: 4.0.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ! '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 4.0.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: mongoid
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 4.0.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ! '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 4.0.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ~>
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '2.14'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '2.14'
|
83
83
|
description: SimpleEnum::Persistence is extension of SimpleEnum, which brings data
|
@@ -88,8 +88,8 @@ executables: []
|
|
88
88
|
extensions: []
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
|
-
-
|
92
|
-
-
|
91
|
+
- .gitignore
|
92
|
+
- .travis.yml
|
93
93
|
- Gemfile
|
94
94
|
- LICENSE.txt
|
95
95
|
- README.md
|
@@ -112,17 +112,17 @@ require_paths:
|
|
112
112
|
- lib
|
113
113
|
required_ruby_version: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - ! '>='
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '0'
|
118
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
119
|
requirements:
|
120
|
-
- -
|
120
|
+
- - ! '>='
|
121
121
|
- !ruby/object:Gem::Version
|
122
122
|
version: '0'
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.4.
|
125
|
+
rubygems_version: 2.4.8
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: Persistence support for SimpleEnum.
|