sequel_enum 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 08f97db0b4aece1769c75037b639cd7d8f2c5af4
4
- data.tar.gz: 464bea4508eefc6436ca91ddfaeb242a554799f4
3
+ metadata.gz: '09d1193b9cc43bff32889c4ab0f3aea15a44c2de'
4
+ data.tar.gz: af7d9423ae0458790b4433b8c1f72e00bc5876a4
5
5
  SHA512:
6
- metadata.gz: cd69833c833f6885c9ec7e4e504cdc5e5fbae80e409d7cca3a9befc235b4095e72f5de464d873538980acd659f0b0d2455532446b779450edf62e0e62b685ec2
7
- data.tar.gz: 450fdfe9efcb8ab77832f07bc1b2fde0dea212344e01a1da65c7f2e3619323855a12b7705b6f45d512e035317ccae9257400ee554958a55e82e3e9d1ebd82a3d
6
+ metadata.gz: f757ee8c49dae3678ee838f180eaca8e81c14fc60fae40372bfc238a4369b266bbbb94d63425777b5102f4e1133e9108c3e457fd11e1a7025423cd4ca55278d2
7
+ data.tar.gz: 5ed11d2377ccbad5282d5f57c88e0104a53c61735af225ff25502062e035d52b54fdd338662b16649c5339c1fa3263a2c441879e10937b1bc314eb3af098599a
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ *.db
1
2
  *.gem
2
3
  *.rbc
3
4
  .bundle
@@ -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 numeric, #{key} provided wich it's a #{key.class}" unless key.is_a? Fixnum
17
- raise ArgumentError "value should be a symbol, #{val} provided wich it's a #{val.class}" unless val.is_a? Symbol
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,v] }]
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 { index => :value }"
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
- index = self.class.enums[column].rassoc(value)
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].fetch(self[column], nil)
30
+ self.class.enums[column].rassoc(self[column])&.first
32
31
  end
33
32
 
34
33
  values.each do |key, value|
35
- define_method "#{value}?" do
36
- self.send(column) == value
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
@@ -1,3 +1,3 @@
1
1
  module SequelEnum
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -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@liquidcodeworks.com"]
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.5"
25
- spec.add_development_dependency "rspec", "~> 2.0"
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
@@ -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.should eq({ condition: { 0 => :mint, 1 => :very_good, 2 => :fair}})
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, 0 => :mint, 1 => :very_good, 2 => :good, 3 => :poor
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' => :mint }
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].should eq 0
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].should be_nil
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.should eq :very_good
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.should be_nil
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?.should eq true
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?.should eq false
111
+ expect(item.poor?).to be false
88
112
  end
89
113
  end
90
114
  end
@@ -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.1
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: 2014-03-11 00:00:00.000000000 Z
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.5'
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.5'
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: '2.0'
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: '2.0'
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@liquidcodeworks.com
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: '0'
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.2.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