module Dilute::Modelize

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/dilute/modelize.rb, line 108
def initialize(attributes = {})
  @attributes = if attributes.has_key?("_source")
    attributes
  else
    {"_source" => HashWithIndifferentAccess.new.merge(attributes)}
  end
end

Public Instance Methods

attributes_without_elasticsearch_vars() click to toggle source
# File lib/dilute/modelize.rb, line 80
def attributes_without_elasticsearch_vars
  # elasticsearch_vars = %w(ok _index _type _id _version)
  attributes["_source"]
end
destroy() click to toggle source
# File lib/dilute/modelize.rb, line 103
def destroy
  raise "id can't be nil" unless id
  type.delete(id)
end
id() click to toggle source
# File lib/dilute/modelize.rb, line 76
def id
  attributes["_id"]
end
save() click to toggle source
# File lib/dilute/modelize.rb, line 85
def save
  run_callbacks :save do
    if valid?
      if id
        type.put(id, attributes_without_elasticsearch_vars)
      else
        results = type.post(attributes_without_elasticsearch_vars)
        if results["ok"] == true
          attributes.merge!(results)
        else
          puts "Something went wrong!"
          puts "attributes: #{attributes}"
        end
      end
    end
  end
end
type() click to toggle source
# File lib/dilute/modelize.rb, line 116
def type
  self.class.type.type
end