class Elos::Index::Model::Object

Attributes

object[R]

Public Class Methods

encode(object) click to toggle source
# File lib/elos/index/model/object.rb, line 4
def self.encode(object)
  inner_encode(object).to_json
end
new(params = {}) click to toggle source
# File lib/elos/index/model/object.rb, line 8
def initialize(params = {})
  @params = params
  @object = objectify(params)
end

Protected Class Methods

inner_encode(object) click to toggle source
# File lib/elos/index/model/object.rb, line 25
def self.inner_encode(object)
  if object.is_a?(Hash)
    object.each do |k, v|
      object[k] = inner_encode(v)
    end
  elsif object.is_a?(Array)
    object.each_with_index do |o, i|
      object[i] = inner_encode(o)
    end
  elsif object.is_a?(Elos::Index)
    object.attributes.merge!(type: object.class.name)
  else
    object
  end
end

Public Instance Methods

method_missing(method, *args, **hargs, &block) click to toggle source
Calls superclass method
# File lib/elos/index/model/object.rb, line 13
def method_missing(method, *args, **hargs, &block)
  if method.to_s.end_with?('=')
    object.send(method, objectify(args[0]))
  elsif object.respond_to?(method)
    object.send(method, *args)
  else
    super
  end
end

Protected Instance Methods

objectify(value) click to toggle source
# File lib/elos/index/model/object.rb, line 41
def objectify(value)
  if value.is_a?(Hash)
    if value.key?(:type)
      value.delete(:type).constantize.new(value)
    else
      OpenStruct.new(Hash[value.map { |k, v| [k, objectify(v)] }])
    end
  elsif value.is_a?(Array)
    value.map { |v| objectify(v) }
  else
    value
  end
end