Class: Hashr

Inherits:
Hash show all
Defined in:
lib/hashr.rb,
lib/hashr/version.rb,
lib/hashr/env_defaults.rb

Defined Under Namespace

Modules: EnvDefaults

Constant Summary collapse

TEMPLATE =
new
VERSION =
"0.0.7"

Constants inherited from Hash

Hash::MERGER

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#deep_merge, #deep_symbolize_keys, #except, #slice

Constructor Details

#initialize(data = {}, definition = self.class.definition) ⇒ Hashr

Returns a new instance of Hashr.



20
21
22
# File 'lib/hashr.rb', line 20

def initialize(data = {}, definition = self.class.definition)
  replace(deep_hashrize(definition.deep_merge(data.deep_symbolize_keys)))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing(name, *args, &block)
  case name.to_s[-1, 1]
  when '?'
    !!self[name.to_s[0..-2].to_sym]
  when '='
    self[name.to_s[0..-2].to_sym] = args.first
  else
    raise(IndexError.new("Key #{name.inspect} is not defined.")) if !key?(name) && self.class.raise_missing_keys
    self[name]
  end
end

Class Attribute Details

.raise_missing_keysObject

Returns the value of attribute raise_missing_keys.



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

def raise_missing_keys
  @raise_missing_keys
end

Class Method Details

.define(definition) ⇒ Object



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

def define(definition)
  @definition = definition.deep_symbolize_keys
end

.definitionObject



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

def definition
  @definition ||= {}
end

Instance Method Details

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  super(key, value.is_a?(Hash) ? self.class.new(value, {}) : value)
end

#include_modules(modules) ⇒ Object



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

def include_modules(modules)
  Array(modules).each { |mod| meta_class.send(:include, mod) } if modules
end

#meta_classObject



48
49
50
# File 'lib/hashr.rb', line 48

def meta_class
  class << self; self end
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(name)
  true
end