Class: Hashr

Inherits:
BasicObject
Defined in:
lib/hashr.rb,
lib/hashr/env.rb,
lib/hashr/version.rb,
lib/hashr/delegate/conditional.rb

Defined Under Namespace

Modules: Delegate, Env

Constant Summary collapse

VERSION =
'2.0.1'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, data = nil, defaults = nil, &block) ⇒ Hashr

Returns a new instance of Hashr.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hashr.rb', line 30

def initialize(klass, data = nil, defaults = nil, &block)
  ::Kernel.fail ::ArgumentError.new("Invalid input #{data.inspect}") unless data.nil? || data.is_a?(::Hash)

  data = (data || {}).deep_symbolize_keys
  defaults = (defaults || klass.defaults || {}).deep_symbolize_keys

  @class = klass
  @data = defaults.deep_merge(data).inject({}) do |result, (key, value)|
    result.merge(key => value.is_a?(::Hash) ? ::Hashr.new(value, {}) : value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/hashr.rb', line 62

def method_missing(name, *args, &block)
  case name.to_s[-1, 1]
  when '?'
    !!self[name.to_s[0..-2]]
  when '='
    self[name.to_s[0..-2]] = args.first
  else
    self[name]
  end
end

Class Attribute Details

.defaultsObject (readonly)

Returns the value of attribute defaults.



8
9
10
# File 'lib/hashr.rb', line 8

def defaults
  @defaults
end

Instance Attribute Details

#classObject (readonly)

Returns the value of attribute class.



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

def class
  @class
end

Class Method Details

.const_missing(name) ⇒ Object



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

def const_missing(name)
  Kernel.const_get(name)
end

.default(defaults) ⇒ Object Also known as: define



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

def default(defaults)
  @defaults = (self.defaults || {}).deep_merge(defaults || {})
end

.inherited(other) ⇒ Object



10
11
12
# File 'lib/hashr.rb', line 10

def inherited(other)
  other.default(defaults)
end

.new(*args) ⇒ Object



14
15
16
# File 'lib/hashr.rb', line 14

def new(*args)
  super(self, *args)
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  to_h == other.to_h if other.respond_to?(:to_h)
end

#[](key) ⇒ Object



46
47
48
# File 'lib/hashr.rb', line 46

def [](key)
  @data[to_key(key)]
end

#[]=(key, value) ⇒ Object



50
51
52
# File 'lib/hashr.rb', line 50

def []=(key, value)
  @data.store(to_key(key), value.is_a?(::Hash) ? ::Hashr.new(value, {}) : value)
end

#defined?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def defined?(key)
  @data.key?(to_key(key))
end

#inspectObject



99
100
101
# File 'lib/hashr.rb', line 99

def inspect
  "<#{self.class.name} #{@data.inspect}>"
end

#instance_of?(const) ⇒ Boolean

Returns:

  • (Boolean)


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

def instance_of?(const)
  self.class == const
end

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

Returns:

  • (Boolean)


92
93
94
95
96
# File 'lib/hashr.rb', line 92

def is_a?(const)
  consts = [self.class]
  consts << consts.last.superclass while consts.last.superclass
  consts.include?(const)
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/hashr.rb', line 58

def respond_to?(*args)
  true
end

#to_hObject Also known as: to_hash



77
78
79
80
81
# File 'lib/hashr.rb', line 77

def to_h
  @data.inject({}) do |hash, (key, value)|
    hash.merge(key => value.is_a?(Hashr) || value.is_a?(Hash) ? value.to_h : value)
  end
end

#try(key) ⇒ Object



73
74
75
# File 'lib/hashr.rb', line 73

def try(key)
  defined?(key) ? self[key] : nil # TODO needs to look for to_h etc
end

#values_at(*keys) ⇒ Object



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

def values_at(*keys)
  keys.map { |key| self[key] }
end