sequel_enum 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 +5 -5
- data/lib/sequel_enum.rb +6 -8
- data/lib/sequel_enum/version.rb +1 -1
- data/sequel_enum.gemspec +5 -3
- data/spec/sequel_enum_spec.rb +11 -5
- data/spec/spec_helper.rb +10 -0
- metadata +14 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c9b69d2963caa5819ad5616be906ec65b2c1061d84a7b552296549e88b052cdf
|
4
|
+
data.tar.gz: 0403a21ebd47a82cd558e639dfce27185fd26d41a3771f0f075a2e54e4051a7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd54f66811564832f51d292aa377090a8fcbfa56feca5358ceb7058af14554851bb116da02668ac5749ee7159d7e824a1d6b1d3098195bf2becd641f24c7cec3
|
7
|
+
data.tar.gz: 63296d2f13271084c03eb44c158a2c45f79a36fb968ca3d6167005def6837ca66df3a7fad10ac0491d50bde340a2c47f1f9ab7669ef8ef7e7689d1519b26f086
|
data/lib/sequel_enum.rb
CHANGED
@@ -2,13 +2,13 @@ module Sequel
|
|
2
2
|
module Plugins
|
3
3
|
module Enum
|
4
4
|
def self.apply(model, opts = OPTS)
|
5
|
-
model.instance_eval do
|
6
|
-
@enums = {}
|
7
|
-
end
|
8
5
|
end
|
9
6
|
|
10
7
|
module ClassMethods
|
11
|
-
|
8
|
+
|
9
|
+
def enums
|
10
|
+
@enums ||= {}
|
11
|
+
end
|
12
12
|
|
13
13
|
def enum(column, values)
|
14
14
|
if values.is_a? Hash
|
@@ -23,13 +23,11 @@ module Sequel
|
|
23
23
|
end
|
24
24
|
|
25
25
|
define_method "#{column}=" do |value|
|
26
|
-
|
27
|
-
self[column] = val && val.last
|
26
|
+
self[column] = self.class.enums[column].assoc(value&.to_sym)&.last
|
28
27
|
end
|
29
28
|
|
30
29
|
define_method "#{column}" do
|
31
|
-
|
32
|
-
val && val.first
|
30
|
+
self.class.enums[column].rassoc(self[column])&.first
|
33
31
|
end
|
34
32
|
|
35
33
|
values.each do |key, value|
|
data/lib/sequel_enum/version.rb
CHANGED
data/sequel_enum.gemspec
CHANGED
@@ -18,10 +18,12 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
22
|
+
|
21
23
|
spec.add_runtime_dependency "sequel"
|
22
24
|
|
23
25
|
spec.add_development_dependency "rake"
|
24
|
-
spec.add_development_dependency "bundler"
|
25
|
-
spec.add_development_dependency "rspec",
|
26
|
-
spec.add_development_dependency "sqlite3"
|
26
|
+
spec.add_development_dependency "bundler"
|
27
|
+
spec.add_development_dependency "rspec", ">= 3.5"
|
28
|
+
spec.add_development_dependency "sqlite3"
|
27
29
|
end
|
data/spec/sequel_enum_spec.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
plugin :enum
|
5
|
-
end
|
6
|
-
|
7
|
-
describe "sequel_enum" do
|
3
|
+
RSpec.describe "sequel_enum" do
|
8
4
|
let(:item) { Item.new }
|
9
5
|
|
10
6
|
specify "class should provide reflection" do
|
@@ -12,6 +8,11 @@ describe "sequel_enum" do
|
|
12
8
|
expect(Item.enums).to eq({ condition: { :mint => 0, :very_good => 1, :fair => 2}})
|
13
9
|
end
|
14
10
|
|
11
|
+
specify "inheriting from abstract model should provide reflection" do
|
12
|
+
RealModel.enum :condition, [:mint, :very_good, :fair]
|
13
|
+
expect(RealModel.enums).to eq({ condition: { :mint => 0, :very_good => 1, :fair => 2}})
|
14
|
+
end
|
15
|
+
|
15
16
|
specify "it accepts an array of symbols" do
|
16
17
|
expect{
|
17
18
|
Item.enum :condition, [:mint, :very_good, :good, :poor]
|
@@ -71,6 +72,11 @@ describe "sequel_enum" do
|
|
71
72
|
item.condition = :mint
|
72
73
|
expect(item[:condition]).to be 0
|
73
74
|
end
|
75
|
+
|
76
|
+
it "should set column to the blank value" do
|
77
|
+
item.condition = nil
|
78
|
+
expect(item[:condition]).to be nil
|
79
|
+
end
|
74
80
|
end
|
75
81
|
|
76
82
|
context "with an invalid value" do
|
data/spec/spec_helper.rb
CHANGED
@@ -9,12 +9,22 @@ RSpec.configure do |config|
|
|
9
9
|
config.order = 'random'
|
10
10
|
|
11
11
|
config.before(:suite) do
|
12
|
+
AbstractModel = Class.new(Sequel::Model)
|
13
|
+
AbstractModel.require_valid_table = false
|
14
|
+
AbstractModel.plugin :enum
|
15
|
+
|
16
|
+
class RealModel < AbstractModel; end
|
17
|
+
|
12
18
|
DB.create_table :items do
|
13
19
|
primary_key :id
|
14
20
|
column :name, String
|
15
21
|
column :condition, Integer
|
16
22
|
column :edition, Integer
|
17
23
|
end
|
24
|
+
|
25
|
+
class Item < Sequel::Model
|
26
|
+
plugin :enum
|
27
|
+
end
|
18
28
|
end
|
19
29
|
|
20
30
|
config.after(:suite) do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adrià Planas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -42,44 +42,44 @@ dependencies:
|
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '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
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '3.5'
|
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: '3.5'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sqlite3
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '0'
|
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
|
-
version: '
|
82
|
+
version: '0'
|
83
83
|
description:
|
84
84
|
email:
|
85
85
|
- adriaplanas@edgecodeworks.com
|
@@ -110,15 +110,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
110
|
requirements:
|
111
111
|
- - ">="
|
112
112
|
- !ruby/object:Gem::Version
|
113
|
-
version:
|
113
|
+
version: 2.3.0
|
114
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
115
|
requirements:
|
116
116
|
- - ">="
|
117
117
|
- !ruby/object:Gem::Version
|
118
118
|
version: '0'
|
119
119
|
requirements: []
|
120
|
-
|
121
|
-
rubygems_version: 2.6.10
|
120
|
+
rubygems_version: 3.1.4
|
122
121
|
signing_key:
|
123
122
|
specification_version: 4
|
124
123
|
summary: A Sequel plugin that provides enum-like functionality to your models
|