Module: KayakoClient::Object::ClassMethods

Defined in:
lib/kayako_client/mixins/object.rb

Instance Method Summary collapse

Instance Method Details

#aliasesObject



316
317
318
# File 'lib/kayako_client/mixins/object.rb', line 316

def aliases
    @aliases ||= {}
end

#associate(name, property, object) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# 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) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
# 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 = {}) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# 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 = {}) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# 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

#embeddedObject



108
109
110
# File 'lib/kayako_client/mixins/object.rb', line 108

def embedded
    @embedded ||= true
end

#embedded?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/kayako_client/mixins/object.rb', line 112

def embedded?
    @embedded ||= false
end

#mapObject



324
325
326
# File 'lib/kayako_client/mixins/object.rb', line 324

def map
    @map ||= {}
end

#optionsObject



320
321
322
# File 'lib/kayako_client/mixins/object.rb', line 320

def options
    @options ||= {}
end

#path(path = nil) ⇒ Object



135
136
137
138
139
140
141
# 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

#propertiesObject



312
313
314
# File 'lib/kayako_client/mixins/object.rb', line 312

def properties
    @properties ||= {}
end

#property(name, type, options = {}) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# 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) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
# 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) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
# 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) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# 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) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# 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