Skip to content

Commit

Permalink
Rework solution for #1
Browse files Browse the repository at this point in the history
  • Loading branch information
gladtocode committed Mar 15, 2016
1 parent d6e7cda commit 721ab58
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/strict_open_struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def method_missing(method_name, *args, &block)
if open_struct_responds_to?(method_name)
@open_struct.send(method_name, *args, &block)
else
fail NoMethodError, "undefined method `#{method_name}' for #{self}"
failure(method_name)
end
end

Expand All @@ -25,11 +25,15 @@ def ==(other)
end

def [](key)
self.send(key)
@open_struct.to_h.fetch(key.to_sym)
end

private

def failure(method_name)
fail NoMethodError, "undefined method `#{method_name}' for #{self}"
end

def open_struct_responds_to?(method_name)
@open_struct.respond_to?(method_name) || method_name =~ /=$/
end
Expand Down
2 changes: 1 addition & 1 deletion test/basic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def first_name
end

specify "raises an exception if key is undefined" do
assert_raises NoMethodError do
assert_raises KeyError do
StrictOpenStruct.new[:a]
end
end
Expand Down

1 comment on commit 721ab58

@jgautsch
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍

Please sign in to comment.