Skip to content

Raise NoMethodError if raise_on_missing option is enabled #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ ros = RecursiveOpenStruct.new(h, preserve_original_keys: true)
ros.to_h # => { 'fear' => 'is', 'the' => 'mindkiller' }
```

### Optional: Raise error on missing attribute

This option allows to raise an error if you try to call an attribute you didn't specify in hash

```ruby
h = { 'fear' => 'is', 'the' => 'mindkiller' } }
ros = RecursiveOpenStruct.new(h, raise_on_missing: true)
ros.undefined # => undefined method `undefined' for #<RecursiveOpenStruct fear="is", the="mindkiller">
```

The default behaviour returns nil

```ruby
h = { 'fear' => 'is', 'the' => 'mindkiller' } }
ros = RecursiveOpenStruct.new(h)
ros.undefined # => nil
```

## Installation

Expand Down
7 changes: 6 additions & 1 deletion lib/recursive_open_struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def self.default_options
{
mutate_input_hash: false,
recurse_over_arrays: false,
preserve_original_keys: false
preserve_original_keys: false,
raise_on_missing: false
}
end

Expand Down Expand Up @@ -124,6 +125,10 @@ def method_missing(mid, *args)
if @table.key?(_get_key_from_table_(key))
new_ostruct_member!(key)
public_send(mid)
elsif @options[:raise_on_missing]
err = NoMethodError.new "undefined method `#{mid}' for #{self}", mid, args
err.set_backtrace caller(1)
raise err
end
else
err = NoMethodError.new "undefined method `#{mid}' for #{self}", mid, args
Expand Down
24 changes: 24 additions & 0 deletions spec/recursive_open_struct/indifferent_access_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,30 @@
end
end

context 'when undefined method' do
context 'when raise_on_missing is enabled' do
subject(:recursive) { RecursiveOpenStruct.new(recursive_hash, raise_on_missing: true) }
let(:recursive_hash) { {:foo => [ {'bar' => [ { 'foo' => :bar} ] } ] } }

specify 'raises NoMethodError' do
expect {
recursive.undefined_method
}.to raise_error(NoMethodError)
end
end

context 'when raise_on_missing is disabled' do
context 'preserves the original keys' do
subject(:recursive) { RecursiveOpenStruct.new(recursive_hash) }
let(:recursive_hash) { {:foo => [ {'bar' => [ { 'foo' => :bar} ] } ] } }

specify 'returns nil' do
expect(recursive.undefined_method).to be_nil
end
end
end
end

end

end
Expand Down