Skip to content

Commit

Permalink
Call init on every value that responds to it, even if no args are given.
Browse files Browse the repository at this point in the history
  • Loading branch information
duelinmarkers committed Sep 1, 2009
1 parent 130416f commit 286ddd3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/renum/enumerated_value_type_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions spec/renum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 286ddd3

Please sign in to comment.