class Xeroizer::Record::Base

Attributes

attributes[R]
complete_record_downloaded[RW]
errors[RW]
model[R]
paged_record_downloaded[RW]
parent[R]

Public Class Methods

build(attributes, parent) click to toggle source

Build a record with attributes set to the value of attributes.

# File lib/xeroizer/record/base.rb, line 30
def build(attributes, parent)
  record = new(parent)
  attributes.each do | key, value |
    attr = record.respond_to?("#{key}=") || record.class.fields[key].nil? ? key : record.class.fields[key][:internal_name]
    record.send("#{attr}=", value)
  end
  record
end
new(parent) click to toggle source
# File lib/xeroizer/record/base.rb, line 43
def initialize(parent)
  @parent = parent
  @model = new_model_class(self.class.name.demodulize)
  @attributes = {}
end

Public Instance Methods

[](attribute) click to toggle source
# File lib/xeroizer/record/base.rb, line 53
def [](attribute)
  self.send(attribute)
end
[]=(attribute, value) click to toggle source
# File lib/xeroizer/record/base.rb, line 57
def []=(attribute, value)
  parent.mark_dirty(self) if parent
  self.send("#{attribute}=".to_sym, value)
end
as_json(options = {}) click to toggle source

Deprecated

# File lib/xeroizer/record/base.rb, line 137
def as_json(options = {})
  to_h.to_json
end
attributes=(new_attributes) click to toggle source
# File lib/xeroizer/record/base.rb, line 66
def attributes=(new_attributes)
  return unless new_attributes.is_a?(Hash)
  parent.mark_dirty(self) if parent
  new_attributes.each do | key, value |
    attr = respond_to?("#{key}=") ? key : self.class.fields[key][:internal_name]
    self.send("#{attr}=", value)
  end
end
complete_record_downloaded?() click to toggle source

Check to see if the complete record is downloaded.

# File lib/xeroizer/record/base.rb, line 85
def complete_record_downloaded?
  if !!self.class.list_contains_summary_only?
    !!complete_record_downloaded
  else
    true
  end
end
download_complete_record!() click to toggle source

Downloads the complete record if we only have a summary of the record.

# File lib/xeroizer/record/base.rb, line 99
def download_complete_record!
  record = self.parent.find(self.id)
  @attributes = record.attributes if record
  @complete_record_downloaded = true
  parent.mark_clean(self)
  self
end
inspect() click to toggle source
# File lib/xeroizer/record/base.rb, line 148
def inspect
  attribute_string = self.attributes.collect do |attr, value|
    "#{attr.inspect}: #{value.inspect}"
  end.join(", ")
  "#<#{self.class} #{attribute_string}>"
end
new_model_class(model_name) click to toggle source
# File lib/xeroizer/record/base.rb, line 49
def new_model_class(model_name)
  Xeroizer::Record.const_get("#{model_name}Model".to_sym).new(parent.try(:application), model_name.to_s)
end
new_record?() click to toggle source
# File lib/xeroizer/record/base.rb, line 80
def new_record?
  id.nil?
end
non_calculated_attributes() click to toggle source
# File lib/xeroizer/record/base.rb, line 62
def non_calculated_attributes
  attributes.reject {|name| self.class.fields[name][:calculated] }
end
paged_record_downloaded?() click to toggle source
# File lib/xeroizer/record/base.rb, line 94
def paged_record_downloaded?
  !!paged_record_downloaded
end
save() click to toggle source
# File lib/xeroizer/record/base.rb, line 107
def save
  save!
  true
rescue XeroizerError => e
  log "[ERROR SAVING] (#{__FILE__}:#{__LINE__}) - #{e.message}"
  false
end
save!() click to toggle source
# File lib/xeroizer/record/base.rb, line 115
def save!
  raise RecordInvalid unless valid?
  if new_record?
    create
  else
    update
  end

  saved!
end
saved!() click to toggle source
# File lib/xeroizer/record/base.rb, line 126
def saved!
  @complete_record_downloaded = true
  parent.mark_clean(self)
  true
end
to_h() click to toggle source
# File lib/xeroizer/record/base.rb, line 141
def to_h
  attrs = self.attributes.reject {|k, v| k == :parent }.map do |k, v|
    [k, v.kind_of?(Array) ? v.map(&:to_h) : (v.respond_to?(:to_h) ? v.to_h : v)]
  end
  Hash[attrs]
end
to_json(*args) click to toggle source
# File lib/xeroizer/record/base.rb, line 132
def to_json(*args)
  to_h.to_json(*args)
end
update_attributes(attributes) click to toggle source
# File lib/xeroizer/record/base.rb, line 75
def update_attributes(attributes)
  self.attributes = attributes
  save
end

Protected Instance Methods

create() click to toggle source

Attempt to create a new record.

# File lib/xeroizer/record/base.rb, line 158
def create
  request = to_xml
  log "[CREATE SENT] (#{__FILE__}:#{__LINE__}) #{request}"

  response = parent.send(parent.create_method, request)

  log "[CREATE RECEIVED] (#{__FILE__}:#{__LINE__}) #{response}"

  parse_save_response(response)
end
log(what) click to toggle source
# File lib/xeroizer/record/base.rb, line 196
def log(what)
  Xeroizer::Logging::Log.info what
end
parse_save_response(response_xml) click to toggle source

Parse the response from a create/update request.

# File lib/xeroizer/record/base.rb, line 187
def parse_save_response(response_xml)
  response = parent.parse_response(response_xml)
  record = response.response_items.first if response.response_items.is_a?(Array)
  if record && record.is_a?(self.class)
    @attributes = record.attributes
  end
  self
end
update() click to toggle source

Attempt to update an existing record.

# File lib/xeroizer/record/base.rb, line 170
def update
  if self.class.possible_primary_keys && self.class.possible_primary_keys.all? { | possible_key | self[possible_key].nil? }
    raise RecordKeyMustBeDefined.new(self.class.possible_primary_keys)
  end

  request = to_xml

  log "[UPDATE SENT] (#{__FILE__}:#{__LINE__}) \r\n#{request}"

  response = parent.http_post(request)

  log "[UPDATE RECEIVED] (#{__FILE__}:#{__LINE__}) \r\n#{response}"

  parse_save_response(response)
end