sequel_enum 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/lib/sequel_enum.rb +9 -10
- data/lib/sequel_enum/version.rb +1 -1
- data/sequel_enum.gemspec +5 -3
- data/spec/sequel_enum_spec.rb +33 -9
- data/spec/spec_helper.rb +1 -1
- metadata +9 -10
- data/test.db +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '09d1193b9cc43bff32889c4ab0f3aea15a44c2de'
|
4
|
+
data.tar.gz: af7d9423ae0458790b4433b8c1f72e00bc5876a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f757ee8c49dae3678ee838f180eaca8e81c14fc60fae40372bfc238a4369b266bbbb94d63425777b5102f4e1133e9108c3e457fd11e1a7025423cd4ca55278d2
|
7
|
+
data.tar.gz: 5ed11d2377ccbad5282d5f57c88e0104a53c61735af225ff25502062e035d52b54fdd338662b16649c5339c1fa3263a2c441879e10937b1bc314eb3af098599a
|
data/.gitignore
CHANGED
data/lib/sequel_enum.rb
CHANGED
@@ -13,31 +13,30 @@ module Sequel
|
|
13
13
|
def enum(column, values)
|
14
14
|
if values.is_a? Hash
|
15
15
|
values.each do |key,val|
|
16
|
-
raise ArgumentError, "index should be
|
17
|
-
raise ArgumentError "value should be
|
16
|
+
raise ArgumentError, "index should be a symbol, #{key} provided which it's a #{key.class}" unless key.is_a? Symbol
|
17
|
+
raise ArgumentError, "value should be an integer, #{val} provided which it's a #{val.class}" unless val.is_a? Integer
|
18
18
|
end
|
19
19
|
elsif values.is_a? Array
|
20
|
-
values = Hash[values.map.with_index { |v, i| [i
|
20
|
+
values = Hash[values.map.with_index { |v, i| [v, i] }]
|
21
21
|
else
|
22
|
-
raise ArgumentError, "#enum expects the second argument to be an array of symbols or a hash like {
|
22
|
+
raise ArgumentError, "#enum expects the second argument to be an array of symbols or a hash like { :symbol => integer }"
|
23
23
|
end
|
24
24
|
|
25
25
|
define_method "#{column}=" do |value|
|
26
|
-
|
27
|
-
self[column] = index && index.first
|
26
|
+
self[column] = self.class.enums[column].assoc(value.to_sym)&.last
|
28
27
|
end
|
29
28
|
|
30
29
|
define_method "#{column}" do
|
31
|
-
self.class.enums[column].
|
30
|
+
self.class.enums[column].rassoc(self[column])&.first
|
32
31
|
end
|
33
32
|
|
34
33
|
values.each do |key, value|
|
35
|
-
define_method "#{
|
36
|
-
self.send(column) ==
|
34
|
+
define_method "#{key}?" do
|
35
|
+
self.send(column) == key
|
37
36
|
end
|
38
37
|
end
|
39
38
|
|
40
|
-
enums[column] = values
|
39
|
+
self.enums[column] = values
|
41
40
|
end
|
42
41
|
end
|
43
42
|
end
|
data/lib/sequel_enum/version.rb
CHANGED
data/sequel_enum.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.name = "sequel_enum"
|
9
9
|
spec.version = SequelEnum::VERSION
|
10
10
|
spec.authors = ["Adrià Planas"]
|
11
|
-
spec.email = ["adriaplanas@
|
11
|
+
spec.email = ["adriaplanas@edgecodeworks.com"]
|
12
12
|
spec.summary = %q{A Sequel plugin that provides enum-like functionality to your models}
|
13
13
|
spec.homepage = "https://github.com/planas/sequel_enum"
|
14
14
|
spec.license = "MIT"
|
@@ -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 = '>= 2.3'
|
22
|
+
|
21
23
|
spec.add_runtime_dependency "sequel"
|
22
24
|
|
23
25
|
spec.add_development_dependency "rake"
|
24
|
-
spec.add_development_dependency "bundler", "~> 1.
|
25
|
-
spec.add_development_dependency "rspec", "~>
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.5"
|
26
28
|
spec.add_development_dependency "sqlite3", "~> 1.3"
|
27
29
|
end
|
data/spec/sequel_enum_spec.rb
CHANGED
@@ -9,7 +9,7 @@ describe "sequel_enum" do
|
|
9
9
|
|
10
10
|
specify "class should provide reflection" do
|
11
11
|
Item.enum :condition, [:mint, :very_good, :fair]
|
12
|
-
Item.enums.
|
12
|
+
expect(Item.enums).to eq({ condition: { :mint => 0, :very_good => 1, :fair => 2}})
|
13
13
|
end
|
14
14
|
|
15
15
|
specify "it accepts an array of symbols" do
|
@@ -20,13 +20,13 @@ describe "sequel_enum" do
|
|
20
20
|
|
21
21
|
specify "it accepts a hash of index => value" do
|
22
22
|
expect{
|
23
|
-
Item.enum :condition,
|
23
|
+
Item.enum :condition, :mint => 0, :very_good => 1, :good => 2, :poor => 3
|
24
24
|
}.not_to raise_error
|
25
25
|
end
|
26
26
|
|
27
27
|
specify "it rejects an invalid hash" do
|
28
28
|
expect{
|
29
|
-
Item.enum :condition, { '0'
|
29
|
+
Item.enum :condition, { :mint => '0' }
|
30
30
|
}.to raise_error(ArgumentError)
|
31
31
|
end
|
32
32
|
|
@@ -39,20 +39,44 @@ describe "sequel_enum" do
|
|
39
39
|
describe "methods" do
|
40
40
|
before(:all) do
|
41
41
|
Item.enum :condition, [:mint, :very_good, :good, :poor]
|
42
|
+
Item.enum :edition, [:first, :second, :rare, :other]
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#initialize_set" do
|
46
|
+
it "handles multiple enums" do
|
47
|
+
i = Item.create(:condition => :mint, :edition => :first)
|
48
|
+
|
49
|
+
expect(i[:condition]).to eq 0
|
50
|
+
expect(i[:edition]).to eq 0
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#update" do
|
55
|
+
it "accepts strings" do
|
56
|
+
i = Item.create(:condition => "mint")
|
57
|
+
expect(i[:condition]).to eq 0
|
58
|
+
end
|
59
|
+
|
60
|
+
it "handles multiple enums" do
|
61
|
+
i = Item.create(:condition => :mint, :edition => :first)
|
62
|
+
i.update(:edition => :second)
|
63
|
+
|
64
|
+
expect(i[:edition]).to eq 1
|
65
|
+
end
|
42
66
|
end
|
43
67
|
|
44
68
|
describe "#column=" do
|
45
69
|
context "with a valid value" do
|
46
70
|
it "should set column to the value index" do
|
47
71
|
item.condition = :mint
|
48
|
-
item[:condition].
|
72
|
+
expect(item[:condition]).to be 0
|
49
73
|
end
|
50
74
|
end
|
51
75
|
|
52
76
|
context "with an invalid value" do
|
53
77
|
it "should set column to nil" do
|
54
78
|
item.condition = :fair
|
55
|
-
item[:condition].
|
79
|
+
expect(item[:condition]).to be_nil
|
56
80
|
end
|
57
81
|
end
|
58
82
|
end
|
@@ -61,14 +85,14 @@ describe "sequel_enum" do
|
|
61
85
|
context "with a valid index stored on the column" do
|
62
86
|
it "should return its matching value" do
|
63
87
|
item[:condition] = 1
|
64
|
-
item.condition.
|
88
|
+
expect(item.condition).to be :very_good
|
65
89
|
end
|
66
90
|
end
|
67
91
|
|
68
92
|
context "with an invalid index stored on the column" do
|
69
93
|
it "should return nil" do
|
70
94
|
item[:condition] = 10
|
71
|
-
item.condition.
|
95
|
+
expect(item.condition).to be_nil
|
72
96
|
end
|
73
97
|
end
|
74
98
|
end
|
@@ -77,14 +101,14 @@ describe "sequel_enum" do
|
|
77
101
|
context "when the actual value match" do
|
78
102
|
it "should return true" do
|
79
103
|
item.condition = :good
|
80
|
-
item.good
|
104
|
+
expect(item.good?).to be true
|
81
105
|
end
|
82
106
|
end
|
83
107
|
|
84
108
|
context "when the actual value doesn't match" do
|
85
109
|
it "should return false" do
|
86
110
|
item.condition = :mint
|
87
|
-
item.poor
|
111
|
+
expect(item.poor?).to be false
|
88
112
|
end
|
89
113
|
end
|
90
114
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -5,7 +5,6 @@ require 'sequel_enum'
|
|
5
5
|
DB = Sequel.connect('sqlite://test.db')
|
6
6
|
|
7
7
|
RSpec.configure do |config|
|
8
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
8
|
config.run_all_when_everything_filtered = true
|
10
9
|
config.order = 'random'
|
11
10
|
|
@@ -14,6 +13,7 @@ RSpec.configure do |config|
|
|
14
13
|
primary_key :id
|
15
14
|
column :name, String
|
16
15
|
column :condition, Integer
|
16
|
+
column :edition, Integer
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
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.0
|
4
|
+
version: 0.1.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: 2017-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -44,28 +44,28 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '1.
|
47
|
+
version: '1.14'
|
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: '1.
|
54
|
+
version: '1.14'
|
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
|
-
version: '
|
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
|
-
version: '
|
68
|
+
version: '3.5'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sqlite3
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -82,7 +82,7 @@ dependencies:
|
|
82
82
|
version: '1.3'
|
83
83
|
description:
|
84
84
|
email:
|
85
|
-
- adriaplanas@
|
85
|
+
- adriaplanas@edgecodeworks.com
|
86
86
|
executables: []
|
87
87
|
extensions: []
|
88
88
|
extra_rdoc_files: []
|
@@ -98,7 +98,6 @@ files:
|
|
98
98
|
- sequel_enum.gemspec
|
99
99
|
- spec/sequel_enum_spec.rb
|
100
100
|
- spec/spec_helper.rb
|
101
|
-
- test.db
|
102
101
|
homepage: https://github.com/planas/sequel_enum
|
103
102
|
licenses:
|
104
103
|
- MIT
|
@@ -111,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
111
110
|
requirements:
|
112
111
|
- - ">="
|
113
112
|
- !ruby/object:Gem::Version
|
114
|
-
version: '
|
113
|
+
version: '2.3'
|
115
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
115
|
requirements:
|
117
116
|
- - ">="
|
@@ -119,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
118
|
version: '0'
|
120
119
|
requirements: []
|
121
120
|
rubyforge_project:
|
122
|
-
rubygems_version: 2.
|
121
|
+
rubygems_version: 2.6.10
|
123
122
|
signing_key:
|
124
123
|
specification_version: 4
|
125
124
|
summary: A Sequel plugin that provides enum-like functionality to your models
|
data/test.db
DELETED
Binary file
|