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.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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



40
41
42
# File 'lib/mashed/mash.rb', line 40

def self.name
  "Mashed::Mash"
end

Instance Method Details

#delete(key) ⇒ Object



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

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

#inspectObject Also known as: to_s



86
87
88
# File 'lib/mashed/mash.rb', line 86

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

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

Returns:

  • (Boolean)


54
55
56
# File 'lib/mashed/mash.rb', line 54

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

#object_idObject



18
19
20
# File 'lib/mashed/mash.rb', line 18

def object_id
  __id__
end

#pretty_inspectObject

I hate pry



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

def pretty_inspect(*)
  inspect
end

#pretty_printObject



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

def pretty_print(*)
  inspect
end

#puts(*args) ⇒ Object



32
33
34
# File 'lib/mashed/mash.rb', line 32

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

#respond_to?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/mashed/mash.rb', line 82

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

#singleton_method_added(symbol) ⇒ Object



5
6
7
8
# File 'lib/mashed/mash.rb', line 5

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

#singleton_method_removed(symbol) ⇒ Object



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

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

#singleton_method_undefined(symbol) ⇒ Object



13
14
15
# File 'lib/mashed/mash.rb', line 13

def singleton_method_undefined(symbol)
  singleton_method_removed(symbol)
end

#to_hashObject Also known as: to_h



23
24
25
# File 'lib/mashed/mash.rb', line 23

def to_hash
  unwrap @hash
end

#to_json(*args) ⇒ Object



28
29
30
# File 'lib/mashed/mash.rb', line 28

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