Class: Dilute::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/dilute/type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Type

Returns a new instance of Type.



6
7
8
9
10
# 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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/dilute/type.rb', line 5

def attributes
  @attributes
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/dilute/type.rb', line 5

def options
  @options
end

Instance Method Details

#attribute(key, attribute_type = :string) ⇒ Object



64
65
66
# File 'lib/dilute/type.rb', line 64

def attribute(key, attribute_type = :string)
  attributes[key] = Dilute::Attribute.new(type: attribute_type)
end

#deleteObject



43
44
45
46
47
# File 'lib/dilute/type.rb', line 43

def delete
  result = type.delete_mapping
  @type = nil
  result
end

#generate_mappingObject



49
50
51
# File 'lib/dilute/type.rb', line 49

def generate_mapping
  { :"#{type_name}" => { properties: properties } }
end

#get(*args) ⇒ Object



72
73
74
# File 'lib/dilute/type.rb', line 72

def get(*args)
  type.get *args
end

#indexObject



29
30
31
32
33
34
# 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_nameObject



20
21
22
23
# File 'lib/dilute/type.rb', line 20

def index_name
  # Ugly
  index_name = "#{options[:index_name]}".gsub(/([A-Z])/) { "_#{$1.downcase}"}.sub(/\A_/,'')
end

#mappingObject



60
61
62
# File 'lib/dilute/type.rb', line 60

def mapping
  type.get_mapping
end

#propertiesObject



53
54
55
56
57
58
# 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

#search(*args) ⇒ Object



68
69
70
# File 'lib/dilute/type.rb', line 68

def search(*args)
  type.search *args
end

#serverObject



16
17
18
# File 'lib/dilute/type.rb', line 16

def server
  @server ||= Stretcher::Server.new(server_url, log_level: :info)
end

#server_urlObject



12
13
14
# File 'lib/dilute/type.rb', line 12

def server_url
  options[:server_url] || "http://localhost:9200"
end

#typeObject



36
37
38
39
40
41
# 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_nameObject



25
26
27
# File 'lib/dilute/type.rb', line 25

def type_name
  options[:type_name]
end