Class: Hashcraft::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Dsl
Defined in:
lib/hashcraft/base.rb

Overview

Super-class for craftable objects.

Instance Attribute Summary

Attributes included from Dsl

#local_key_transformer, #local_value_transformer

Instance Method Summary collapse

Methods included from Dsl

find_option, key_transformer, key_transformer_to_use, local_option_set, option, option_set, value_transformer, value_transformer_to_use

Constructor Details

#initialize(opts = {}, &block) ⇒ Base

Returns a new instance of Base.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hashcraft/base.rb', line 25

def initialize(opts = {}, &block)
  @data = {}

  load_default_data
  load_opts(opts)

  return unless block_given?

  if block.arity == 1
    yield self
  else
    instance_eval(&block)
  end
end

Instance Method Details

#to_hObject

Main compilation method. Once an object is hydrated, you can call this method to get the materialized hash.



42
43
44
45
46
47
48
# File 'lib/hashcraft/base.rb', line 42

def to_h
  data.each_with_object({}) do |(key, value), memo|
    method = value.is_a?(Array) ? :evaluate_values! : :evaluate_value!

    send(method, memo, key, value)
  end
end