class ClioClient::Resource

Attributes

attributes[RW]
session[RW]

Public Class Methods

has_association(name, klass, options = {}) click to toggle source
# File lib/clio_client/resource.rb, line 66
def has_association(name, klass, options = {})
  attr_accessor "#{name}_id"
  attr_reader name
  self.attributes["#{name}_id".intern] = {type: :foreign_key}
  define_method "#{name}=" do |attributes|
    write_attribute("#{name}_id", attributes["id"])
    if options[:polymorphic]
      obj = polymorphic_object(attributes, options[:accepted_types])
      obj ||= klass.new(attributes, session)
    else
      obj = klass.new(attributes, session)
    end
    write_attribute("#{name}", obj)
  end      
end
has_many_association(name, klass, options = {}) click to toggle source
# File lib/clio_client/resource.rb, line 82
def has_many_association(name, klass, options = {})
  attr_reader name
  self.attributes[name.intern] = {type: :has_many_association}
  define_method "#{name}=" do |arr|
    many = arr.collect do |attributes| 
      if options[:polymorphic]
        obj = polymorphic_object(attributes, options[:accepted_types])
        obj ||= klass.new(attributes, session)
      else
        klass.new(attributes, session)
      end
    end
    write_attribute(name, many)
  end      
end
inherited(subclass) click to toggle source
# File lib/clio_client/resource.rb, line 60
def inherited(subclass)
  if !self.attributes.nil?
    subclass.attributes = self.attributes
  end
end
inspect() click to toggle source
# File lib/clio_client/resource.rb, line 10
def self.inspect
  attr_list = attributes.inject([]) do |a, (attr, opts)|
    a << "#{attr}: #{opts[:type]}"
  end * ", "
  "#{super}(#{attr_list})"
end
new(values = {}, session = nil) click to toggle source
# File lib/clio_client/resource.rb, line 28
def initialize(values = {}, session = nil)
  self.session = session
  values.each_pair do |k, v|
    self.send("#{k}=", v) if respond_to?("#{k}=") && !v.nil?
  end
end
set_attributes(attrs) click to toggle source
# File lib/clio_client/resource.rb, line 47
def set_attributes(attrs)
  self.attributes = attrs
  attrs.each_pair do |name, options|
    attr_reader name
    define_method "#{name}=" do |value|
      if options[:readonly] && !instance_variable_get("@#{name}").nil?
        raise AttributeReadOnly 
      end
      write_attribute("#{name}", convert_attribute(value, options))
    end
  end
end

Public Instance Methods

==(o) click to toggle source
# File lib/clio_client/resource.rb, line 100
def ==(o)
  self.class == o.class && !self.id.nil? && self.id != "" && self.id == o.id
end
Also aliased as: eql?
[](val) click to toggle source
# File lib/clio_client/resource.rb, line 105
def [](val)
  self.send(val)
end
destroy() click to toggle source
# File lib/clio_client/resource.rb, line 126
def destroy
  raise ResourceNotSaved if self.id.nil?
  api.destroy(self.id)
end
eql?(o)
Alias for: ==
inspect() click to toggle source
# File lib/clio_client/resource.rb, line 17
def inspect
  attr_list = self.class.attributes.inject([]) do |a, (attr, opts)|
    if has_attribute?(attr)
      a << "#{attr}: #{self[attr].inspect}"
    else
      a
    end
  end * ", "      
  "#<#{self.class} #{attr_list}>"
end
reload() click to toggle source
# File lib/clio_client/resource.rb, line 121
def reload
  raise ResourceNotSaved if self.id.nil?      
  api.find(self.id)
end
save() click to toggle source
# File lib/clio_client/resource.rb, line 110
def save
  if self.id.nil?
    saved_item = api.create(self.to_params)
    self.id = saved_item && saved_item.id
    self
  else
    api.update(self.id, self.to_params)
    self
  end
end
to_params() click to toggle source
# File lib/clio_client/resource.rb, line 35
def to_params
  self.class.attributes.inject({}) do |h, (attr, opts)|
    has_attribute?(attr) ? h.merge(attr => paramify(self[attr])) : h
  end
end

Private Instance Methods

api() click to toggle source
# File lib/clio_client/resource.rb, line 143
def api
  raise NotImplementedError
end
convert_attribute(val, options) click to toggle source
# File lib/clio_client/resource.rb, line 161
def convert_attribute(val, options)
  case options[:type]
  when :int
    val.to_i
  when :string
    val.to_s
  when :date
    val.kind_of?(Date) ? val : Date.parse(val)
  when :decimal
    val.to_f
  when :boolean 
    val == true || val == "true"
  when :datetime 
    val.kind_of?(DateTime) ? val : DateTime.parse(val)
  when :datetime 
    val.kind_of?(Time) ? val : Time.parse(val)
  when :foreign_key 
    (val == "" || val.nil?) ? nil : val.to_i
  else 
    val
  end
end
has_attribute?(attr) click to toggle source
# File lib/clio_client/resource.rb, line 147
def has_attribute?(attr)
  instance_variable_defined?("@#{attr}")
end
paramify(val) click to toggle source
# File lib/clio_client/resource.rb, line 151
def paramify(val)      
  if val.kind_of? ClioClient::Resource
    val.to_params
  elsif val.kind_of? Array
    val.collect{|v| paramify(v) }
  else
    val
  end
end
polymorphic_object(attributes, accepted_types) click to toggle source
# File lib/clio_client/resource.rb, line 132
def polymorphic_object(attributes, accepted_types)
  if accepted_types.include? attributes["type"]
    klass = ClioClient.const_get attributes["type"].intern
    klass.new(attributes, session)
  end
end
write_attribute(name, value) click to toggle source
# File lib/clio_client/resource.rb, line 139
def write_attribute(name, value)
  instance_variable_set("@#{name}", value)
end