Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
method_missing for missing attr
Browse files Browse the repository at this point in the history
  • Loading branch information
James Wong authored and James Wong committed Jul 11, 2017
1 parent 3af723b commit 15ad288
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/ansible_tower_client/base_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ def self.endpoint
base_class.to_s.split(/::/)[1].tableize.to_s.freeze
end

def method_missing(m, *args, &_block)
if self.class.send(:id_attrs).include?(m.to_s)
super
else
self.class.send(:convert_value, m, args, self)
end
end

def respond_to_missing?(method, *)
!self.class.send(:id_attrs).include?(method.to_s)
end

# Constructs and returns a new JSON wrapper class. Pass in a plain
# JSON string and it will automatically give you accessor methods
# that make it behave like a typical Ruby object. You may also pass
Expand Down
3 changes: 2 additions & 1 deletion spec/base_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
hash_with_relations = hash.merge("host" => 1, "related" => {"host" => "http://example.com/hosts/1"})
instance = described_class.new(api, hash_with_relations)

expect(instance).to_not respond_to(:host)
expect(instance).to_not respond_to(:host)
expect(instance.host_id).to eq(1)
expect(instance).to respond_to(:anything)
end
end

Expand Down

0 comments on commit 15ad288

Please sign in to comment.