sequel-enum_values 1.0.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/sequel/plugins/enum_values.rb +91 -8
- metadata +24 -38
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f81fca70a9503e19d460aa390dee3b66f84f098566d33c7e5cbcfb145333aff6
|
4
|
+
data.tar.gz: 4c97bce203bd0f1b6cf8c7ce49fe00fec806661896d650592bc85ff647fde0aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8124b2eca292e683b2dc2f78fdaa0d1f12f73d40e92d9bb588a96a7492ce002b21575fdfe44325e1c5e3e5e980f3a5c6cc757cfd1c3184f618e2639d98ae785
|
7
|
+
data.tar.gz: 957ba624bbb0124d30cdce2e263f2ec7ba637ec3a47be52315fd6c8b21ab57023ddfb6208d1fa44223efa18ad3ac03bd08598abf11f27d4615288dae795af4e2
|
@@ -2,20 +2,103 @@
|
|
2
2
|
|
3
3
|
module Sequel
|
4
4
|
module Plugins
|
5
|
+
## Plugin for getting enum values from PostgreSQL by field name
|
5
6
|
module EnumValues
|
6
|
-
VERSION = '1.
|
7
|
+
VERSION = '1.2.1'
|
8
|
+
|
9
|
+
## Initialize model state for this plugin
|
10
|
+
## @param model [Sequel::Model] model for which plugin applying
|
11
|
+
## @param _options [Hash] options (don't affect anything in this moment)
|
12
|
+
def self.apply(model, _options = {})
|
13
|
+
model.instance_exec do
|
14
|
+
@enum_values_cache = {}
|
15
|
+
@enum_values_caching = true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
## Configure model for this plugin
|
20
|
+
## @param model [Sequel::Model] model in which plugin enabled
|
21
|
+
## @param options [Hash] options for plugin
|
22
|
+
## @option options [Boolean] caching (true) cache enum values
|
23
|
+
## @option options [Boolean, Symbol, Array<Symbol>]
|
24
|
+
## predicate_methods (false)
|
25
|
+
## enum fields for which predicate methods will be defined
|
26
|
+
## @example Disable caching
|
27
|
+
## Item.plugin :enum_values, caching: false
|
28
|
+
## @example Define predicate methods for all enum fields
|
29
|
+
## Item.plugin :enum_values, predicate_methods: true
|
30
|
+
## @example Define predicate methods for specific enum fields
|
31
|
+
## Item.plugin :enum_values, predicate_methods: %[status type]
|
32
|
+
## @example Define predicate methods for specific enum field
|
33
|
+
## Item.plugin :enum_values, predicate_methods: :status
|
34
|
+
def self.configure(model, options = {})
|
35
|
+
model.instance_exec do
|
36
|
+
@enum_values_caching = options.fetch(:caching, @enum_values_caching)
|
37
|
+
|
38
|
+
predicate_methods = options.fetch(:predicate_methods, false)
|
39
|
+
|
40
|
+
transform_predicate_methods_to_enum_fields(predicate_methods)
|
41
|
+
.each do |field|
|
42
|
+
all_enum_fields[field][:enum_values].each do |enum_value|
|
43
|
+
define_predicate_method field, enum_value
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
7
48
|
|
8
49
|
## Module for class methods
|
9
50
|
module ClassMethods
|
51
|
+
## Get enum values for specific field
|
52
|
+
## @param field [Symbol] name of enum field
|
53
|
+
## @return [Array<String>] values of enum
|
54
|
+
## @example Get enum values for `status` field of `Item` model
|
55
|
+
## Item.enum_values(:status)
|
10
56
|
def enum_values(field)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
57
|
+
if @enum_values_caching && (cached_values = @enum_values_cache[field])
|
58
|
+
return cached_values
|
59
|
+
end
|
60
|
+
|
61
|
+
field_schema = all_enum_fields[field]
|
62
|
+
raise_field_nonexistent(field) if field_schema.nil?
|
63
|
+
|
64
|
+
enum_values = field_schema[:enum_values]
|
65
|
+
@enum_values_cache[field] = enum_values if @enum_values_caching
|
66
|
+
enum_values
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def transform_predicate_methods_to_enum_fields(predicate_methods)
|
72
|
+
case predicate_methods
|
73
|
+
when TrueClass
|
74
|
+
all_enum_fields.keys
|
75
|
+
when FalseClass
|
76
|
+
[]
|
77
|
+
else
|
78
|
+
Array(predicate_methods)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def define_predicate_method(field, enum_value)
|
83
|
+
define_method "#{enum_value}?" do
|
84
|
+
public_send(field) == enum_value
|
17
85
|
end
|
18
|
-
|
86
|
+
end
|
87
|
+
|
88
|
+
def all_enum_fields
|
89
|
+
if @enum_values_caching && defined?(@all_enum_fields)
|
90
|
+
return @all_enum_fields
|
91
|
+
end
|
92
|
+
@all_enum_fields =
|
93
|
+
db.schema(table_name).to_h
|
94
|
+
.select { |_field, schema| schema.key?(:enum_values) }
|
95
|
+
end
|
96
|
+
|
97
|
+
def raise_field_nonexistent(field)
|
98
|
+
raise(
|
99
|
+
ArgumentError,
|
100
|
+
"'#{table_name}' table does not have '#{field}' column"
|
101
|
+
)
|
19
102
|
end
|
20
103
|
end
|
21
104
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel-enum_values
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Popov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -31,61 +31,47 @@ dependencies:
|
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '6'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
35
|
-
requirement: !ruby/object:Gem::Requirement
|
36
|
-
requirements:
|
37
|
-
- - "~>"
|
38
|
-
- !ruby/object:Gem::Version
|
39
|
-
version: '0.50'
|
40
|
-
type: :development
|
41
|
-
prerelease: false
|
42
|
-
version_requirements: !ruby/object:Gem::Requirement
|
43
|
-
requirements:
|
44
|
-
- - "~>"
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: '0.50'
|
47
|
-
- !ruby/object:Gem::Dependency
|
48
|
-
name: rake
|
34
|
+
name: codecov
|
49
35
|
requirement: !ruby/object:Gem::Requirement
|
50
36
|
requirements:
|
51
37
|
- - "~>"
|
52
38
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
39
|
+
version: '0'
|
54
40
|
type: :development
|
55
41
|
prerelease: false
|
56
42
|
version_requirements: !ruby/object:Gem::Requirement
|
57
43
|
requirements:
|
58
44
|
- - "~>"
|
59
45
|
- !ruby/object:Gem::Version
|
60
|
-
version: '
|
46
|
+
version: '0'
|
61
47
|
- !ruby/object:Gem::Dependency
|
62
|
-
name:
|
48
|
+
name: pry
|
63
49
|
requirement: !ruby/object:Gem::Requirement
|
64
50
|
requirements:
|
65
51
|
- - "~>"
|
66
52
|
- !ruby/object:Gem::Version
|
67
|
-
version: '
|
53
|
+
version: '0'
|
68
54
|
type: :development
|
69
55
|
prerelease: false
|
70
56
|
version_requirements: !ruby/object:Gem::Requirement
|
71
57
|
requirements:
|
72
58
|
- - "~>"
|
73
59
|
- !ruby/object:Gem::Version
|
74
|
-
version: '
|
60
|
+
version: '0'
|
75
61
|
- !ruby/object:Gem::Dependency
|
76
|
-
name:
|
62
|
+
name: pry-byebug
|
77
63
|
requirement: !ruby/object:Gem::Requirement
|
78
64
|
requirements:
|
79
65
|
- - "~>"
|
80
66
|
- !ruby/object:Gem::Version
|
81
|
-
version: '
|
67
|
+
version: '3.5'
|
82
68
|
type: :development
|
83
69
|
prerelease: false
|
84
70
|
version_requirements: !ruby/object:Gem::Requirement
|
85
71
|
requirements:
|
86
72
|
- - "~>"
|
87
73
|
- !ruby/object:Gem::Version
|
88
|
-
version: '
|
74
|
+
version: '3.5'
|
89
75
|
- !ruby/object:Gem::Dependency
|
90
76
|
name: rack-test
|
91
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -101,61 +87,61 @@ dependencies:
|
|
101
87
|
- !ruby/object:Gem::Version
|
102
88
|
version: '0'
|
103
89
|
- !ruby/object:Gem::Dependency
|
104
|
-
name:
|
90
|
+
name: rake
|
105
91
|
requirement: !ruby/object:Gem::Requirement
|
106
92
|
requirements:
|
107
93
|
- - "~>"
|
108
94
|
- !ruby/object:Gem::Version
|
109
|
-
version: '
|
95
|
+
version: '12'
|
110
96
|
type: :development
|
111
97
|
prerelease: false
|
112
98
|
version_requirements: !ruby/object:Gem::Requirement
|
113
99
|
requirements:
|
114
100
|
- - "~>"
|
115
101
|
- !ruby/object:Gem::Version
|
116
|
-
version: '
|
102
|
+
version: '12'
|
117
103
|
- !ruby/object:Gem::Dependency
|
118
|
-
name:
|
104
|
+
name: rspec
|
119
105
|
requirement: !ruby/object:Gem::Requirement
|
120
106
|
requirements:
|
121
107
|
- - "~>"
|
122
108
|
- !ruby/object:Gem::Version
|
123
|
-
version: '
|
109
|
+
version: '3.7'
|
124
110
|
type: :development
|
125
111
|
prerelease: false
|
126
112
|
version_requirements: !ruby/object:Gem::Requirement
|
127
113
|
requirements:
|
128
114
|
- - "~>"
|
129
115
|
- !ruby/object:Gem::Version
|
130
|
-
version: '
|
116
|
+
version: '3.7'
|
131
117
|
- !ruby/object:Gem::Dependency
|
132
|
-
name:
|
118
|
+
name: rubocop
|
133
119
|
requirement: !ruby/object:Gem::Requirement
|
134
120
|
requirements:
|
135
121
|
- - "~>"
|
136
122
|
- !ruby/object:Gem::Version
|
137
|
-
version: '0'
|
123
|
+
version: '0.52'
|
138
124
|
type: :development
|
139
125
|
prerelease: false
|
140
126
|
version_requirements: !ruby/object:Gem::Requirement
|
141
127
|
requirements:
|
142
128
|
- - "~>"
|
143
129
|
- !ruby/object:Gem::Version
|
144
|
-
version: '0'
|
130
|
+
version: '0.52'
|
145
131
|
- !ruby/object:Gem::Dependency
|
146
|
-
name:
|
132
|
+
name: simplecov
|
147
133
|
requirement: !ruby/object:Gem::Requirement
|
148
134
|
requirements:
|
149
135
|
- - "~>"
|
150
136
|
- !ruby/object:Gem::Version
|
151
|
-
version: '
|
137
|
+
version: '0'
|
152
138
|
type: :development
|
153
139
|
prerelease: false
|
154
140
|
version_requirements: !ruby/object:Gem::Requirement
|
155
141
|
requirements:
|
156
142
|
- - "~>"
|
157
143
|
- !ruby/object:Gem::Version
|
158
|
-
version: '
|
144
|
+
version: '0'
|
159
145
|
description: Now your `Sequel::Model` classes has method for getting `pg_enum` values
|
160
146
|
from DataBase by field name.
|
161
147
|
email:
|
@@ -185,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
185
171
|
version: '0'
|
186
172
|
requirements: []
|
187
173
|
rubyforge_project:
|
188
|
-
rubygems_version: 2.
|
174
|
+
rubygems_version: 2.7.3
|
189
175
|
signing_key:
|
190
176
|
specification_version: 4
|
191
177
|
summary: Sequel plugin that provides method for getting `pg_enum` values
|