acts_as_enumeration 0.0.1 → 0.1.5

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.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in acts_as_enum.gemspec
3
+ # Specify your gem's dependencies in acts_as_enumeration.gemspec
4
4
  gemspec
data/README ADDED
@@ -0,0 +1 @@
1
+ This file was created by JetBrains RubyMine 5.4.2 for binding GitHub repository
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # ActsAsEnum
1
+ # ActsAsEnumeration
2
2
 
3
- acts_as_enumerable and its aliases acts_as_enum, enumerable_column and enum_column
3
+ acts_as_enumeration and its aliases acts_as_enumerable, acts_as_enum, enumerable_column and enum_column
4
4
  allow for unique names in a database column to behave as if they were enumerated
5
5
  types, including chaining etc.
6
6
 
@@ -8,7 +8,7 @@ types, including chaining etc.
8
8
 
9
9
  Add this line to your application's Gemfile:
10
10
 
11
- gem 'acts_as_enum'
11
+ gem 'acts_as_enumeration'
12
12
 
13
13
  And then execute:
14
14
 
@@ -16,13 +16,13 @@ And then execute:
16
16
 
17
17
  Or install it yourself as:
18
18
 
19
- $ gem install acts_as_enum
19
+ $ gem install acts_as_enumeration
20
20
 
21
21
  ## Usage
22
22
 
23
23
  So if a database table had a column called "name", declaring
24
24
 
25
- acts_as_enumerable :name
25
+ acts_as_enumeration :name
26
26
 
27
27
  would select all of the items in that column and map them in a hash to their
28
28
  primary key values.
@@ -41,8 +41,8 @@ as is_paul_or_michael_or_luke? which has the same effect as
41
41
  is_not_paul_or_michael_or_like which has the same effect as
42
42
  !(is_paul || is_michael? || is_luke?) or !is?(:paul, :michael, :luke)
43
43
 
44
- CAVEAT: due to the mechanism that the method_missing uses in is someone actually
45
- had the name "not bruce", the combinations cannot use this as the first element.
44
+ CAVEAT: Due to the mechanism that the method_missing uses, if someone actually
45
+ had the name "not bruce", the combination query cannot use this as the first element.
46
46
  i.e. "not <anything>" not just "not bruce".
47
47
 
48
48
  So a combination of is_not_bruce_or_paul? would have to be written
@@ -1,22 +1,22 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
+ require File.dirname(__FILE__) + '/lib/active_record/acts/enumeration'
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'acts_as_enumeration/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = 'acts_as_enumeration'
8
- spec.version = ActsAsEnumeration::VERSION
9
- spec.authors = ['Paul McKibbin']
10
- spec.email = ['pmckibbin@gmail.com']
7
+ spec.name = 'acts_as_enumeration'
8
+ spec.version = ActiveRecord::Acts::Enumeration::VERSION
9
+ spec.authors = ['Paul McKibbin']
10
+ spec.email = ['pmckibbin@gmail.com']
11
11
 
12
- spec.description = %q(Active Record extender to enable the selection of a database column and have all of it's values enumerable)
13
- spec.summary = %q(enumerable values from database columns)
14
- spec.homepage = "http://blackrat.org"
15
- spec.license = "MIT"
12
+ spec.description = %q(Active Record extender to enable the selection of a database column and have all of it's values enumerable)
13
+ spec.summary = %q(enumerable values from database columns)
14
+ spec.homepage = "http://blackrat.org"
15
+ spec.license = "MIT"
16
16
 
17
- spec.files = `git ls-files`.split($/)
18
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = ['lib']
21
21
 
22
22
  spec.add_development_dependency 'bundler', '~> 1.3'
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'lib', 'acts_as_enumeration')
@@ -0,0 +1,107 @@
1
+ module ActiveRecord
2
+ module Acts
3
+ module Enumeration
4
+ VERSION='0.1.5'
5
+ class << self
6
+ def included(base)
7
+ base.class_eval do
8
+ extend ClassMethods
9
+ end
10
+ end
11
+ end
12
+
13
+ def method_missing(method_id, *args, &block)
14
+ method_name=method_id.to_s
15
+ if match_data=method_name.match(/^(is(?:_not)*)(\?|_(\w*)\?)/)
16
+ method=match_data[1] << "?"
17
+ new_args= ((match_data[2]=='?') ? args : match_data[3].split(/_or_/).map { |x| x.intern })
18
+ respond_to?(method) ? send(method, *new_args, &block) : false
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+
25
+ module ClassMethods
26
+ def normalize(field)
27
+ field.to_s.gsub(/[\\W]+/, ' ').strip.gsub(/\s+/, '_').underscore
28
+ end
29
+
30
+ def acts_as_enumeration(*opts)
31
+ opts.each do |field|
32
+ klass=self
33
+ (
34
+ class<<self;
35
+ self;
36
+ end).class_eval do
37
+ define_method "enum_#{field}" do
38
+ instance_variable_get("@enum_#{field}") || instance_variable_set("@enum_#{field}", HashWithIndifferentAccess[*klass.all.map { |x| [x.send(field).gsub(/[\\W]+/, ' ').strip.gsub(/\s+/, '_').underscore, x.send(x.class.primary_key)] }.flatten])
39
+ end
40
+
41
+ define_method :as_key do |value|
42
+ return '' unless exists?(value)
43
+ klass.find(value).as_key
44
+ end
45
+
46
+ alias_method :as_symbol, :as_key
47
+
48
+ define_method "valid_#{field}?" do |value|
49
+ send("enum_#{field}").has_key?(value)
50
+ end
51
+
52
+ define_method "id_for_#{field}" do |value|
53
+ send("enum_#{field}")[value]
54
+ end
55
+
56
+ define_method "for_#{field}" do |value|
57
+ klass.find(send("id_for_#{field}", value))
58
+ end
59
+ end
60
+
61
+ define_method 'is?' do |*types|
62
+ types.any? { |x| send(self.class.primary_key)==self.class.send("enum_#{field}")[x] }
63
+ end
64
+
65
+ define_method 'is_not?' do |*types|
66
+ !is?(*types)
67
+ end
68
+
69
+ define_method :as_key do
70
+ self.class.normalize(send(field)).intern
71
+ end
72
+
73
+ alias_method :as_symbol, :as_key
74
+
75
+ all.map { |x| normalize(x.send(field)) }.each do |y|
76
+
77
+ define_method "is_#{y}?" do
78
+ is?("#{y}")
79
+ end
80
+
81
+ identifier = y.to_s=~/^[a-z_]/ ? y.to_s : "_#{y.to_s}"
82
+
83
+ define_method "#{identifier}?" do
84
+ is?("#{y}")
85
+ end
86
+
87
+ (
88
+ class << self;
89
+ self;
90
+ end).class_eval do
91
+ define_method identifier do
92
+ self.send("for_#{field}", y)
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ [:acts_as_enum, :enum_column, :enumerable_column, :acts_as_enumerable].each do |aliased|
100
+ unless defined?(aliased)
101
+ alias_method :aliased, :acts_as_enumeration
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -1,6 +1,2 @@
1
- require 'acts_as_enumeration/version'
2
- require 'acts_as_enumeration/enumerable'
3
-
4
- class ActiveRecord::Base
5
- extend ActsAsEnumeration
6
- end
1
+ require_relative File.join('active_record', 'acts', 'enumeration.rb')
2
+ ActiveRecord::Base.send :include, ActiveRecord::Acts::Enumeration
@@ -0,0 +1,80 @@
1
+ require 'rubygems'
2
+ if RUBY_VERSION >= '1.9'
3
+ require 'minitest/autorun'
4
+ require 'active_record'
5
+ else
6
+ require 'test/unit'
7
+ require 'activerecord'
8
+ require 'sqlite3'
9
+ end
10
+ $:.unshift File.dirname(__FILE__) + '/../lib'
11
+ require File.dirname(__FILE__) + '/../acts_as_enumeration'
12
+
13
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
14
+
15
+ def setup_db
16
+ ActiveRecord::Base.logger
17
+ ActiveRecord::Schema.define(:version => 1) do
18
+ create_table :enumerates do |t|
19
+ t.column :type, :string
20
+ t.column :name, :string
21
+ t.column :description, :string
22
+ end
23
+ end
24
+ end
25
+
26
+ def teardown_db
27
+ ActiveRecord::Base.connection.tables.each do |table|
28
+ ActiveRecord::Base.connection.drop_table(table)
29
+ end
30
+ end
31
+
32
+ class Enumerate < ActiveRecord::Base;
33
+ end
34
+ class EnumerateAll < Enumerate;
35
+ end
36
+
37
+ class EnumerateTest < (
38
+ begin
39
+ MiniTest::Test rescue Test::Unit::TestCase
40
+ end)
41
+ def setup
42
+ setup_db
43
+ [EnumerateAll].each do |klass|
44
+ klass.create! :name => 'first', :description => 'First field'
45
+ klass.create! :name => 'second', :description => 'Second field'
46
+ klass.create! :name => 'third', :description => 'Third field'
47
+ klass.create! :name => 'forth', :description => 'Forth field'
48
+ klass.create! :name => 'fifth', :description => 'Fifth field'
49
+ klass.create! :name => 'sixth', :description => 'Sixth field'
50
+ end
51
+ Enumerate.acts_as_enumeration :description
52
+ end
53
+
54
+ def teardown
55
+ teardown_db
56
+ end
57
+
58
+ def test_basics
59
+ assert Enumerate.first.is?(:first_field)
60
+ assert Enumerate.first.is?('first_field')
61
+ assert Enumerate.first.is_not?(:second_field)
62
+ assert Enumerate.first.is?(:first_field, :second_field)
63
+ assert Enumerate.first.is_not?(:second_field, :third_field)
64
+ assert Enumerate.first.is_first_field?
65
+ assert Enumerate.first.is_not_second_field?
66
+ assert Enumerate.first.is_first_field_or_second_field?
67
+ assert Enumerate.second_field.is_first_field_or_second_field?
68
+ assert Enumerate.second_field.is_not_first_field?
69
+ assert Enumerate.first.first_field?
70
+ assert_equal Enumerate.first, Enumerate.first_field
71
+ assert_equal(:first_field, Enumerate.first.as_key)
72
+ assert_equal(:first_field, Enumerate.as_key(Enumerate.first.id))
73
+ assert Enumerate.valid_description?(:first_field)
74
+ assert Enumerate.valid_description?('first_field')
75
+ assert !Enumerate.valid_description?(:blah_blah_field)
76
+ assert !Enumerate.valid_description?('blah_blah_field')
77
+ assert Enumerate.first.id, Enumerate.id_for_description(:first_field)
78
+ end
79
+ end
80
+
metadata CHANGED
@@ -1,88 +1,104 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: acts_as_enumeration
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.1
3
+ version: !ruby/object:Gem::Version
4
+ hash: 17
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 5
10
+ version: 0.1.5
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Paul McKibbin
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2013-07-02 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2016-03-18 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: bundler
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: '1.3'
22
- type: :development
23
22
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
23
+ requirement: &id001 !ruby/object:Gem::Requirement
25
24
  none: false
26
- requirements:
25
+ requirements:
27
26
  - - ~>
28
- - !ruby/object:Gem::Version
29
- version: '1.3'
30
- - !ruby/object:Gem::Dependency
31
- name: rake
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
27
+ - !ruby/object:Gem::Version
28
+ hash: 9
29
+ segments:
30
+ - 1
31
+ - 3
32
+ version: "1.3"
38
33
  type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rake
39
37
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
38
+ requirement: &id002 !ruby/object:Gem::Requirement
41
39
  none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
46
- description: Active Record extender to enable the selection of a database column and
47
- have all of it's values enumerable
48
- email:
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: Active Record extender to enable the selection of a database column and have all of it's values enumerable
50
+ email:
49
51
  - pmckibbin@gmail.com
50
52
  executables: []
53
+
51
54
  extensions: []
55
+
52
56
  extra_rdoc_files: []
53
- files:
57
+
58
+ files:
54
59
  - .gitignore
55
60
  - Gemfile
56
61
  - LICENSE.txt
62
+ - README
57
63
  - README.md
58
64
  - Rakefile
59
65
  - acts_as_enumeration.gemspec
66
+ - init.rb
67
+ - lib/active_record/acts/enumeration.rb
60
68
  - lib/acts_as_enumeration.rb
61
- - lib/acts_as_enumeration/enumerable.rb
62
- - lib/acts_as_enumeration/version.rb
69
+ - test/enumeration_test.rb
63
70
  homepage: http://blackrat.org
64
- licenses:
71
+ licenses:
65
72
  - MIT
66
73
  post_install_message:
67
74
  rdoc_options: []
68
- require_paths:
75
+
76
+ require_paths:
69
77
  - lib
70
- required_ruby_version: !ruby/object:Gem::Requirement
78
+ required_ruby_version: !ruby/object:Gem::Requirement
71
79
  none: false
72
- requirements:
73
- - - ! '>='
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
88
  none: false
78
- requirements:
79
- - - ! '>='
80
- - !ruby/object:Gem::Version
81
- version: '0'
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
82
96
  requirements: []
97
+
83
98
  rubyforge_project:
84
- rubygems_version: 1.8.25
99
+ rubygems_version: 1.8.15
85
100
  signing_key:
86
101
  specification_version: 3
87
102
  summary: enumerable values from database columns
88
- test_files: []
103
+ test_files:
104
+ - test/enumeration_test.rb
@@ -1,100 +0,0 @@
1
- module ActsAsEnumeration
2
- def acts_as_enumerable(*args)
3
- args.each do |column_name|
4
-
5
- class_eval(%Q(
6
- unless defined? @@enum_#{column_name}
7
- @@enum_#{column_name}=Hash[*all.map{|x| [x.send(column_name).gsub(/[\\W]+/,' ').strip.gsub(/\s+/,'_').underscore.intern,x.send(x.class.primary_key)]}.flatten]
8
- end
9
- ), __FILE__, __LINE__+1)
10
-
11
- all.map { |x| x.send(column_name).gsub(/[\W]+/, ' ').strip.gsub(/\s+/, '_').underscore.intern }.each do |y|
12
- class_eval(%Q(
13
- def is_#{y}?
14
- is?("#{y}".intern)
15
- end
16
- ), __FILE__, __LINE__+1)
17
-
18
- identifier = y.to_s=~/^[a-z_]/ ? y.to_s : "_#{y.to_s}"
19
- class_eval(%Q(
20
- def #{identifier}?
21
- is_#{y}?
22
- end
23
- ), __FILE__, __LINE__+1)
24
-
25
- class_eval(%Q(
26
- def self.#{identifier}
27
- for_#{column_name}("#{y}".intern)
28
- end
29
- ), __FILE__, __LINE__+1)
30
- end
31
-
32
- class_eval(%Q(
33
- def self.enum_#{column_name}
34
- @@enum_#{column_name}
35
- end
36
- ), __FILE__, __LINE__+1)
37
-
38
- class_eval(%Q(
39
- def self.as_key(value)
40
- return nil unless exists?(value)
41
- find(value).as_key
42
- end
43
- ), __FILE__, __LINE__+1)
44
-
45
- class_eval(%Q(
46
- def self.valid_#{column_name}?(value)
47
- @@enum_#{column_name}.keys.include?(value)
48
- end
49
- ), __FILE__, __LINE__+1)
50
-
51
- class_eval(%Q(
52
- def self.id_for_#{column_name}(value)
53
- @@enum_#{column_name}[value]
54
- end
55
- ), __FILE__, __LINE__+1)
56
-
57
- class_eval(%Q(
58
- def self.for_#{column_name}(value)
59
- find(id_for_#{column_name}(value))
60
- end
61
- ), __FILE__, __LINE__+1)
62
-
63
- class_eval(%Q(
64
- def is?(*types)
65
- types.any?{|x| send(self.class.primary_key)==self.class.enum_#{column_name}[x]}
66
- end
67
- ), __FILE__, __LINE__+1)
68
-
69
- class_eval(%Q(
70
- def is_not?(*types)
71
- !is?(*types)
72
- end
73
- ), __FILE__, __LINE__+1)
74
-
75
- class_eval(%Q(
76
- def as_key
77
- #{column_name}.gsub(/[\\W]+/,' ').strip.gsub(/\s+/,'_').underscore.intern
78
- end
79
- alias_method :as_symbol, :as_key
80
- ))
81
-
82
- class_eval(%Q(
83
- def method_missing(method_id, *args, &block)
84
- method_name=method_id.to_s
85
- if match_data=method_name.match(/^(is[_not]*)(\\?|_(\\w*)\\?)/)
86
- method=match_data[1] << "?"
87
- new_args= ((match_data[2]=='?') ? args : match_data[3].split(/_or_/).map{|x| x.intern})
88
- respond_to?(method) ? send(method,*new_args,&block) : false
89
- else
90
- super
91
- end
92
- end
93
- ), __FILE__, __LINE__+1)
94
- end
95
- end
96
-
97
- alias_method :acts_as_enumeration, :acts_as_enumerable
98
- alias_method :enumerable_column, :acts_as_enumerable
99
- alias_method :enum_column, :acts_as_enumerable
100
- end
@@ -1,3 +0,0 @@
1
- module ActsAsEnumeration
2
- VERSION = "0.0.1"
3
- end