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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mashed/mash.rb', line 61

def method_missing(symbol, *args, &blk)
  string = symbol.to_s
  if blk
    super
  elsif args.length == 0
    if @hash.key?(string)
      self[string]
    elsif string =~ /\?$/
      !!self[string[0..-2]]
    else
      nil
    end
  elsif args.length == 1 && string =~ /=$/
    self[string[0..-2]] = args.first
  else
    super
  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

#delete(key) ⇒ Object



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

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

#inspectObject Also known as: to_s



84
85
86
# File 'lib/mashed/mash.rb', line 84

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

#object_idObject



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

def object_id
  __id__
end

#pretty_inspectObject

I hate pry



90
91
92
# File 'lib/mashed/mash.rb', line 90

def pretty_inspect(*)
  inspect
end

#pretty_printObject



94
95
96
# File 'lib/mashed/mash.rb', line 94

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)


80
81
82
# File 'lib/mashed/mash.rb', line 80

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