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

Direct Known Subclasses

Aws, Azure, Google, Heat, Rackspace, Terraform

Defined Under Namespace

Classes: Aws, Azure, Google, Heat, Terraform

Constant Summary collapse

OpenStack =
Heat
Rackspace =
Heat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_) ⇒ SparkleStruct

Override initializer to force desired behavior



56
57
58
59
60
# File 'lib/sparkle_formation/sparkle_struct.rb', line 56

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sparkle_formation/sparkle_struct.rb', line 115

def method_missing(sym, *args, &block)
  if sym.is_a?(::String) || sym.is_a?(::Symbol)
    if sym.to_s.start_with?("_") || sym.to_s.end_with?("!")
      ::Kernel.raise ::NoMethodError.new "Undefined method `#{sym}` for #{_klass.name}"
    end
  end
  super(*[sym, *args], &block)
  if sym.is_a?(::String) || sym.is_a?(::Symbol)
    if (s = sym.to_s).end_with?("=")
      s.slice!(-1, s.length)
      sym = s
    end
    sym = _process_key(sym)
  else
    sym = function_bubbler(sym)
  end
  # When setting an AttributeStruct type instance check parent or context if
  # available and reset if it has been moved.
  if @table[sym].is_a?(::AttributeStruct)
    if @table[sym].is_a?(::SparkleFormation::FunctionStruct)
      if @table[sym].respond_to?(:_fn_context) && @table[sym]._fn_context != self
        @table[sym] = @table[sym]._clone
        @table[sym]._fn_context = self
      end
    elsif @table[sym]._parent != self
      @table[sym]._parent(self)
    end
  end
  @table[sym] = function_bubbler(@table[sym])
  @table[sym]
end

Instance Attribute Details

#_struct_classSparkleStruct

Returns:



53
54
55
# File 'lib/sparkle_formation/sparkle_struct.rb', line 53

def _struct_class
  @_struct_class
end

Instance Method Details

#__attribute_key(key) ⇒ String Originally defined in module SparkleAttribute

Format the provided key. If symbol type is provided formatting is forced. Otherwise the default formatting is applied

Parameters:

  • key (String, Symbol)

    given key

Returns:

  • (String)

    formatted key

#__t_check(val, types) ⇒ NilClass Originally defined in module Utils::TypeCheckers

Validate given value is type defined within valid types

Parameters:

  • val (Object)

    value

  • types (Class, Array<Class>)

    valid types

Returns:

  • (NilClass)

Raises:

  • (TypeError)

#__t_hashish(val) ⇒ NilClass Originally defined in module Utils::TypeCheckers

Validate given value is a Hash type

Returns:

  • (NilClass)

Raises:

  • (TypeError)

#__t_stringish(val) ⇒ NilClass Originally defined in module Utils::TypeCheckers

Validate given value is String or Symbol type

Returns:

  • (NilClass)

Raises:

  • (TypeError)

#_dynamic(resource_type, custom_name, options = {}) ⇒ self #_dynamic(dynamic_name, custom_name, options = {}) ⇒ self Also known as: dynamic! Originally defined in module SparkleAttribute

Overloads:

  • #_dynamic(resource_type, custom_name, options = {}) ⇒ self
    Note:

    All other options are set into the new resource's properties

    Insert builtin resource

    Parameters:

    • resource_type (String, Symbol)

      provider resource type

    • custom_name (String, Symbol)

      custom name used for resource name generation

    • options (Hash) (defaults to: {})

    Options Hash (options):

    • :resource_name_suffix (String, NilClass)

      custom suffix to use for name generation (defaults to resource_type)

  • #_dynamic(dynamic_name, custom_name, options = {}) ⇒ self
    Note:

    All options are passed to dynamic with custom_name

    Call custom dynamic from available sparkle packs

    Parameters:

    • dynamic_name (Symbol)

      name of registered dynamic

    • custom_name (Symbol, String)

      unique name passed directly to registered dynamic

    • options (Hash) (defaults to: {})

    Options Hash (options):

    • :provider (String, Symbol)

      override provider restriction when fetching dynamic

Yields:

  • (new_struct)

    Provides newly inserted structure

Yield Parameters:

  • new_struct (SparkleStruct)

    newly inserted structure which can be modified

Yield Returns:

  • (Object)

    discarded

Returns:

  • (self)

#_klassClass

Returns:

  • (Class)


148
149
150
# File 'lib/sparkle_formation/sparkle_struct.rb', line 148

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

#_klass_new(*args, &block) ⇒ SparkleStruct

Instantiation override properly set origin template

Returns:



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/sparkle_formation/sparkle_struct.rb', line 155

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

#_method(sym) ⇒ Method Also known as: method! Originally defined in module SparkleAttribute

Note:

usually used as puts! method!(:foo).source_location

Lookup a method definition on self

Parameters:

  • sym (Symbol)

    name of method

Returns:

  • (Method)

See Also:

  • Object#method

#_nest(template, *names, options = {}) ⇒ self Also known as: nest! Originally defined in module SparkleAttribute

Nest a stack resource

Parameters:

  • template (String, Symbol)

    name of desired template

  • names (String, Symbol)

    list of optional string/symbol values for resource name generation

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :provider (String, Symbol)

    override provider restriction when fetching template

  • :overwrite_name (Truthy, Falsey)

    when set to true, will not include template name in resource name

  • :parameters (Hash)

    compile time parameter values to pass to nested template

Yields:

  • (new_struct)

    Provides newly inserted structure

Yield Parameters:

  • new_struct (SparkleStruct)

    newly inserted structure which can be modified

Yield Returns:

  • (Object)

    discarded

Returns:

  • (self)

#_puts(obj, ...) ⇒ NilClass Also known as: puts! Originally defined in module SparkleAttribute

Print to stdout

Parameters:

  • obj (Object)

    object to print

Returns:

  • (NilClass)

See Also:

  • Kernel.puts

#_raise(*args) ⇒ Object Also known as: raise! Originally defined in module SparkleAttribute

Raise an exception

See Also:

  • Kernel.raise

#_registry(name) ⇒ Object #_registry(name, *args, options = {}) ⇒ Object Also known as: registry! Originally defined in module SparkleAttribute

Return value of registry item

Overloads:

  • #_registry(name) ⇒ Object

    Return value from registry item with given name

    Parameters:

    • name (String, Symbol)

      registry item name

  • #_registry(name, *args, options = {}) ⇒ Object
    Note:

    args and options will be passed directly to registry item when called

    Pass given parameters to registry item with given name and return the value

    Parameters:

    • name (String, Symbol)

      registry item name

    • options (Hash) (defaults to: {})
    • options (Hash) (defaults to: {})

      :provider override provider restriction when fetching registry item

    • args (Object)

      argument list

Returns:

  • (Object)

    return value of registry item

#_resource_nameString Also known as: resource_name! Originally defined in module SparkleAttribute

Return current resource name

Returns:

  • (String)

#_self(*_) ⇒ SparkleFormation

Returns:



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

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:



66
67
68
69
70
71
# File 'lib/sparkle_formation/sparkle_struct.rb', line 66

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

#_sparkle_dumpAttributeStruct::AttributeHash, Mash Also known as: sparkle_dump!

Returns dump struct to hashish

Returns:

  • (AttributeStruct::AttributeHash, Mash)

    dump struct to hashish



223
224
225
226
227
228
229
230
# File 'lib/sparkle_formation/sparkle_struct.rb', line 223

def _sparkle_dump
  processed = @table.keys.map do |key|
    value = @table[key]
    val = _sparkle_dump_unpacker(value)
    [_sparkle_dump_unpacker(key), val] unless val == UNSET_VALUE
  end.compact
  __hashish[*processed.flatten(1)]
end

#_sparkle_dump_unpacker(item) ⇒ Object

Process and unpack items for dumping within deeply nested enumerable types

Parameters:

  • item (Object)

Returns:

  • (Object)


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/sparkle_formation/sparkle_struct.rb', line 198

def _sparkle_dump_unpacker(item)
  if item.is_a?(::Enumerable)
    if item.respond_to?(:keys)
      item.class[
        *item.map do |entry|
          _sparkle_dump_unpacker(entry)
        end.flatten(1)
      ]
    else
      item.class[
        *item.map do |entry|
          _sparkle_dump_unpacker(entry)
        end
      ]
    end
  elsif item.is_a?(::AttributeStruct)
    item.nil? ? UNSET_VALUE : item._sparkle_dump
  elsif item.is_a?(::SparkleFormation)
    item.sparkle_dump
  else
    item
  end
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)

Raises:

  • (ArgumentError)


174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/sparkle_formation/sparkle_struct.rb', line 174

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

#_system(command) ⇒ String Also known as: system! Originally defined in module SparkleAttribute

Execute system command

Parameters:

  • command (String)

Returns:

  • (String)

    result

#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)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sparkle_formation/sparkle_struct.rb', line 91

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