Module: HashMapper

Defined in:
lib/hash_mapper.rb,
lib/hash_mapper/version.rb

Defined Under Namespace

Classes: Map, PathMap

Constant Summary collapse

DEFAULT_OPTIONS =
{}.freeze
NO_VALUE =
:hash_mapper_no_value
NO_DEFAULT =
:hash_mapper_no_default
VERSION =
"0.2.7"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hash_mapper.rb', line 48

def self.extended(base)
  base.class_eval do
    class_attribute :maps, :before_normalize_filters,
      :before_denormalize_filters, :after_normalize_filters,
      :after_denormalize_filters

    self.maps = []
    self.before_normalize_filters = []
    self.before_denormalize_filters = []
    self.after_normalize_filters = []
    self.after_denormalize_filters = []
  end
end

Instance Method Details

#after_denormalize(&blk) ⇒ Object



101
102
103
# File 'lib/hash_mapper.rb', line 101

def after_denormalize(&blk)
  self.after_denormalize_filters = self.after_denormalize_filters + [blk]
end

#after_normalize(&blk) ⇒ Object



97
98
99
# File 'lib/hash_mapper.rb', line 97

def after_normalize(&blk)
  self.after_normalize_filters = self.after_normalize_filters + [blk]
end

#before_denormalize(&blk) ⇒ Object



93
94
95
# File 'lib/hash_mapper.rb', line 93

def before_denormalize(&blk)
  self.before_denormalize_filters = self.before_denormalize_filters + [blk]
end

#before_normalize(&blk) ⇒ Object



89
90
91
# File 'lib/hash_mapper.rb', line 89

def before_normalize(&blk)
  self.before_normalize_filters = self.before_normalize_filters + [blk]
end

#denormalize(a_hash, options: DEFAULT_OPTIONS, context: nil) ⇒ Object



85
86
87
# File 'lib/hash_mapper.rb', line 85

def denormalize(a_hash, options: DEFAULT_OPTIONS, context: nil)
  perform_hash_mapping a_hash, :denormalize, options: options, context: context
end

#from(path, &filter) ⇒ Object Also known as: to



68
69
70
71
72
# File 'lib/hash_mapper.rb', line 68

def from(path, &filter)
  path_map = PathMap.new(path)
  path_map.filter = filter if block_given? # Useful if two blocks given
  path_map
end

#map(from, to, options = {}, &filter) ⇒ Object



62
63
64
65
66
# File 'lib/hash_mapper.rb', line 62

def map(from, to, options={}, &filter)
  self.maps = self.maps.dup
  self.maps << Map.new(from, to, options)
  to.filter = filter if block_given? # Useful if just one block given
end

#normalize(a_hash, options: DEFAULT_OPTIONS, context: nil) ⇒ Object



81
82
83
# File 'lib/hash_mapper.rb', line 81

def normalize(a_hash, options: DEFAULT_OPTIONS, context: nil)
  perform_hash_mapping a_hash, :normalize, options: options, context: context
end

#using(mapper_class) ⇒ Object



76
77
78
79
# File 'lib/hash_mapper.rb', line 76

def using(mapper_class)
  warn "[DEPRECATION] `using` is deprecated, instead of `using(#{mapper_class.name})` you should specify `{ using: #{mapper_class.name} }`"
  { using: mapper_class }
end