Class: ActionKitApi::ApiDataModel
- Inherits:
-
Object
- Object
- ActionKitApi::ApiDataModel
- Defined in:
- lib/action_kit_api/data_model.rb
Direct Known Subclasses
Action, Event, EventCampaign, Page, User
Instance Attribute Summary collapse
-
#created_at ⇒ Object
All the different types of objects have at least these.
-
#updated_at ⇒ Object
All the different types of objects have at least these.
Instance Method Summary collapse
-
#initialize(hash = {}) ⇒ ApiDataModel
constructor
A new instance of ApiDataModel.
- #safe_hash ⇒ Object
- #save ⇒ Object
- #to_hash ⇒ Object
-
#update(hash = {}) ⇒ Object
Updates all the instance variables in our local object with the values in the hash.
- #valid? ⇒ Boolean
Constructor Details
#initialize(hash = {}) ⇒ ApiDataModel
Returns a new instance of ApiDataModel.
11 12 13 14 15 16 17 18 |
# File 'lib/action_kit_api/data_model.rb', line 11 def initialize(hash = {}) @required_attrs ||= [] @read_only_attrs ||= [] @read_only_attrs.concat([:created_at, :updated_at]) self.update(hash) end |
Instance Attribute Details
#created_at ⇒ Object
All the different types of objects have at least these
9 10 11 |
# File 'lib/action_kit_api/data_model.rb', line 9 def created_at @created_at end |
#updated_at ⇒ Object
All the different types of objects have at least these
9 10 11 |
# File 'lib/action_kit_api/data_model.rb', line 9 def updated_at @updated_at end |
Instance Method Details
#safe_hash ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/action_kit_api/data_model.rb', line 85 def safe_hash attrs_hash = self.to_hash attrs_hash.delete_if do |k, v| @read_only_attrs.include?(k) end attrs_hash end |
#save ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/action_kit_api/data_model.rb', line 20 def save class_name = self.class.to_s.split("::").last raise MissingRequiredAttributeException unless self.valid? if self.id.nil? call = "#{class_name}.create" response = ActionKitApi.connection.call(call, self.safe_hash) else call = "#{class_name}.save_or_create" response = ActionKitApi.connection.call(call, self.safe_hash) end # Update ourselves to include the data that the server populated self.update(response) self end |
#to_hash ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/action_kit_api/data_model.rb', line 66 def to_hash user_hash = {} self.instance_variables.each do |iv| key = iv.to_s.delete("@").to_sym next if key == :required_attrs next if key == :read_only_attrs user_hash[key] = self.instance_variable_get(iv) end # XMLRPC::Client doesn't like empty values user_hash.delete_if do |k, v| v.to_s.empty? || v.nil? end user_hash end |
#update(hash = {}) ⇒ Object
Updates all the instance variables in our local object with the values in the hash. This will selectively update only the keys in the hash that is passed, and will not update/add non-existant attributes
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/action_kit_api/data_model.rb', line 43 def update(hash = {}) hash.each do |k,v| # The name of the setter for the value of k setter = "#{k}=" # Check if there is a matching setter if self.respond_to?(setter) # Yes, there is. Call the setter with the value self.send(setter, v) end end self end |
#valid? ⇒ Boolean
58 59 60 61 62 63 64 |
# File 'lib/action_kit_api/data_model.rb', line 58 def valid? @required_attrs.each do |k| return false if self.send(k).nil? end true end |