module KayakoClient::Object::ClassMethods

Public Instance Methods

aliases() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 316
def aliases
    @aliases ||= {}
end
associate(name, property, object) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 199
def associate(name, property, object)
    unless @properties.include?(property)
        raise ArgumentError, "undefined property: #{property}; use 'property :#{property} ...' first"
    end
    klass = object.is_a?(Class) ? object : KayakoClient.const_get(object)
    # method for access to associated object
    define_method(name) do
        if @associated.has_key?(property)
            @associated[property]
        elsif instance_variable_defined?("@#{property}")
            id = instance_variable_get("@#{property}")
            if id.is_a?(Array)
                @associated[property] = id.inject([]) do |array, i|
                    array << klass.get(i.to_i, inherited_options)
                    array
                end
            elsif id.respond_to?(:to_i) && id.to_i > 0
                @associated[property] = klass.get(id.to_i, inherited_options)
            else
                @associated[property] = nil
            end
        else
            @associated[property] = nil
        end
    end
end
check_conditions(params) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 226
def check_conditions(params)
    errors = params.delete(:errors)
    return unless errors
    options.each do |property, option|
        if params[property]
            if option[:condition] && option[:condition].is_a?(Hash)
                option[:condition].each do |name, value|
                    errors[property] = "condition not met" unless params[name] == value
                end
            end
        end
    end
end
convert(type, value, options = {}) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 269
def convert(type, value, options = {})
    raise "property is readonly" if options[:readonly]
    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|
            convert_value(type, item, options)
        end
    else
        value = convert_value(type, value, options)
    end
    value
end
convert_value(type, value, options = {}) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 286
def convert_value(type, value, options = {})
    if options[:in] && options[:in].is_a?(Array)
        raise "not in list of allowed values" unless options[:in].include?(value)
    end
    case type
    when :integer
        if options[:range] && options[:range].is_a?(Range)
            raise "out of range" unless options[:range].include?(value)
        end
        result = value
    when :symbol
        result = value.to_s
    when :boolean
        result = (value == true) ? 1 : 0
    when :date
        result = value ? value.to_i : 0
    when :object
        raise RuntimeError, ":object cannot be used as a parameter"
    when :binary
        result = Base64.encode64(value).strip
    else
        result = value
    end
    result
end
embedded() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 108
def embedded
    @embedded ||= true
end
embedded?() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 112
def embedded?
    @embedded ||= false
end
map() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 324
def map
    @map ||= {}
end
options() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 320
def options
    @options ||= {}
end
path(path = nil) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 135
def path(path = nil)
    if path
        @path = path
    else
        @path ||= '/' + self.superclass.name.split('::').last + '/' + self.name.split('::').last
    end
end
properties() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 312
def properties
    @properties ||= {}
end
property(name, type, options = {}) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 143
def property(name, type, options = {})
    if (type.is_a?(Array) && type.size == 1 && PROPERTY_TYPES.include?(type.first)) || PROPERTY_TYPES.include?(type)
        init_variables
        @properties[name] = type # save original type
        type = type.first if type.is_a?(Array)
        get_alias = options.delete(:get) if options[:get]
        set_alias = options.delete(:set) if options[:set]
        if (name.to_s.include?(?_))
            altname = name.to_s.gsub(%r{_}, '')
            get_alias = altname unless get_alias
            set_alias = altname unless set_alias
        end
        # check options
        options.each do |option, value|
            if COMMON_OPTIONS.include?(option) || (OPTIONS.include?(type) && OPTIONS[type].include?(option))
                @options[name] ||= {}
                @options[name][option] = value
            else
                raise ArgumentError, "unsupported option: #{option}"
            end
        end
        # check if :class was specified for :object
        if type == :object
            @options[name] ||= {}
            @options[name][:readonly] = true
            raise ArgumentError, ":object requires :class" unless @options[name][:class]
        end
        # define setter and getter methods
        if !embedded? && (!@options[name] || !@options[name][:readonly])
            define_method("#{name}=") do |value|
                if self.class.options[name] && self.class.options[name][:new] && !new?
                    raise ArgumentError, "property :#{name} cannot be changed"
                end
                changes(name)
                @associated.delete(name)
                value = assign(self.class.properties[name], value, self.class.options[name] ? self.class.options[name] : {})
                instance_variable_set("@#{name}", value)
            end
            if set_alias
                alias_method("#{set_alias}=", "#{name}=")
                @map[name] = set_alias.to_sym
            end
        end
        define_method(name) do
            instance_variable_defined?("@#{name}") ? instance_variable_get("@#{name}") : nil
        end
        alias_method("#{name}?", name) if type == :boolean
        if get_alias
            alias_method(get_alias, name)
            @aliases[get_alias.to_sym] = name
        end
    else
        raise ArgumentError, "unsupported type: #{type.inspect}"
    end
end
require_properties(method, params) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 240
def require_properties(method, params)
    options.each do |property, option|
        next if params[property]
        if option[:required]
            if (option[:required].is_a?(Symbol) && option[:required] == method) ||
                (option[:required].is_a?(Array) && option[:required].include?(method))
                raise ArgumentError, "missing :#{property}"
            end
        end
    end
end
support?(method) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 127
def support?(method)
    unless embedded?
        defined?(@supported_methods) ? @supported_methods.include?(method) : true
    else
        false
    end
end
supports(*args) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 116
def supports(*args)
    args.each do |method|
        if %w{all get put post delete}.include?(method.to_s)
            @supported_methods ||= []
            @supported_methods << method.to_sym
        else
            logger.warn "ignored unsupported method :#{method}" if logger
        end
    end
end
validate(params) click to toggle source
# File lib/kayako_client/mixins/object.rb, line 252
def validate(params)
    errors = params.delete(:errors)
    params.inject({}) do |hash, (property, value)|
        if properties.include?(property)
            begin
                name = map[property] ? map[property] : property
                hash[name] = convert(properties[property], value, options[property] ? options[property] : {})
            rescue => error
                errors[property] = error.message if errors
            end
        else
            logger.warn "skipping validation of unknown property: #{property}" if logger
        end
        hash
    end
end

Private Instance Methods

init_variables() click to toggle source
# File lib/kayako_client/mixins/object.rb, line 330
def init_variables
    @properties ||= {}
    @aliases ||= {}
    @options ||= {}
    @map ||= {}
end