Class: Mashie

Inherits:
Hash
  • Object
show all
Defined in:
lib/kurchatov/mashie.rb

Instance Method Summary collapse

Constructor Details

#initialize(source_hash = nil, default = nil, &blk) ⇒ Mashie

Returns a new instance of Mashie.



3
4
5
6
# File 'lib/kurchatov/mashie.rb', line 3

def initialize(source_hash = nil, default = nil, &blk)
  deep_update(source_hash) if source_hash
  default ? super(default) : super(&blk)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/kurchatov/mashie.rb', line 119

def method_missing(method_name, *args, &blk)
  return self.[](method_name, &blk) if key?(method_name)
  match = method_name.to_s.match(/(.*?)([?=!_]?)$/)
  case match[2]
    when '='
      self[match[1]] = args.first
    when '?'
      !!self[match[1]]
    when '!'
      initializing_reader(match[1])
    when '_'
      underbang_reader(match[1])
    else
      default(method_name, *args, &blk)
  end
end

Instance Method Details

#custom_reader(key) {|value| ... } ⇒ Object Also known as: []

Yields:

  • (value)


23
24
25
26
27
# File 'lib/kurchatov/mashie.rb', line 23

def custom_reader(key)
  value = regular_reader(convert_key(key))
  yield value if block_given?
  value
end

#custom_writer(key, value) ⇒ Object Also known as: []=

:nodoc:



29
30
31
# File 'lib/kurchatov/mashie.rb', line 29

def custom_writer(key, value) #:nodoc:
  regular_writer(convert_key(key), convert_value(value))
end

#deep_merge(other_hash, &blk) ⇒ Object Also known as: merge



73
74
75
# File 'lib/kurchatov/mashie.rb', line 73

def deep_merge(other_hash, &blk)
  dup.deep_update(other_hash, &blk)
end

#deep_update(other_hash, &blk) ⇒ Object Also known as: deep_merge!, update



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kurchatov/mashie.rb', line 79

def deep_update(other_hash, &blk)
  other_hash.each_pair do |k, v|
    key = convert_key(k)
    if regular_reader(key).is_a?(Mash) and v.is_a?(::Hash)
      custom_reader(key).deep_update(v, &blk)
    else
      value = convert_value(v, true)
      value = blk.call(key, self[k], value) if blk
      custom_writer(key, value)
    end
  end
  self
end

#delete(key) ⇒ Object



55
56
57
# File 'lib/kurchatov/mashie.rb', line 55

def delete(key)
  super(convert_key(key))
end

#dupObject

Duplicates the current mash as a new mash.



61
62
63
# File 'lib/kurchatov/mashie.rb', line 61

def dup
  self.class.new(self, self.default)
end

#fetch(key, *args) ⇒ Object



51
52
53
# File 'lib/kurchatov/mashie.rb', line 51

def fetch(key, *args)
  super(convert_key(key), *args)
end

#idObject

:nodoc:



12
13
14
# File 'lib/kurchatov/mashie.rb', line 12

def id #:nodoc:
  self['id']
end

#initializing_reader(key) ⇒ Object



36
37
38
39
40
# File 'lib/kurchatov/mashie.rb', line 36

def initializing_reader(key)
  ck = convert_key(key)
  regular_writer(ck, self.class.new) unless key?(ck)
  regular_reader(ck)
end

#key?(key) ⇒ Boolean Also known as: has_key?, include?, member?

Returns:

  • (Boolean)


65
66
67
# File 'lib/kurchatov/mashie.rb', line 65

def key?(key)
  super(convert_key(key))
end

#regular_dupObject



59
# File 'lib/kurchatov/mashie.rb', line 59

alias_method :regular_dup, :dup

#replace(other_hash) ⇒ Object



108
109
110
111
112
# File 'lib/kurchatov/mashie.rb', line 108

def replace(other_hash)
  (keys - other_hash.keys).each { |key| delete(key) }
  other_hash.each { |key, value| self[key] = value }
  self
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/kurchatov/mashie.rb', line 114

def respond_to?(method_name, include_private=false)
  return true if key?(method_name) || method_name.to_s.slice(/[=?!_]\Z/)
  super
end

#shallow_merge(other_hash) ⇒ Object



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

def shallow_merge(other_hash)
  dup.shallow_update(other_hash)
end

#shallow_update(other_hash) ⇒ Object



101
102
103
104
105
106
# File 'lib/kurchatov/mashie.rb', line 101

def shallow_update(other_hash)
  other_hash.each_pair do |k, v|
    regular_writer(convert_key(k), convert_value(v, true))
  end
  self
end

#typeObject

:nodoc:



16
17
18
# File 'lib/kurchatov/mashie.rb', line 16

def type #:nodoc:
  self['type']
end

#underbang_reader(key) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/kurchatov/mashie.rb', line 42

def underbang_reader(key)
  ck = convert_key(key)
  if key?(ck)
    regular_reader(ck)
  else
    self.class.new
  end
end