Sha256: 71c448fa0a2ce8cbf34586a2882a7601d0e4856bf2d3f2eff9cf77d1f1f9ad5c

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

module Elastic::Commands
  class CompareMappings < Elastic::Support::Command.new(:current, :user)
    def perform
      user_properties.reject do |field, property|
        compare_field_properties(current_properties[field], property)
      end.map { |f| f[0] }
    end

    private

    def current_properties
      @current_properties ||= Hash[flatten(current)]
    end

    def user_properties
      @user_properties ||= Hash[flatten(user)]
    end

    def flatten(_raw, _prefix = '')
      _raw['properties'].flat_map do |name, raw_field|
        if raw_field['type'] == 'nested'
          childs = flatten(raw_field, name + '.')
          childs << [
            _prefix + name,
            raw_field.slice(*(raw_field.keys - ['properties']))
          ]
        else
          [[_prefix + name, raw_field.dup]]
        end
      end
    end

    def compare_field_properties(_current, _user)
      return false if _current.nil?

      case _current['type']
      when 'date'
        return _current == { 'format' => 'dateOptionalTime' }.merge(_user)
      else
        return _current == _user
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
elastic-rails-1.0.4 lib/elastic/commands/compare_mappings.rb
elastic-rails-1.0.3 lib/elastic/commands/compare_mappings.rb
elastic-rails-1.0.2 lib/elastic/commands/compare_mappings.rb
elastic-rails-1.0.1 lib/elastic/commands/compare_mappings.rb
elastic-rails-1.0.0 lib/elastic/commands/compare_mappings.rb