Class: Cauchy::IndexSchema

Inherits:
Object
  • Object
show all
Includes:
Normalization
Defined in:
lib/cauchy/index_schema.rb,
lib/cauchy/index_schema/normalization.rb

Defined Under Namespace

Modules: Normalization

Constant Summary collapse

@@schemas =
{}

Constants included from Normalization

Normalization::IGNORE_SETTINGS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Normalization

#normalize_field, #normalize_mapping, #normalize_settings, #normalize_value

Constructor Details

#initialize(index_alias) ⇒ IndexSchema



33
34
35
# File 'lib/cauchy/index_schema.rb', line 33

def initialize(index_alias)
  @index_alias = index_alias
end

Instance Attribute Details

#index_aliasObject

Returns the value of attribute index_alias.



31
32
33
# File 'lib/cauchy/index_schema.rb', line 31

def index_alias
  @index_alias
end

Class Method Details

.define(index_alias, &block) ⇒ Object



12
13
14
15
# File 'lib/cauchy/index_schema.rb', line 12

def define(index_alias, &block)
  index_alias = index_alias.to_s
  schemas[index_alias] = new(index_alias).tap { |s| s.define(&block) }
end

.load_schemas(paths) ⇒ Object



17
18
19
# File 'lib/cauchy/index_schema.rb', line 17

def load_schemas(paths)
  Dir[*Array(paths).map { |p| "#{p}/**/*.rb" }].each { |f| load f }
end

.schemasObject



21
22
23
# File 'lib/cauchy/index_schema.rb', line 21

def schemas
  @@schemas
end

.schemas=(schemas) ⇒ Object



25
26
27
# File 'lib/cauchy/index_schema.rb', line 25

def schemas=(schemas)
  @@schemas = schemas
end

Instance Method Details

#define(&block) ⇒ Object



37
38
39
# File 'lib/cauchy/index_schema.rb', line 37

def define(&block)
  instance_eval(&block)
end

#mapping_for(type) ⇒ Object



52
53
54
55
# File 'lib/cauchy/index_schema.rb', line 52

def mapping_for(type)
  return unless mappings.key?(type)
  mappings[type]['properties']
end

#mappings(&block) ⇒ Object



41
42
43
44
# File 'lib/cauchy/index_schema.rb', line 41

def mappings(&block)
  self.mappings = block.call || {} if block_given?
  @mappings || {}
end

#mappings=(value) ⇒ Object



46
47
48
49
50
# File 'lib/cauchy/index_schema.rb', line 46

def mappings=(value)
  @mappings = value.map do |type, mapping|
    [type.to_s, normalize_mapping(mapping)]
  end.to_h
end

#settings(&block) ⇒ Object



57
58
59
60
# File 'lib/cauchy/index_schema.rb', line 57

def settings(&block)
  self.settings = block.call || {} if block_given?
  @settings || {}
end

#settings=(value) ⇒ Object



62
63
64
# File 'lib/cauchy/index_schema.rb', line 62

def settings=(value)
  @settings = normalize_settings(value)
end

#typesObject



66
67
68
# File 'lib/cauchy/index_schema.rb', line 66

def types
  mappings.keys
end

#versionObject



70
71
72
73
74
# File 'lib/cauchy/index_schema.rb', line 70

def version
  @version ||= Digest::SHA1.hexdigest(
    { settings: settings, mappings: mappings }.to_json
  )
end