class Synchronisable::AttributeMapper

Public Class Methods

map(source, mappings, options = {}) click to toggle source
# File lib/synchronisable/attribute_mapper.rb, line 4
def map(source, mappings, options = {})
  new(mappings, options).map(source)
end
new(mappings, options = {}) click to toggle source
# File lib/synchronisable/attribute_mapper.rb, line 9
def initialize(mappings, options = {})
  @mappings = mappings
  @keep = options[:keep] || []
  @only, @except = options[:only], options[:except]
end

Public Instance Methods

map(source) click to toggle source
# File lib/synchronisable/attribute_mapper.rb, line 15
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 Instance Methods

apply_except(source) click to toggle source
# File lib/synchronisable/attribute_mapper.rb, line 35
def apply_except(source)
  source.delete_if { |key| key.nil? || @except.include?(key) }
end
apply_mappings(source) click to toggle source
# File lib/synchronisable/attribute_mapper.rb, line 27
def apply_mappings(source)
  source.transform_keys! { |key| @mappings[key] || key }
end
apply_only(source) click to toggle source
# File lib/synchronisable/attribute_mapper.rb, line 31
def apply_only(source)
  source.keep_if { |key| @only.include?(key) || @keep.include?(key) }
end