Class: Mashed::Mash

Inherits:
BasicObject
Defined in:
lib/mashed/mash.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Mash

Returns a new instance of Mash.



42
43
44
45
46
47
48
49
50
# File 'lib/mashed/mash.rb', line 42

def initialize(hash)
  @singleton_methods ||= []
  hash = if hash.respond_to?(:to_h)
    hash.to_h
  else
    hash.to_hash
  end
  @hash = hash.stringify
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mashed/mash.rb', line 75

def method_missing(symbol, *args, &blk)
  string = symbol.to_s
  if @hash.key?(string)
    self[string]
  elsif string =~ /\?$/
    !!self[string[0..-2]]
  elsif string =~ /=$/
    self[string[0..-2]] = args.first
  else
    nil
  end
end

Class Method Details

.nameObject



38
39
40
# File 'lib/mashed/mash.rb', line 38

def self.name
  "Mashed::Mash"
end

Instance Method Details

#[](key) ⇒ Object



57
58
59
60
# File 'lib/mashed/mash.rb', line 57

def [](key)
  key = key.to_s
  wrap_up @hash[key]
end

#[]=(key, value) ⇒ Object



62
63
64
65
# File 'lib/mashed/mash.rb', line 62

def []=(key, value)
  key = key.to_s
  @hash[key] = value
end

#delete(key) ⇒ Object



71
72
73
# File 'lib/mashed/mash.rb', line 71

def delete(key)
  wrap_up @hash.delete(key)
end

#inspectObject Also known as: to_s



92
93
94
# File 'lib/mashed/mash.rb', line 92

def inspect
  "#<Mashed::Mash @hash=>#{@hash.inspect}>"
end

#is_a?(other) ⇒ Boolean Also known as: kind_of?

Returns:

  • (Boolean)


52
53
54
# File 'lib/mashed/mash.rb', line 52

def is_a?(other)
  other == self.class
end

#keysObject



67
68
69
# File 'lib/mashed/mash.rb', line 67

def keys
  @hash.keys
end

#object_idObject



16
17
18
# File 'lib/mashed/mash.rb', line 16

def object_id
  __id__
end

#pretty_inspectObject



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

def pretty_inspect
  inspect
end

#pretty_printObject



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

def pretty_print
  inspect
end

#puts(*args) ⇒ Object



30
31
32
# File 'lib/mashed/mash.rb', line 30

def puts(*args)
  ::Kernel.puts(*args)
end

#respond_to?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/mashed/mash.rb', line 88

def respond_to?(symbol)
  methods.map(&:to_s).include?(symbol.to_s)
end

#singleton_method_added(symbol) ⇒ Object



3
4
5
6
# File 'lib/mashed/mash.rb', line 3

def singleton_method_added(symbol)
  @singleton_methods ||= []
  @singleton_methods << symbol
end

#singleton_method_removed(symbol) ⇒ Object



7
8
9
10
# File 'lib/mashed/mash.rb', line 7

def singleton_method_removed(symbol)
  @singleton_methods ||= []
  @singleton_methods.delete symbol
end

#singleton_method_undefined(symbol) ⇒ Object



11
12
13
# File 'lib/mashed/mash.rb', line 11

def singleton_method_undefined(symbol)
  singleton_method_removed(symbol)
end

#to_hashObject Also known as: to_h



21
22
23
# File 'lib/mashed/mash.rb', line 21

def to_hash
  unwrap @hash
end

#to_json(*args) ⇒ Object



26
27
28
# File 'lib/mashed/mash.rb', line 26

def to_json(*args)
  to_hash.to_json(*args)
end