sequel_enum 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 08f97db0b4aece1769c75037b639cd7d8f2c5af4
4
+ data.tar.gz: 464bea4508eefc6436ca91ddfaeb242a554799f4
5
+ SHA512:
6
+ metadata.gz: cd69833c833f6885c9ec7e4e504cdc5e5fbae80e409d7cca3a9befc235b4095e72f5de464d873538980acd659f0b0d2455532446b779450edf62e0e62b685ec2
7
+ data.tar.gz: 450fdfe9efcb8ab77832f07bc1b2fde0dea212344e01a1da65c7f2e3619323855a12b7705b6f45d512e035317ccae9257400ee554958a55e82e3e9d1ebd82a3d
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sequel_enum.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Adrià Planas
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # Sequel_enum
2
+
3
+ A Sequel plugin that provides enum-like functionality to your models.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sequel_enum'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sequel_enum
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ class Item < Sequel::Model
23
+ plugin :enum
24
+ enum :condition, [:mint, :good, :poor]
25
+ end
26
+
27
+ item = Item.new
28
+ item.condition = :mint
29
+
30
+ item.condition #> :mint
31
+ item.mint? #> true
32
+ item.good? #> false
33
+ ```
34
+
35
+ ```#enum``` accepts a hash like ```{ index => value }``` as well:
36
+
37
+ ```ruby
38
+ class Item < Sequel::Model
39
+ plugin :enum
40
+ enum :condition, {10 => :mint, 11 => :good, 15 => :poor}
41
+ end
42
+
43
+ item = Item.new
44
+ item.condition = :mint
45
+
46
+ item[:condition] #> 10
47
+ ```
48
+
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it ( http://github.com/planas/sequel_enum/fork )
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,45 @@
1
+ module Sequel
2
+ module Plugins
3
+ module Enum
4
+ def self.apply(model, opts = OPTS)
5
+ model.instance_eval do
6
+ @enums = {}
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ attr_reader :enums
12
+
13
+ def enum(column, values)
14
+ if values.is_a? Hash
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
18
+ end
19
+ elsif values.is_a? Array
20
+ values = Hash[values.map.with_index { |v, i| [i,v] }]
21
+ else
22
+ raise ArgumentError, "#enum expects the second argument to be an array of symbols or a hash like { index => :value }"
23
+ end
24
+
25
+ define_method "#{column}=" do |value|
26
+ index = self.class.enums[column].rassoc(value)
27
+ self[column] = index && index.first
28
+ end
29
+
30
+ define_method "#{column}" do
31
+ self.class.enums[column].fetch(self[column], nil)
32
+ end
33
+
34
+ values.each do |key, value|
35
+ define_method "#{value}?" do
36
+ self.send(column) == value
37
+ end
38
+ end
39
+
40
+ enums[column] = values
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module SequelEnum
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'sequel_enum/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "sequel_enum"
9
+ spec.version = SequelEnum::VERSION
10
+ spec.authors = ["Adrià Planas"]
11
+ spec.email = ["adriaplanas@liquidcodeworks.com"]
12
+ spec.summary = %q{A Sequel plugin that provides enum-like functionality to your models}
13
+ spec.homepage = "https://github.com/planas/sequel_enum"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "sequel"
22
+
23
+ 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 "sqlite3", "~> 1.3"
27
+ end
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ class Item < Sequel::Model
4
+ plugin :enum
5
+ end
6
+
7
+ describe "sequel_enum" do
8
+ let(:item) { Item.new }
9
+
10
+ specify "class should provide reflection" do
11
+ Item.enum :condition, [:mint, :very_good, :fair]
12
+ Item.enums.should eq({ condition: { 0 => :mint, 1 => :very_good, 2 => :fair}})
13
+ end
14
+
15
+ specify "it accepts an array of symbols" do
16
+ expect{
17
+ Item.enum :condition, [:mint, :very_good, :good, :poor]
18
+ }.not_to raise_error
19
+ end
20
+
21
+ specify "it accepts a hash of index => value" do
22
+ expect{
23
+ Item.enum :condition, 0 => :mint, 1 => :very_good, 2 => :good, 3 => :poor
24
+ }.not_to raise_error
25
+ end
26
+
27
+ specify "it rejects an invalid hash" do
28
+ expect{
29
+ Item.enum :condition, { '0' => :mint }
30
+ }.to raise_error(ArgumentError)
31
+ end
32
+
33
+ specify "it rejects when it's not an array or hash" do
34
+ expect{
35
+ Item.enum :condition, 'whatever'
36
+ }.to raise_error(ArgumentError)
37
+ end
38
+
39
+ describe "methods" do
40
+ before(:all) do
41
+ Item.enum :condition, [:mint, :very_good, :good, :poor]
42
+ end
43
+
44
+ describe "#column=" do
45
+ context "with a valid value" do
46
+ it "should set column to the value index" do
47
+ item.condition = :mint
48
+ item[:condition].should eq 0
49
+ end
50
+ end
51
+
52
+ context "with an invalid value" do
53
+ it "should set column to nil" do
54
+ item.condition = :fair
55
+ item[:condition].should be_nil
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "#column" do
61
+ context "with a valid index stored on the column" do
62
+ it "should return its matching value" do
63
+ item[:condition] = 1
64
+ item.condition.should eq :very_good
65
+ end
66
+ end
67
+
68
+ context "with an invalid index stored on the column" do
69
+ it "should return nil" do
70
+ item[:condition] = 10
71
+ item.condition.should be_nil
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "#column?" do
77
+ context "when the actual value match" do
78
+ it "should return true" do
79
+ item.condition = :good
80
+ item.good?.should eq true
81
+ end
82
+ end
83
+
84
+ context "when the actual value doesn't match" do
85
+ it "should return false" do
86
+ item.condition = :mint
87
+ item.poor?.should eq false
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,23 @@
1
+ require 'sequel'
2
+ require 'sqlite3'
3
+ require 'sequel_enum'
4
+
5
+ DB = Sequel.connect('sqlite://test.db')
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.order = 'random'
11
+
12
+ config.before(:suite) do
13
+ DB.create_table :items do
14
+ primary_key :id
15
+ column :name, String
16
+ column :condition, Integer
17
+ end
18
+ end
19
+
20
+ config.after(:suite) do
21
+ DB.drop_table :items
22
+ end
23
+ end
data/test.db ADDED
Binary file
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel_enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adrià Planas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ description:
84
+ email:
85
+ - adriaplanas@liquidcodeworks.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/sequel_enum.rb
97
+ - lib/sequel_enum/version.rb
98
+ - sequel_enum.gemspec
99
+ - spec/sequel_enum_spec.rb
100
+ - spec/spec_helper.rb
101
+ - test.db
102
+ homepage: https://github.com/planas/sequel_enum
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: A Sequel plugin that provides enum-like functionality to your models
126
+ test_files:
127
+ - spec/sequel_enum_spec.rb
128
+ - spec/spec_helper.rb