Permalink
Browse files

various tweaks

  • Loading branch information...
1 parent 693e6d2 commit 4623645dfc7814bfc77dd69540b6e056f64ae46f Jordan Brough committed Apr 17, 2010
Showing with 27 additions and 9 deletions.
  1. +1 −0 Rakefile
  2. +7 −3 lib/renum/enumerated_value.rb
  3. +18 −6 lib/renum/enumerated_value_type_factory.rb
  4. +1 −0 renum.gemspec
View
@@ -15,6 +15,7 @@ begin
s.homepage = "http://github.com/duelinmarkers/renum"
s.description = "provides a readable but terse enum facility for Ruby"
s.authors = ["John Hume"]
+ s.rubyforge_project = "renum"
end
rescue LoadError
puts "Jeweler or a dependency not available. To install: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
@@ -18,15 +18,19 @@ class << self
def values
@values ||= []
end
-
+
+ def find(name)
+ detect{|v| v.name == name}
+ end
+
end
include Comparable
attr_reader :name
attr_reader :index
def initialize name
- @name = name.to_s
+ @name = name.to_s.downcase
@index = self.class.values.size
self.class.values << self
end
@@ -35,7 +39,7 @@ def initialize name
# Don't override this if you're using Renum with the constantize_attribute
# plugin, which relies on this behavior.
def to_s
- "#{self.class}::#{name}"
+ name
end
# Sorts enumerated values into the order in which they're declared.
@@ -7,19 +7,19 @@ def create nest, type_name, values, &block
klass = create_class nest, type_name
create_values klass, values, &block
end
-
+
def create_class nest, type_name
klass = Class.new EnumeratedValue
nest.const_set(type_name, klass)
klass
end
-
+
def create_values klass, values, &block
setup_for_definition_in_block(klass) if values == :defined_in_block
klass.class_eval &block if block_given?
if values == :defined_in_block
klass.block_defined_values.each do |value_name, init_args, instance_block|
- value = klass.new(value_name)
+ value = klass.new(value_name, *init_args)
klass.const_set(value_name, value)
value.instance_eval &instance_block if instance_block
value.init *init_args if value.respond_to? :init
@@ -30,8 +30,20 @@ def create_values klass, values, &block
klass.const_set(name, klass.new(name))
end
end
+ klass.module_eval do
+ # define query methods
+ klass.values.each do |enum|
+ define_method "#{enum.name}?" do
+ name == enum.name
+ end
+ end
+ # prettier inspect method
+ define_method "inspect" do
+ "#{klass}::#{name.titlecase}"
+ end
+ end
end
-
+
def setup_for_definition_in_block klass
klass.class_eval do
def self.block_defined_values
@@ -43,7 +55,7 @@ def self.method_missing value_name, *init_args, &instance_block
end
end
end
-
+
def teardown_from_definition_in_block klass
class << klass
remove_method :block_defined_values
@@ -52,4 +64,4 @@ class << klass
end
end
end
-end
+end
View
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
s.homepage = %q{http://github.com/duelinmarkers/renum}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
+ s.rubyforge_project = %q{renum}
s.rubygems_version = %q{1.3.1}
s.summary = %q{provides a readable but terse enum facility for Ruby}
s.test_files = [

0 comments on commit 4623645

Please sign in to comment.