module KayakoClient::Object

Constants

COMMON_OPTIONS
OPTIONS
PROPERTY_TYPES

Public Class Methods

included(base) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 41
def self.included(base)
    base.extend(ClassMethods)
end

Public Instance Methods

[](name) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 69
def [](name)
    if name.respond_to?(:to_sym) && self.class.properties.include?(name.to_sym)
        send("#{name}")
    else
        nil
    end
end
[]=(name, value) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 53
def []=(name, value)
    raise ArgumentError, "object properties are read-only" if self.class.embedded?
    raise ArgumentError, "property :#{name} does not exist" unless self.class.properties.include?(name.to_sym)
    if !self.class.options[name.to_sym] || !self.class.options[name.to_sym][:readonly]
        if self.class.options[name.to_sym] && self.class.options[name.to_sym][:new] && !new?
            raise ArgumentError, "property :#{name} cannot be changed"
        end
        changes(name.to_sym)
        @associated.delete(name.to_sym)
        value = assign(self.class.properties[name.to_sym], value, self.class.options[name.to_sym] ? self.class.options[name.to_sym] : {})
        instance_variable_set("@#{name}", value)
    else
        raise ArgumentError, "property :#{name} is read-only"
    end
end
changed?() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 85
def changed?
    !changes.empty?
end
errors() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 77
def errors
    @errors ||= {}
end
has_errors?() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 97
def has_errors?
    errors.size > 0
end
id() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 45
def id
    nil
end
loaded!() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 101
def loaded!
    @changes = []
    @new = false
end
new?() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 89
def new?
    if defined?(@new)
        @new
    else
        @new = true
    end
end
properties() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 81
def properties
    self.class.properties.keys
end
to_i() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 49
def to_i
    instance_variable_defined?(:@id) ? instance_variable_get(:@id).to_i : 0
end

Private Instance Methods

assign(type, value, options = {}) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 413
def assign(type, value, options = {})
    if type.is_a?(Array)
        type = type.first
        if value.is_a?(Hash) && value.size == 1
            value = value.values.first
        end
        value = [ value ] unless value.is_a?(Array)
        value.map! do |item|
            assign_value(type, item, options)
        end
        value.freeze if options[:readonly]
    else
        value = assign_value(type, value, options)
    end
    value
end
assign_value(type, value, options = {}) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 430
def assign_value(type, value, options = {})
    case type
    when :integer
        value.to_i
    when :float
        value.to_f
    when :string
        value.to_s
    when :symbol
        value.to_sym
    when :boolean
        if value.respond_to?(:to_i)
            value.to_i == 0 ? false : true
        else
            case value
            when TrueClass, FalseClass
                value
            else
                !!value
            end
        end
    when :date
        case value
        when Time
            value
        else
            value.to_i > 0 ? Time.at(value.to_i) : nil
        end
    when :object
        raise RuntimeError, "missing :class" unless options[:class]
        klass = options[:class].is_a?(Class) ? options[:class] : KayakoClient.const_get(options[:class])
        object = klass.new(value.merge(inherited_options))
        object.loaded!
        object
    when :binary
        if value =~ %r{^[A-Za-z0-9+/]+={0,3}$} && (value.size % 4) == 0
            logger.debug "decoding base64 string" if logger
            Base64.decode64(value)
        else
            value
        end
    else
        value
    end
end
changes(property = nil) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 405
def changes(property = nil)
    @changes ||= []
    unless property.nil?
        @changes << property unless @changes.include?(property)
    end
    @changes
end
check_conditions(params) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 373
def check_conditions(params)
    @errors ||= {}
    self.class.options.each do |property, options|
        # check :condition
        if params[property]
            if options[:condition] && options[:condition].is_a?(Hash)
                options[:condition].each do |name, value|
                    param = params[name] || (instance_variable_defined?("@#{name}") ? instance_variable_get("@#{name}") : nil)
                    @errors[property] = "condition not met" unless param == value
                end
            end
        end
        # check :new
        if params.has_key?(property)
            @errors[property] = "value cannot be changed" if options[:new] && !new?
        end
    end
end
clean() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 341
def clean
    @associated = {}
    self.class.properties.each do |property, type|
        if instance_variable_defined?("@#{property}")
            remove_instance_variable("@#{property}")
        end
    end
end
import(options = {}) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 350
def import(options = {})
    if options && options.size == 1 && self.class.properties.size > 1
        values = options.values.first
        options = values if values.is_a?(Hash)
    end
    return if options.empty?
    options.each do |property, value|
        name = self.class.aliases.include?(property) ? self.class.aliases[property] : property
        if self.class.properties.include?(name)
            unless self.class.embedded? || (self.class.options[name] && self.class.options[name][:readonly])
                changes(name)
            end
            value = assign(self.class.properties[name], value, self.class.options[name] ? self.class.options[name] : {})
            instance_variable_set("@#{name}", value)
        else
            logger.debug "unsupported property :#{property}" if logger
        end
    end
end
require_properties(method, params) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 392
def require_properties(method, params)
    self.class.options.each do |property, options|
        next if params[property]
        if options[:required]
            if (options[:required].is_a?(Symbol) && options[:required] == method) ||
                (options[:required].is_a?(Array) && options[:required].include?(method))
                params[property] = instance_variable_get("@#{property}") if instance_variable_defined?("@#{property}")
                raise ArgumentError, "missing :#{property}" if params[property].nil?
            end
        end
    end
end
validate(method, params) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 370
def validate(method, params)
end