Class: SparkleFormation::SparkleStruct

Inherits:
AttributeStruct
  • Object
show all
Includes:
SparkleAttribute, TypeCheckers, Utils::TypeCheckers
Defined in:
lib/sparkle_formation/sparkle_struct.rb

Overview

SparkleFormation customized AttributeStruct

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::TypeCheckers

#__t_check, #__t_hashish, #__t_stringish

Methods included from SparkleAttribute

#__attribute_key, #_method, #_puts, #_raise, #_resource_name, #_system, #dynamic!, #nest!, #registry!

Constructor Details

#initialize(*_) ⇒ SparkleStruct

Override initializer to force desired behavior



18
19
20
21
22
# File 'lib/sparkle_formation/sparkle_struct.rb', line 18

def initialize(*_)
  super
  @_camel_keys = true
  _set_state :hash_load_struct => true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Override to inspect result value and fetch root if value is a FunctionStruct



77
78
79
80
81
82
83
# File 'lib/sparkle_formation/sparkle_struct.rb', line 77

def method_missing(sym, *args, &block)
  if(sym.to_s.start_with?('_') || sym.to_s.end_with?('!'))
    ::Kernel.raise ::NoMethodError.new "Undefined method `#{sym}` for #{_klass.name}"
  end
  result = super(*[sym, *args], &block)
  @table[_process_key(sym)] = function_bubbler(result)
end

Instance Attribute Details

#_struct_classSparkleStruct

Returns:



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

def _struct_class
  @_struct_class
end

Instance Method Details

#_klassClass

Returns:

  • (Class)


86
87
88
# File 'lib/sparkle_formation/sparkle_struct.rb', line 86

def _klass
  _struct_class || ::SparkleFormation::SparkleStruct
end

#_klass_new(*args, &block) ⇒ SparkleStruct

Instantiation override properly set origin template

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sparkle_formation/sparkle_struct.rb', line 93

def _klass_new(*args, &block)
  inst = super()
  inst._set_self(_self)
  inst._struct_class = _struct_class
  if(args.first.is_a?(::Hash))
    inst._load(args.first)
  end
  if(block)
    inst.build!(&block)
  end
  inst
end

#_self(*_) ⇒ SparkleFormation

Returns:



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sparkle_formation/sparkle_struct.rb', line 36

def _self(*_)
  unless(@self)
    if(_parent.nil?)
      ::Kernel.raise ::ArgumentError.new 'Creator did not provide return reference!'
    else
      _parent._self
    end
  else
    @self
  end
end

#_set_self(inst) ⇒ SparkleFormation

Set SparkleFormation instance

Parameters:

Returns:



28
29
30
31
32
33
# File 'lib/sparkle_formation/sparkle_struct.rb', line 28

def _set_self(inst)
  unless(inst.is_a?(::SparkleFormation))
    ::Kernel.raise ::TypeError.new "Expecting type of `SparkleFormation` but got `#{inst.class}`"
  end
  @self = inst
end

#_state(arg) ⇒ Object Also known as: state!

Override the state to force helpful error when no value has been provided

Parameters:

  • arg (String, Symbol)

    name of parameter

Returns:

  • (Object)


112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sparkle_formation/sparkle_struct.rb', line 112

def _state(arg)
  result = super
  if(@self && result.nil?)
    if(_self.parameters.keys.map(&:to_s).include?(arg.to_s))
      unless(_self.parameters[arg.to_sym].key?(:default))
        ::Kernel.raise ::ArgumentError.new "No value provided for compile time parameter: `#{arg}`!"
      else
        result = _self.parameters[arg.to_sym][:default]
      end
    end
  end
  result
end

#function_bubbler(item) ⇒ Object

Process value in search for FunctionStruct objects. If found replace with the root item of the structure

Parameters:

  • item (Object)

Returns:

  • (Object)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sparkle_formation/sparkle_struct.rb', line 53

def function_bubbler(item)
  if(item.is_a?(::Enumerable))
    if(item.respond_to?(:keys))
      item.class[
        *item.map do |entry|
          function_bubbler(entry)
        end.flatten(1)
      ]
    else
      item.class[
        *item.map do |entry|
          function_bubbler(entry)
        end
      ]
    end
  elsif(item.is_a?(::SparkleFormation::FunctionStruct))
    item._root
  else
    item
  end
end