Skip to content
Browse files

Call init on every value that responds to it, even if no args are given.

  • Loading branch information...
1 parent 130416f commit 286ddd3a466c0af20953f84c61ccc83682502b96 @duelinmarkers duelinmarkers committed Sep 1, 2009
Showing with 10 additions and 5 deletions.
  1. +1 −1 lib/renum/enumerated_value_type_factory.rb
  2. +9 −4 spec/renum_spec.rb
View
2 lib/renum/enumerated_value_type_factory.rb
@@ -22,7 +22,7 @@ def create_values klass, values, &block
value = klass.new(value_name)
klass.const_set(value_name, value)
value.instance_eval &instance_block if instance_block
- value.init *init_args if init_args.any?
+ value.init *init_args if value.respond_to? :init
end
teardown_from_definition_in_block(klass)
else
View
13 spec/renum_spec.rb
@@ -65,11 +65,12 @@ def abbr
Small("Really really tiny")
Medium("Sort of in the middle")
Large("Quite big")
+ Unknown()
attr_reader :description
- def init description
- @description = description
+ def init description = nil
+ @description = description || "NO DESCRIPTION GIVEN"
end
end
@@ -80,12 +81,12 @@ def init description
end
describe "enum with no values array and values declared in the block" do
- it "provides an alternative means of declaring values where extra information can be provided for initialization" do
+ it "provides another way to declare values where an init method can take extra params" do
Size::Small.description.should == "Really really tiny"
end
it "works the same as the basic form with respect to ordering" do
- Size.values.should == [Size::Small, Size::Medium, Size::Large]
+ Size.values.should == [Size::Small, Size::Medium, Size::Large, Size::Unknown]
end
it "responds as expected to arbitrary method calls, in spite of using method_missing for value definition" do
@@ -95,6 +96,10 @@ def init description
it "supports there being no extra data and no init() method defined, if you don't need them" do
HairColor::BLONDE.name.should == "BLONDE"
end
+
+ it "calls the init method even if no arguments are provided" do
+ Size::Unknown.description.should == "NO DESCRIPTION GIVEN"
+ end
end
enum :Rating do

0 comments on commit 286ddd3

Please sign in to comment.
Something went wrong with that request. Please try again.