Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
caedes
committed
Jun 7, 2013
1 parent
bd5e1b2
commit fc63ef0
Showing
3 changed files
with
102 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,59 @@ | ||
unless String.methods.include? 'underscore' | ||
class String | ||
def underscore | ||
self.gsub(/::/, '/'). | ||
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). | ||
gsub(/([a-z\d])([A-Z])/,'\1_\2'). | ||
tr("-", "_"). | ||
downcase | ||
require 'active_support/core_ext/string' | ||
|
||
module ActAsEnumerable | ||
|
||
def self.included(base) | ||
base.define_singleton_method(:enum) do |name, enumeration| | ||
ActAsEnumerable.enum_method base, name, enumeration | ||
end | ||
end | ||
end | ||
|
||
class Object | ||
def self.enum name, enumeration | ||
name = name.to_s | ||
class_eval " | ||
attr_accessor :#{name} | ||
def self.#{name}_values | ||
#{enumeration} | ||
end | ||
def self.#{name}_captions | ||
#{enumeration}.map do |value| | ||
humanize_#{name} value | ||
end | ||
end" | ||
enumeration.each do |value| | ||
value = value.to_s | ||
eval " | ||
def #{value}? | ||
self.#{name} == '#{value}' | ||
end | ||
def #{value}! | ||
self.#{name} = '#{value}' | ||
end" | ||
protected | ||
|
||
def self.enum_method(base, name, enumeration) | ||
ActAsEnumerable.enum_getter_method base, name, enumeration | ||
end | ||
|
||
def self.enum_getter_method(base, name, enumeration) | ||
base.define_singleton_method("#{name}_values") do | ||
enumeration | ||
end | ||
eval " | ||
def humanize_#{name} value | ||
value = value.to_s | ||
begin | ||
Required::Module::const_get 'I18n' | ||
::I18n.t \"enum.#{self.to_s.underscore}.#{name}.#{'#{value}'}\" | ||
rescue NameError | ||
value | ||
end | ||
base.define_singleton_method("#{name}_captions") do | ||
enumeration.map do |value| | ||
value.to_s.humanize | ||
end | ||
def #{name}_caption | ||
self.#{name}.nil? ? self.#{name} : humanize_#{name}(self.#{name}) | ||
end" | ||
|
||
# validates_inclusion_of name.to_sym, { allow_nil: true, in: enumeration } if self.ancestors.include? 'ActiveRecord::Base' | ||
end | ||
end | ||
end | ||
|
||
# class Object | ||
# def self.enum name, enumeration | ||
# name = name.to_s | ||
|
||
# enumeration.each do |value| | ||
# value = value.to_s | ||
# eval " | ||
# def #{value}? | ||
# self.#{name} == '#{value}' | ||
# end | ||
# def #{value}! | ||
# self.#{name} = '#{value}' | ||
# end" | ||
# end | ||
# eval " | ||
# def humanize_#{name} value | ||
# value = value.to_s | ||
# begin | ||
# Required::Module::const_get 'I18n' | ||
# ::I18n.t \"enum.#{self.to_s.underscore}.#{name}.#{'#{value}'}\" | ||
# rescue NameError | ||
# value | ||
# end | ||
# end | ||
# def #{name}_caption | ||
# self.#{name}.nil? ? self.#{name} : humanize_#{name}(self.#{name}) | ||
# end" | ||
|
||
# # validates_inclusion_of name.to_sym, { allow_nil: true, in: enumeration } if self.ancestors.include? 'ActiveRecord::Base' | ||
# end | ||
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters