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

Add BaseModel class to help auto generate attribute accessors #17

Merged
merged 1 commit into from
Mar 17, 2016

Conversation

bzwei
Copy link
Contributor

@bzwei bzwei commented Mar 1, 2016

The BaseModel class is borrowed from our armrest gem. It automatically converts all hash attributes to instance methods. It can preserve named list of hash attribute from being converted. So far I don't know which attributes need to remain as hash.

With this work module InstanceMethods is no longer needed.

Update: attributes that appear in the related field will be treated as ids. So job_template, inventory, cloud_credential, project etc will be automatically appended with _id.

@bzwei bzwei force-pushed the base_model branch 2 times, most recently from 2cf3311 to 0cd071b Compare March 4, 2016 14:58
@gmcculloug
Copy link
Contributor

@bdunne @syncrou Please review.

attr_reader :inventory_id

def initialize(raw_body)
@inventory_id = raw_body["inventory"]
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the best way to implement a mapping back to @inventory_id?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can define your own method for particular model, for example

def inventory_id
  __getobj__['inventory']
end

The auto generated inventory method still exists and returns the same thing.

@syncrou
Copy link
Contributor

syncrou commented Mar 9, 2016

👍 LGTM - Except in regards to the removed inventory_id attribute for Group, Host - I thought the app was expecting a return on that method. If not - I'm set.

@bzwei
Copy link
Contributor Author

bzwei commented Mar 9, 2016

@syncrou If inventory_id is already used in manageiq app, then we need to update manageiq also. Or we can add back inventory_id method.

@bdunne
Copy link
Contributor

bdunne commented Mar 10, 2016

@bzwei Should we extract MiqHashStruct to more_core_extensions and use it as a superclass for BaseModel? I think that would simplify the code in BaseModel.

@bdunne
Copy link
Contributor

bdunne commented Mar 10, 2016

@bzwei I'm still stuck on AnsibleTowerClient::Host#inventory.class returning Integer rather than AnsibleTowerClient::Inventory.

@bzwei
Copy link
Contributor Author

bzwei commented Mar 10, 2016

@bdunne @syncrou Update: attributes that appear in the related field will be treated as ids. So job_template, inventory, cloud_credential, project etc will be automatically appended with _id.

@miq-bot
Copy link
Collaborator

miq-bot commented Mar 10, 2016

<pr_mergeability_checker />This pull request is not mergeable. Please rebase and repush.

@bzwei
Copy link
Contributor Author

bzwei commented Mar 11, 2016

@bdunne @syncrou please review and merge.

@syncrou
Copy link
Contributor

syncrou commented Mar 11, 2016

@bzwei - 👍

# # Or you can get back the original JSON if necessary.
# person.to_json # => Returns original JSON
#
def initialize(api, json = {})
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is json optional?

@bdunne
Copy link
Contributor

bdunne commented Mar 11, 2016

@bzwei I think this PR is getting much closer. I still think we should split BaseModel into:

  • BaseModel with an initialize method that filters what attribute methods should be defined and
  • its underlying ReadOnlyHashStruct

Does anyone else have an opinion? @Fryguy @gmcculloug @syncrou

@bdunne
Copy link
Contributor

bdunne commented Mar 11, 2016

Also, If BaseModel is split, the ReadOnlyHashStruct can do something very similar to MiqHashStruct using method_missing and respond_to_missing? to hit:[]. I think that will simplify the code a lot and remove the need for the instance_eval and define_method lines.

@bzwei
Copy link
Contributor Author

bzwei commented Mar 11, 2016

@bdunne the nice thing with this implementation is that it is more like a real class. You can call #methods(false) to get all new methods it added based on the hash.

@miq-bot
Copy link
Collaborator

miq-bot commented Mar 16, 2016

Checked commit bzwei@a1489e8 with ruby 2.2.3, rubocop 0.37.2, and haml-lint 0.16.1
13 files checked, 3 offenses detected

lib/ansible_tower_client/hash_model.rb

bdunne added a commit that referenced this pull request Mar 17, 2016
Add BaseModel class to help auto generate attribute accessors
@bdunne bdunne merged commit 5fea338 into ansible:master Mar 17, 2016
@bzwei bzwei deleted the base_model branch March 18, 2016 03:08
bdunne added a commit to bdunne/ansible_tower_client that referenced this pull request Feb 23, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants