class Dilute::Type

Attributes

attributes[R]
options[R]

Public Class Methods

new(options = {}, &block) click to toggle source
# File lib/dilute/type.rb, line 6
def initialize(options = {}, &block)
  @options = HashWithIndifferentAccess.new.merge(options)
  @attributes = HashWithIndifferentAccess.new
  instance_eval(&block) if block_given?
end

Public Instance Methods

attribute(key, attribute_type = :string) click to toggle source
# File lib/dilute/type.rb, line 64
def attribute(key, attribute_type = :string)
  attributes[key] = Dilute::Attribute.new(type: attribute_type)
end
delete() click to toggle source
# File lib/dilute/type.rb, line 43
def delete
  result = type.delete_mapping
  @type = nil
  result
end
generate_mapping() click to toggle source
# File lib/dilute/type.rb, line 49
def generate_mapping
  { :"#{type_name}" => { properties: properties } }
end
get(*args) click to toggle source
# File lib/dilute/type.rb, line 72
def get(*args)
  type.get *args
end
index() click to toggle source
# File lib/dilute/type.rb, line 29
def index
  @index ||= begin
    the_index = server.index(index_name)
    the_index.exists? ? the_index : (the_index.create && the_index)
  end
end
index_name() click to toggle source
# File lib/dilute/type.rb, line 20
def index_name
  # Ugly
  index_name = "#{options[:index_name]}".gsub(/([A-Z])/) { "_#{$1.downcase}"}.sub(/\A_/,'')
end
mapping() click to toggle source
# File lib/dilute/type.rb, line 60
def mapping
  type.get_mapping
end
properties() click to toggle source
# File lib/dilute/type.rb, line 53
def properties
  attributes.inject({}) do |sum, pair|
    k, v = pair
    sum[k] = v.to_config ; sum
  end
end
server() click to toggle source
# File lib/dilute/type.rb, line 16
def server
  @server ||= Stretcher::Server.new(server_url, log_level: :info)
end
server_url() click to toggle source
# File lib/dilute/type.rb, line 12
def server_url
  options[:server_url] || "http://localhost:9200"
end
type() click to toggle source
# File lib/dilute/type.rb, line 36
def type
  @type ||= begin
    the_type = index.type(type_name)
    the_type.exists? ? the_type : (the_type.put_mapping(generate_mapping) && the_type)
  end
end
type_name() click to toggle source
# File lib/dilute/type.rb, line 25
def type_name
  options[:type_name]
end