legacy_enum 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/MIT-LICENSE +20 -0
- data/README.rdoc +5 -0
- data/Rakefile +27 -0
- data/lib/legacy_enum.rb +6 -0
- data/lib/legacy_enum/class_methods.rb +87 -0
- data/lib/legacy_enum/method_definition_dsl.rb +17 -0
- data/lib/legacy_enum/version.rb +3 -0
- data/lib/tasks/legacy_enum_tasks.rake +4 -0
- metadata +87 -0
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright 2012 YOURNAME
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env rake
|
|
2
|
+
begin
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
rescue LoadError
|
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
|
6
|
+
end
|
|
7
|
+
begin
|
|
8
|
+
require 'rdoc/task'
|
|
9
|
+
rescue LoadError
|
|
10
|
+
require 'rdoc/rdoc'
|
|
11
|
+
require 'rake/rdoctask'
|
|
12
|
+
RDoc::Task = Rake::RDocTask
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
17
|
+
rdoc.title = 'LegacyEnum'
|
|
18
|
+
rdoc.options << '--line-numbers'
|
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
Bundler::GemHelper.install_tasks
|
|
27
|
+
|
data/lib/legacy_enum.rb
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
module LegacyEnum
|
|
2
|
+
module ClassMethods
|
|
3
|
+
def legacy_enum(name, *options, &block)
|
|
4
|
+
values_name = "@@#{name}_values".downcase
|
|
5
|
+
options = options.extract_options!
|
|
6
|
+
id_attr_name = options[:lookup].try(:to_s) || "#{name.to_s.capitalize}ID"
|
|
7
|
+
|
|
8
|
+
config = MethodDefinitionDSL.new
|
|
9
|
+
config.instance_eval(&block)
|
|
10
|
+
|
|
11
|
+
cattr_accessor :enum_config unless defined? self.enum_config
|
|
12
|
+
self.enum_config ||= {}
|
|
13
|
+
self.enum_config[name] = config.enum_def
|
|
14
|
+
|
|
15
|
+
#TODO make more lightweight by conditionally adding these methods on first use via method_missing
|
|
16
|
+
class_eval do
|
|
17
|
+
if options[:scope]
|
|
18
|
+
scope name.to_sym, lambda { |enum_val|
|
|
19
|
+
{ :conditions => { id_attr_name.to_sym => enums[name.to_sym][enum_val] } }
|
|
20
|
+
}
|
|
21
|
+
self.enums[name].keys.each do |value|
|
|
22
|
+
if options[:scope] == :one
|
|
23
|
+
(class << self; self; end).instance_eval do
|
|
24
|
+
define_method value.to_sym, lambda {
|
|
25
|
+
send(name.to_sym, value.to_sym).first
|
|
26
|
+
}
|
|
27
|
+
end
|
|
28
|
+
else
|
|
29
|
+
(class << self; self; end).instance_eval do
|
|
30
|
+
define_method value.to_sym, lambda {
|
|
31
|
+
send(name.to_sym, value.to_sym)
|
|
32
|
+
}
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
find_enum_entry = Proc.new do |_self, name, sym|
|
|
39
|
+
enum_entry = _self.enum_config[name].find do |hash|
|
|
40
|
+
attr_value = _self.send(id_attr_name.to_sym)
|
|
41
|
+
attr_value.to_s.upcase == hash[:value].to_s.upcase
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
enum_entry[sym] unless enum_entry.blank?
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
define_method name do
|
|
48
|
+
find_enum_entry.call(self, name, :name)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
define_method "#{name}=" do |value|
|
|
52
|
+
self.send("#{id_attr_name}=".to_sym, nil) if value.blank?
|
|
53
|
+
enum_entry = self.enum_config[name].find { |hash| hash[:name] == (value.blank? ? value : value.to_sym) }
|
|
54
|
+
unless enum_entry.blank?
|
|
55
|
+
self.send("#{id_attr_name}=".to_sym, enum_entry[:value])
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
define_method "#{name}_label" do
|
|
60
|
+
find_enum_entry.call(self, name, :label)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def enums
|
|
66
|
+
inject_block(:name, :value)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def labels
|
|
70
|
+
inject_block(:name, :label)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def enum_ids
|
|
74
|
+
inject_block(:value, :name)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
#this name sux, refactor it
|
|
79
|
+
def inject_block(key, value)
|
|
80
|
+
self.enum_config.inject({}) do |acc, (name,config)|
|
|
81
|
+
acc.merge( { name => config.inject({}) do |inner_acc, enum_item|
|
|
82
|
+
inner_acc.merge( { enum_item[key] => enum_item[value] } )
|
|
83
|
+
end })
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module LegacyEnum
|
|
2
|
+
class MethodDefinitionDSL
|
|
3
|
+
attr_accessor :enum_def
|
|
4
|
+
|
|
5
|
+
def method_missing(symbol, *args)
|
|
6
|
+
@enum_def ||= []
|
|
7
|
+
|
|
8
|
+
options = args.extract_options!
|
|
9
|
+
|
|
10
|
+
options.merge! name: symbol
|
|
11
|
+
options.merge! value: args[0] unless args.empty?
|
|
12
|
+
options.merge! label: symbol.to_s.titleize unless options.keys.include?(:label)
|
|
13
|
+
|
|
14
|
+
@enum_def << options
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: legacy_enum
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Sean Scally
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-02-08 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rails
|
|
16
|
+
requirement: &70179477187020 !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>'
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 3.0.0
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: *70179477187020
|
|
25
|
+
- !ruby/object:Gem::Dependency
|
|
26
|
+
name: sqlite3
|
|
27
|
+
requirement: &70179477186600 !ruby/object:Gem::Requirement
|
|
28
|
+
none: false
|
|
29
|
+
requirements:
|
|
30
|
+
- - ! '>='
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: *70179477186600
|
|
36
|
+
- !ruby/object:Gem::Dependency
|
|
37
|
+
name: rspec
|
|
38
|
+
requirement: &70179477186140 !ruby/object:Gem::Requirement
|
|
39
|
+
none: false
|
|
40
|
+
requirements:
|
|
41
|
+
- - ! '>='
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
type: :development
|
|
45
|
+
prerelease: false
|
|
46
|
+
version_requirements: *70179477186140
|
|
47
|
+
description: Allows you to address enumerated integer columns as more sane and readable
|
|
48
|
+
symbols
|
|
49
|
+
email:
|
|
50
|
+
- sean.scally@gmail.com
|
|
51
|
+
executables: []
|
|
52
|
+
extensions: []
|
|
53
|
+
extra_rdoc_files: []
|
|
54
|
+
files:
|
|
55
|
+
- lib/legacy_enum/class_methods.rb
|
|
56
|
+
- lib/legacy_enum/method_definition_dsl.rb
|
|
57
|
+
- lib/legacy_enum/version.rb
|
|
58
|
+
- lib/legacy_enum.rb
|
|
59
|
+
- lib/tasks/legacy_enum_tasks.rake
|
|
60
|
+
- MIT-LICENSE
|
|
61
|
+
- Rakefile
|
|
62
|
+
- README.rdoc
|
|
63
|
+
homepage: http://github.com/anydiem/legacy_enum
|
|
64
|
+
licenses: []
|
|
65
|
+
post_install_message:
|
|
66
|
+
rdoc_options: []
|
|
67
|
+
require_paths:
|
|
68
|
+
- lib
|
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
70
|
+
none: false
|
|
71
|
+
requirements:
|
|
72
|
+
- - ! '>='
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
|
+
none: false
|
|
77
|
+
requirements:
|
|
78
|
+
- - ! '>='
|
|
79
|
+
- !ruby/object:Gem::Version
|
|
80
|
+
version: '0'
|
|
81
|
+
requirements: []
|
|
82
|
+
rubyforge_project:
|
|
83
|
+
rubygems_version: 1.8.10
|
|
84
|
+
signing_key:
|
|
85
|
+
specification_version: 3
|
|
86
|
+
summary: Brings enumerated int columns into the 21st century
|
|
87
|
+
test_files: []
|