diff --git a/lib/ansible_tower_client/base_model.rb b/lib/ansible_tower_client/base_model.rb index 121b894..a7f83ee 100644 --- a/lib/ansible_tower_client/base_model.rb +++ b/lib/ansible_tower_client/base_model.rb @@ -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 @@ -83,11 +95,7 @@ def update_attributes!(attributes) @api.patch(url, attributes.to_json) attributes.each do |method_name, value| invoke_name = "#{override_raw_attributes[method_name] || method_name}=" - if respond_to?(invoke_name) - send(invoke_name, value) - else - AnsibleTowerClient.logger.warn("Unknown attribute/method: #{invoke_name}. Skip updating it ...") - end + send(invoke_name, value) end true end diff --git a/spec/base_model_spec.rb b/spec/base_model_spec.rb index b55849e..6179883 100644 --- a/spec/base_model_spec.rb +++ b/spec/base_model_spec.rb @@ -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 diff --git a/spec/support/shared_examples/crud_methods.rb b/spec/support/shared_examples/crud_methods.rb index 267bcba..f65da30 100644 --- a/spec/support/shared_examples/crud_methods.rb +++ b/spec/support/shared_examples/crud_methods.rb @@ -37,12 +37,6 @@ expect(obj.name).to eq 'blah' end - it "ignore unknown attributes if patch succeeds" do - expect(instance_api).to receive(:patch).and_return(instance_double("Faraday::Result", :body => raw_instance.to_json)) - expect(obj.update_attributes!(:name => 'blah', :stranger_thing => 'bomb')).to eq true - expect(obj.name).to eq 'blah' - end - it "returns an error if an error is raised" do expect(instance_api).to receive(:patch).and_raise(AnsibleTowerClient::Error, 'error') expect { obj.update_attributes!(:name => 'bad name') }.to raise_error(AnsibleTowerClient::Error)