class ActionKitApi::ApiDataModel

Attributes

created_at[RW]

All the different types of objects have at least these

updated_at[RW]

All the different types of objects have at least these

Public Class Methods

new(hash = {}) click to toggle source
# 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

Public Instance Methods

safe_hash() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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 = {}) click to toggle source

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

# 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?() click to toggle source
# 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