Module: Elastictastic::Properties::ClassMethods
- Defined in:
- lib/elastictastic/properties.rb
Instance Method Summary collapse
- #all_fields ⇒ Object
- #boost(field, options = {}) ⇒ Object
- #define_embed(embed_name, options) ⇒ Object
- #define_field(field_name, options, &block) ⇒ Object
- #each_field ⇒ Object
- #embed(*embed_names) ⇒ Object
- #embeds ⇒ Object
- #field(*field_names, &block) ⇒ Object
- #field_properties ⇒ Object
- #properties ⇒ Object
- #properties_for_field(field_name) ⇒ Object
- #route(instance) ⇒ Object
- #route_with(field, options = {}) ⇒ Object
- #routing_required? ⇒ Boolean
- #select_fields ⇒ Object
Instance Method Details
#all_fields ⇒ Object
34 35 36 37 38 |
# File 'lib/elastictastic/properties.rb', line 34 def all_fields @_all_fields ||= {}.tap do |fields| each_field { |field, properties| fields[field] = properties } end end |
#boost(field, options = {}) ⇒ Object
69 70 71 |
# File 'lib/elastictastic/properties.rb', line 69 def boost(field, = {}) @_boost = { 'name' => field.to_s, 'null_value' => 1.0 }.merge(.stringify_keys) end |
#define_embed(embed_name, options) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/elastictastic/properties.rb', line 115 def (, ) = .to_s = Association.new(, ) module_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{} read_embed(#{.inspect}) end def #{}=(value) Util.call_or_each(value) do |check_value| unless check_value.nil? || check_value.is_a?(#{.class_name}) raise TypeError, "Expected instance of class #{.class_name}; got \#{check_value.inspect}" end end write_embed(#{.inspect}, value) end RUBY [] = end |
#define_field(field_name, options, &block) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/elastictastic/properties.rb', line 90 def define_field(field_name, , &block) field_name = field_name.to_s module_eval(<<-RUBY, __FILE__, __LINE__ + 1) def #{field_name} read_attribute(#{field_name.inspect}) end def #{field_name}=(value) write_attribute(#{field_name.inspect}, value) end RUBY field_properties[field_name.to_s] = Field.process(field_name, , &block) end |
#each_field ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/elastictastic/properties.rb', line 6 def each_field properties.each_pair do |field, properties| if properties['properties'] [field].clazz.each_field do |, | yield("#{field}.#{}", ) end elsif properties['fields'] properties['fields'].each_pair do |variant_field, variant_properties| if variant_field == field yield(field, variant_properties) else yield("#{field}.#{variant_field}", variant_properties) end end else yield field, properties end end end |
#embed(*embed_names) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/elastictastic/properties.rb', line 107 def (*) = . .each do || (, ) end end |
#embeds ⇒ Object
58 59 60 |
# File 'lib/elastictastic/properties.rb', line 58 def @_embeds ||= {} end |
#field(*field_names, &block) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/elastictastic/properties.rb', line 62 def field(*field_names, &block) = field_names. field_names.each do |field_name| define_field(field_name, , &block) end end |
#field_properties ⇒ Object
40 41 42 |
# File 'lib/elastictastic/properties.rb', line 40 def field_properties @_field_properties ||= {} end |
#properties ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/elastictastic/properties.rb', line 44 def properties return @_properties if defined? @_properties @_properties = {} @_properties.merge!(field_properties) .each_pair do |name, | @_properties[name] = { 'properties' => .clazz.properties } end @_properties end |
#properties_for_field(field_name) ⇒ Object
54 55 56 |
# File 'lib/elastictastic/properties.rb', line 54 def properties_for_field(field_name) properties[field_name.to_s] end |
#route(instance) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/elastictastic/properties.rb', line 82 def route(instance) if @_routing_field @_routing_field.to_s.split('.').inject(instance) do |obj, attr| Util.call_or_map(obj) { |el| el.__send__(attr) } if obj end end end |
#route_with(field, options = {}) ⇒ Object
73 74 75 76 |
# File 'lib/elastictastic/properties.rb', line 73 def route_with(field, = {}) @_routing_field = field @_routing_required = !![:required] end |
#routing_required? ⇒ Boolean
78 79 80 |
# File 'lib/elastictastic/properties.rb', line 78 def routing_required? !!@_routing_required end |
#select_fields ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/elastictastic/properties.rb', line 26 def select_fields [].tap do |fields| each_field do |field, properties| fields << [field, properties] if yield(field, properties) end end end |