-
Notifications
You must be signed in to change notification settings - Fork 41
Add BaseModel class to help auto generate attribute accessors #17
Conversation
2cf3311
to
0cd071b
Compare
attr_reader :inventory_id | ||
|
||
def initialize(raw_body) | ||
@inventory_id = raw_body["inventory"] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
👍 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. |
@syncrou If |
@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. |
@bzwei I'm still stuck on |
<pr_mergeability_checker />This pull request is not mergeable. Please rebase and repush. |
@bzwei - 👍 |
# # Or you can get back the original JSON if necessary. | ||
# person.to_json # => Returns original JSON | ||
# | ||
def initialize(api, json = {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is json
optional?
@bzwei I think this PR is getting much closer. I still think we should split
Does anyone else have an opinion? @Fryguy @gmcculloug @syncrou |
Also, If BaseModel is split, the ReadOnlyHashStruct can do something very similar to MiqHashStruct using |
@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. |
Checked commit bzwei@a1489e8 with ruby 2.2.3, rubocop 0.37.2, and haml-lint 0.16.1 lib/ansible_tower_client/hash_model.rb
|
Add BaseModel class to help auto generate attribute accessors
`raw_body` was removed in ansible#17
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.