Class: Hashr

Inherits:
Hash
  • Object
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.3"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Hashr

Returns a new instance of Hashr.



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

def initialize(data = {})
  include_modules(data.delete(:_include)) if data
  replace(deep_hasherize(deep_merge(self.class.definition, data)))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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
    self[name]
  end
end

Class Method Details

.define(definition) ⇒ Object



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

def define(definition)
  @definition = definition
end

.definitionObject



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

def definition
  @definition ||= {}
end

Instance Method Details

#[]=(key, value) ⇒ Object



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

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

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(name)
  true
end