Class: Dilute::Type
- Inherits:
-
Object
- Object
- Dilute::Type
- Defined in:
- lib/dilute/type.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #attribute(key, attribute_type = :string) ⇒ Object
- #delete ⇒ Object
- #generate_mapping ⇒ Object
- #get(*args) ⇒ Object
- #index ⇒ Object
- #index_name ⇒ Object
-
#initialize(options = {}, &block) ⇒ Type
constructor
A new instance of Type.
- #mapping ⇒ Object
- #properties ⇒ Object
- #search(*args) ⇒ Object
- #server ⇒ Object
- #server_url ⇒ Object
- #type ⇒ Object
- #type_name ⇒ Object
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( = {}, &block) @options = HashWithIndifferentAccess.new.merge() @attributes = HashWithIndifferentAccess.new instance_eval(&block) if block_given? end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/dilute/type.rb', line 5 def attributes @attributes end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/dilute/type.rb', line 5 def @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 |
#delete ⇒ Object
43 44 45 46 47 |
# File 'lib/dilute/type.rb', line 43 def delete result = type.delete_mapping @type = nil result end |
#generate_mapping ⇒ Object
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 |
#index ⇒ Object
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_name ⇒ Object
20 21 22 23 |
# File 'lib/dilute/type.rb', line 20 def index_name # Ugly index_name = "#{[:index_name]}".gsub(/([A-Z])/) { "_#{$1.downcase}"}.sub(/\A_/,'') end |
#mapping ⇒ Object
60 61 62 |
# File 'lib/dilute/type.rb', line 60 def mapping type.get_mapping end |
#properties ⇒ Object
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 |
#server ⇒ Object
16 17 18 |
# File 'lib/dilute/type.rb', line 16 def server @server ||= Stretcher::Server.new(server_url, log_level: :info) end |
#server_url ⇒ Object
12 13 14 |
# File 'lib/dilute/type.rb', line 12 def server_url [:server_url] || "http://localhost:9200" end |
#type ⇒ Object
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_name ⇒ Object
25 26 27 |
# File 'lib/dilute/type.rb', line 25 def type_name [:type_name] end |