Class: HashWrap

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_wrap.rb,
lib/hash_wrap/json.rb,
lib/hash_wrap/yaml.rb,
lib/hash_wrap/version.rb

Constant Summary collapse

VERSION =
"0.3.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ HashWrap



4
5
6
7
8
9
10
11
12
13
# File 'lib/hash_wrap.rb', line 4

def initialize( hash )
  @hash = case hash
  when Hash
    hash.transform_keys(&:to_sym).freeze
  when HashWrap
    hash
  else
    raise "dunno how to wrap #{hash.inspect}"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing(meth, *args, &blk)
  # handle both string and symbol keys
  value = @hash.fetch meth.to_s do |_key|
    @hash.fetch meth do |_key|
      super
    end
  end
  __wrap value
end

Class Method Details

.of_json(json) ⇒ Object

really a convenience method



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/hash_wrap/json.rb', line 5

def self.of_json json
  vs = JSON.parse json, symbolize_names: true
  case vs
  when Array
    vs.map{|obj| new obj}
  when Hash
    new vs
  else
    raise "Cannot construct #{self.class} from json in #{json.inspect}"
  end
end

.of_yaml(yaml) ⇒ Object

really a convenience method



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/hash_wrap/yaml.rb', line 5

def self.of_yaml yaml
  vs = YAML.load yaml
  case vs
  when Array
    vs.map{|obj| new obj}
  when Hash
    new vs
  else
    raise "Cannot construct #{self.class} from yaml in #{yaml.inspect}"
  end
end

Instance Method Details

#==(rhs) ⇒ Object



44
45
46
# File 'lib/hash_wrap.rb', line 44

def == rhs
  @hash == __hash__
end

#aiObject

for awesome_print



90
# File 'lib/hash_wrap.rb', line 90

def ai; inspect end

#deconstructObject

Raises:

  • (NotImplementedError)


64
65
66
# File 'lib/hash_wrap.rb', line 64

def deconstruct
  raise NotImplementedError
end

#deconstruct_keys(keys) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/hash_wrap.rb', line 68

def deconstruct_keys keys
  if keys.nil? || keys.empty?
    @hash
  else
    @hash.slice(*keys).map{|k,v| [k, __wrap(v)]}.to_h
  end
end

#dig(*parts) ⇒ Object



60
61
62
# File 'lib/hash_wrap.rb', line 60

def dig *parts
  @hash.dig *parts
end

#each(&blk) ⇒ Object



48
49
50
51
52
53
# File 'lib/hash_wrap.rb', line 48

def each &blk
  return enum_for :each unless block_given?
  @hash.each do |k,v|
    yield [k,__wrap(v)]
  end
end

#inspectObject



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

def inspect
  @hash.inspect
end

#pretty_print(pp) ⇒ Object

have to handle this otherwise pry gets upset



85
86
87
# File 'lib/hash_wrap.rb', line 85

def pretty_print(pp)
  pp.text inspect
end

#respond_to_missing?(meth, _include_private_methods) ⇒ Boolean



25
26
27
# File 'lib/hash_wrap.rb', line 25

def respond_to_missing?(meth, _include_private_methods)
  @hash.has_key? meth
end

#to_hObject



55
56
57
58
# File 'lib/hash_wrap.rb', line 55

def to_h
  # no, you can't have a copy of this hash to mess with
  @hash.dup
end

#to_sObject



76
77
78
# File 'lib/hash_wrap.rb', line 76

def to_s
  @hash.to_s
end