acts_as_enumeration 0.0.1

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/.gitignore ADDED
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_enum.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Paul McKibbin
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.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # ActsAsEnum
2
+
3
+ acts_as_enumerable and its aliases acts_as_enum, enumerable_column and enum_column
4
+ allow for unique names in a database column to behave as if they were enumerated
5
+ types, including chaining etc.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'acts_as_enum'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install acts_as_enum
20
+
21
+ ## Usage
22
+
23
+ So if a database table had a column called "name", declaring
24
+
25
+ acts_as_enumerable :name
26
+
27
+ would select all of the items in that column and map them in a hash to their
28
+ primary key values.
29
+
30
+ In addition, it creates the instance methods is_#{key} where key is all of the
31
+ values in that column, is?(array) and is_not?(array) that check for type within
32
+ that array.
33
+
34
+ Keeping name as our column example, the class methods id_for_name will return
35
+ the primary key value for that mapping and valid_name?(value) will say if the
36
+ table contains and entry in the name column with that value.
37
+
38
+ Finally, the method missing allows for is_ and is_not chaining of methods, such
39
+ as is_paul_or_michael_or_luke? which has the same effect as
40
+ (is_paul? || is_michael? || is_luke?) or is?(:paul, :michael, :luke) and
41
+ is_not_paul_or_michael_or_like which has the same effect as
42
+ !(is_paul || is_michael? || is_luke?) or !is?(:paul, :michael, :luke)
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.
46
+ i.e. "not <anything>" not just "not bruce".
47
+
48
+ So a combination of is_not_bruce_or_paul? would have to be written
49
+ is_paul_or_not_bruce? to have the desired effect. is_not_not_bruce_or_paul
50
+ is fine, but the chances of this happening are about as likely as someone actually
51
+ called "not bruce" outside of a Monty Python sketch.
52
+
53
+ (Which means it probably will, so before you change code to support a new entry,
54
+ try just changing the order)
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_enumeration/version'
5
+
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']
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"
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)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.3'
23
+ spec.add_development_dependency 'rake'
24
+ end
@@ -0,0 +1,100 @@
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
@@ -0,0 +1,3 @@
1
+ module ActsAsEnumeration
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require 'acts_as_enumeration/version'
2
+ require 'acts_as_enumeration/enumerable'
3
+
4
+ class ActiveRecord::Base
5
+ extend ActsAsEnumeration
6
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_enumeration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul McKibbin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ 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
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
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'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ 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:
49
+ - pmckibbin@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - acts_as_enumeration.gemspec
60
+ - lib/acts_as_enumeration.rb
61
+ - lib/acts_as_enumeration/enumerable.rb
62
+ - lib/acts_as_enumeration/version.rb
63
+ homepage: http://blackrat.org
64
+ licenses:
65
+ - MIT
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.25
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: enumerable values from database columns
88
+ test_files: []