Sha256: 7ee7c74fda1a742ed64ba21f7c0be9489f713d2c3ec4c104924f90a13adecde6

Contents?: true

Size: 897 Bytes

Versions: 36

Compression:

Stored size: 897 Bytes

Contents

module Synchronisable
  class AttributeMapper
    class << self
      def map(source, mappings, options = {})
        new(mappings, options).map(source)
      end
    end

    def initialize(mappings, options = {})
      @mappings = mappings
      @keep = options[:keep] || []
      @only, @except = options[:only], options[:except]
    end

    def map(source)
      result = source.dup

      apply_mappings(result) if @mappings.present?
      apply_only(result)     if @only.present?
      apply_except(result)   if @except.present?

      result
    end

    private

    def apply_mappings(source)
      source.transform_keys! { |key| @mappings[key] || key }
    end

    def apply_only(source)
      source.keep_if { |key| @only.include?(key) || @keep.include?(key) }
    end

    def apply_except(source)
      source.delete_if { |key| key.nil? || @except.include?(key) }
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
synchronisable-1.3.1 lib/synchronisable/attribute_mapper.rb
synchronisable-1.3.0 lib/synchronisable/attribute_mapper.rb
synchronisable-1.2.7 lib/synchronisable/attribute_mapper.rb
synchronisable-1.2.6 lib/synchronisable/attribute_mapper.rb
synchronisable-1.2.5 lib/synchronisable/attribute_mapper.rb
synchronisable-1.2.4 lib/synchronisable/attribute_mapper.rb
synchronisable-1.2.3 lib/synchronisable/attribute_mapper.rb
synchronisable-1.2.2 lib/synchronisable/attribute_mapper.rb
synchronisable-1.2.1 lib/synchronisable/attribute_mapper.rb
synchronisable-1.2.0 lib/synchronisable/attribute_mapper.rb
synchronisable-1.1.9 lib/synchronisable/attribute_mapper.rb
synchronisable-1.1.8 lib/synchronisable/attribute_mapper.rb
synchronisable-1.1.7 lib/synchronisable/attribute_mapper.rb
synchronisable-1.1.6 lib/synchronisable/attribute_mapper.rb
synchronisable-1.1.5 lib/synchronisable/attribute_mapper.rb
synchronisable-1.1.4 lib/synchronisable/attribute_mapper.rb
synchronisable-1.1.3 lib/synchronisable/attribute_mapper.rb
synchronisable-1.1.2 lib/synchronisable/attribute_mapper.rb
synchronisable-1.1.1 lib/synchronisable/attribute_mapper.rb
synchronisable-1.1.0 lib/synchronisable/attribute_mapper.rb