Sha256: 974bd704737c4b916e277f843ac3b4257a1a84fad944ce29589b362fd9c5852e

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

module Elastic::Commands
  class CompareMappings < Elastic::Support::Command.new(:current, :user)
    def perform
      user_properties.select 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

9 entries across 9 versions & 1 rubygems

Version Path
elastic-rails-0.8.7 lib/elastic/commands/compare_mappings.rb
elastic-rails-0.8.6 lib/elastic/commands/compare_mappings.rb
elastic-rails-0.8.5 lib/elastic/commands/compare_mappings.rb
elastic-rails-0.8.4 lib/elastic/commands/compare_mappings.rb
elastic-rails-0.8.3 lib/elastic/commands/compare_mappings.rb
elastic-rails-0.8.2 lib/elastic/commands/compare_mappings.rb
elastic-rails-0.8.1 lib/elastic/commands/compare_mappings.rb
elastic-rails-0.8.0 lib/elastic/commands/compare_mappings.rb
elastic-rails-0.7.0 lib/elastic/commands/compare_mappings.rb