Module: Dry::ElasticModel::Attributes

Included in:
Base
Defined in:
lib/dry/elastic_model/attributes.rb

Instance Method Summary collapse

Instance Method Details

#field(name, type, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dry/elastic_model/attributes.rb', line 6

def field(name, type, opts = {})
  allow_missing = opts.delete(:allow_missing)
  type_definition = Types::TYPES.fetch(type.to_sym)

  type_options_klass = type_definition.meta[:type_options]

  options = if type_options_klass
              type_options_klass.new(opts).to_h
            else
              opts
            end

  default_opts = type_definition.meta[:opts] || {}
  define_attribute(
    name,
    type_definition.meta(opts: default_opts.merge(options.to_h)),
    allow_missing: allow_missing
  )
end

#list(name, type, opts = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dry/elastic_model/attributes.rb', line 40

def list(name, type, opts = {})
  allow_missing = opts.delete(:allow_missing)
  member = Types::TYPES.fetch(type.to_sym)

  type_definition = Types::Array.call(member)

  default_opts = type_definition.meta[:opts] || {}
  define_attribute(
    name,
    type_definition.meta(opts: default_opts.merge(opts)),
    allow_missing: allow_missing
  )
end

#range(name, type, opts = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dry/elastic_model/attributes.rb', line 26

def range(name, type, opts = {})
  allow_missing = allow_missing
  member = Types::RANGE_TYPES.fetch(type.to_sym)

  type_definition = Types::Range.call(member)

  default_opts = type_definition.meta[:opts] || {}
  define_attribute(
    name,
    type_definition.meta(opts: default_opts.merge(opts)),
    allow_missing: opts[:allow_missing]
  )
end