Class: Smash

Inherits:
Mash
  • Object
show all
Defined in:
lib/racker/smash/smash.rb

Overview

This class wraps mash and adds extended smart functionality

Direct Known Subclasses

Racker::Template

Instance Method Summary collapse

Methods inherited from Mash

#[]=, #default, #delete, #except, #fetch, from_hash, #initialize, #initialize_copy, #key?, #merge, #regular_update, #regular_writer, #stringify_keys!, #symbolize_keys, #to_hash, #update, #values_at

Constructor Details

This class inherits a constructor from Mash

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/racker/smash/smash.rb', line 46

def method_missing(symbol, *args)
  if symbol == :to_ary
    super
  elsif args.empty?
    self[symbol]
  elsif symbol.to_s =~ /=$/
    key_to_set = symbol.to_s[/^(.+)=$/, 1]
    self[key_to_set] = (args.length == 1 ? args[0] : args)
  else
    raise NoMethodError, "Undefined key or method `#{symbol}' on `Smash`."
  end
end

Instance Method Details

#[](key) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/racker/smash/smash.rb', line 7

def [](key)
  if !key?(key)
    self[key] = self.class.new
  else
    super
  end
end

#compact(opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/racker/smash/smash.rb', line 15

def compact(opts={})
  inject(self.class.new) do |new_hash, (k,v)|
    unless v.nil?
      new_hash[k] = opts[:recurse] && v.class == self.class ? v.compact(opts) : v
    end
    new_hash
  end
end

#convert_value(value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/racker/smash/smash.rb', line 24

def convert_value(value)
  case value
  when Smash
    value
  when Mash
    self.class.new(value)
  when Hash
    self.class.new(value)
  else
    value
  end
end

#deep_merge!(source, options = {}) ⇒ Object



37
38
39
40
# File 'lib/racker/smash/smash.rb', line 37

def deep_merge!(source, options = {})
  default_opts = {:preserve_unmergeables => false}
  DeepMergeModified::deep_merge!(source, self, default_opts.merge(options))
end

#dupObject



42
43
44
# File 'lib/racker/smash/smash.rb', line 42

def dup
  self.class.new(self)
end