Class: SparkleFormation::Sparkle

Inherits:
Object
  • Object
show all
Includes:
Bogo::Memoization
Defined in:
lib/sparkle_formation/sparkle.rb

Overview

Independent collection of SparkleFormation items

Direct Known Subclasses

SparkleCollection

Constant Summary collapse

VALID_ROOT_DIRS =

Valid directories from cwd to set as root

[
  "sparkleformation",
  "sfn",
  "cloudformation",
  "cfn",
  ".",
]
DIRS =

Reserved directories

[
  "components",
  "registry",
  "dynamics",
]
TYPES =

Valid types

Smash.new(
  "component" => "components",
  "registry" => "registries",
  "dynamic" => "dynamics",
  "template" => "templates",
)
@@_pack_registry =
Smash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ self

Create new sparkle instance

Parameters:

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

Options Hash (args):

  • :root (String)

    path to sparkle directories

  • :name (String, Symbol)

    registered pack name

  • :provider (String, Symbol)

    name of default provider



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/sparkle_formation/sparkle.rb', line 235

def initialize(args = {})
  if args[:name]
    @root = self.class.path(args[:name])
  else
    @root = args.fetch(:root, locate_root)
  end
  if @root != :none && !File.directory?(@root)
    raise Errno::ENOENT.new("No such directory - #{@root}")
  end
  @raw_data = Smash.new(
    :dynamic => [],
    :component => [],
    :registry => [],
    :template => [],
  )
  @provider = Bogo::Utility.snake(args.fetch(:provider, "aws").to_s).to_sym
  @wrapper = eval_wrapper
  wrapper.part_data(raw_data)
  load_parts! unless @root == :none
end

Instance Attribute Details

#providerSymbol

Returns provider

Returns:

  • (Symbol)

    provider



226
227
228
# File 'lib/sparkle_formation/sparkle.rb', line 226

def provider
  @provider
end

#raw_dataSmash (readonly)

Returns raw part data

Returns:

  • (Smash)

    raw part data



224
225
226
# File 'lib/sparkle_formation/sparkle.rb', line 224

def raw_data
  @raw_data
end

#rootString (readonly)

Returns path to sparkle directories

Returns:

  • (String)

    path to sparkle directories



222
223
224
# File 'lib/sparkle_formation/sparkle.rb', line 222

def root
  @root
end

Class Method Details

.path(name) ⇒ String

Return the path to the SparkePack registered with the given name

Parameters:

  • name (String, Symbol)

    name of pack

Returns:

  • (String)

    path



53
54
55
56
57
58
59
# File 'lib/sparkle_formation/sparkle.rb', line 53

def path(name)
  if @@_pack_registry[name]
    @@_pack_registry[name]
  else
    raise KeyError.new "No pack registered with requested name: #{name}!"
  end
end

.register!(name = nil, path = nil) ⇒ Array<String:name, String:path>

Register a SparklePack for short name access

Parameters:

  • name (String, Symbol) (defaults to: nil)

    name of pack

  • path (String) (defaults to: nil)

    path to pack

Returns:

  • (Array<String:name, String:path>)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sparkle_formation/sparkle.rb', line 15

def register!(name = nil, path = nil)
  unless path
    idx = caller.index do |item|
      item.end_with?("`register!'")
    end
    idx = idx ? idx.next : 0
    # Trim from the end to determine path allowing windows paths
    # to not be improperly truncated
    file = caller[idx].split(":").reverse.drop(2).reverse.join(":")
    path = File.join(File.dirname(file), "sparkleformation")
    unless File.directory?(path)
      path = nil
    end
    unless name
      name = File.basename(file)
      name.sub!(File.extname(name), "")
    end
  end
  unless name
    if path
      name = path.split(File::PATH_SEPARATOR)[-3].to_s
    end
  end
  unless path
    raise ArgumentError.new("No SparklePack path provided and failed to auto-detect!")
  end
  unless name
    raise ArgumentError.new("No SparklePack name provided and failed to auto-detect!")
  end
  @@_pack_registry[name] = path
  [name, path]
end

Instance Method Details

#componentsSmash<name:block>

Returns:

  • (Smash<name:block>)


257
258
259
260
261
# File 'lib/sparkle_formation/sparkle.rb', line 257

def components
  memoize(:components) do
    Smash.new
  end
end

#dynamicsSmash<name:block>

Returns:

  • (Smash<name:block>)


264
265
266
267
268
# File 'lib/sparkle_formation/sparkle.rb', line 264

def dynamics
  memoize(:dynamics) do
    Smash.new
  end
end

#eval_wrapperObject

Wrapper for evaluating sfn files to store within sparkle container



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/sparkle_formation/sparkle.rb', line 64

def eval_wrapper
  # Evaluation context wrapper for loading SparkleFormation files
  Class.new(BasicObject).new.instance_exec do
    wrapper = ::Class.new(BasicObject)
    wrapper.class_eval "
    class SparkleFormation
      attr_accessor :sparkle_path

      class << self
        def insert(*args, &block)
          ::SparkleFormation.insert(*args, &block)
        end

        def part_data(data = nil)
          if data
            @data = data
          else
            @data
          end
        end

        def dynamic(name, args = {}, &block)
          part_data[:dynamic].push(
            ::Smash.new(
              :name => name,
              :block => block,
              :args => ::Smash[
                args.map(&:to_a)
              ],
              :type => :dynamic,
            )
          ).last
        end

        def build(&block)
          part_data[:component].push(
            ::Smash.new(
              :block => block,
              :type => :component,
            )
          ).last
        end

        def component(name, args = {}, &block)
          part_data[:component].push(
            ::Smash.new(
              :name => name,
              :block => block,
              :args => ::Smash[
                args.map(&:to_a)
              ],
              :type => :component,
            )
          ).last
        end

        def dynamic_info(*args)
          ::Smash.new(:metadata => {}, :args => {})
        end
      end

      def initialize(*args)
        opts = args.detect { |a| a.is_a?(Hash) } || {}
        SparkleFormation.part_data[:template].push(
          ::Smash.new(
            :name => args.first,
            :args => opts,
          )
        )
        raise ::TypeError
      end

      class Registry
        def self.register(name, args = {}, &block)
          SparkleFormation.part_data[:registry].push(
            ::Smash.new(
              :name => name,
              :block => block,
              :args => ::Smash[
                args.map(&:to_a)
              ],
              :type => :registry,
            )
          ).last
        end
      end
    end

    SfnRegistry = SparkleFormation::Registry

    # @!visibility private
    def require(*args)
      ::Kernel.require(*args)
    end

    # NOTE: Enable access to top level constants but do not
    # include deprecated constants to prevent warning outputs
    deprecated_constants = [
      :Data,
      :Config,
      :TimeoutError,
      :Fixnum,
      :Bignum,
      :NIL,
      :TRUE,
      :FALSE,
    ]
    ::Object.constants.each do |const|
      unless self.const_defined?(const) # rubocop:disable Style/RedundantSelf
        next if deprecated_constants.include?(const)
        self.const_set(const, ::Object.const_get(const)) # rubocop:disable Style/RedundantSelf
      end
    end

    # @!visibility private
    def part_data(arg)
      SparkleFormation.part_data(arg)
    end

    def __sparkle_formation_id
      SparkleFormation.object_id
    end

    def __sfn_registry_id
      SfnRegistry.object_id
    end
    "
    wrapper.new
  end
end

#get(type, name, target_provider = nil) ⇒ Smash

Request item from the store

Parameters:

  • type (String, Symbol)

    item type (see: TYPES)

  • name (String, Symbol)

    name of item

  • target_provider (String, Symbol) (defaults to: nil)

    restrict to provider

Returns:

  • (Smash)

    requested item

Raises:



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/sparkle_formation/sparkle.rb', line 291

def get(type, name, target_provider = nil)
  unless TYPES.keys.include?(type.to_s)
    raise NameError.new "Invalid type requested (#{type})! Valid types: #{TYPES.keys.join(", ")}"
  end
  unless target_provider
    target_provider = provider
  end
  result = send(TYPES[type]).get(target_provider, name)
  if result.nil? && TYPES[type] == "templates"
    result = (send(TYPES[type]).fetch(target_provider, Smash.new).detect { |_, v|
      name = name.to_s
      short_name = v[:path].sub(%r{#{Regexp.escape(root)}/?}, "")
      v[:path] == name ||
      short_name == name ||
      short_name.sub(".rb", "").gsub(File::SEPARATOR, "__").tr("-", "_") == name ||
      v[:path].end_with?(name)
    } || []).last
  end
  unless result
    klass = Error::NotFound.const_get(type.capitalize)
    raise klass.new("No #{type} registered with requested name (#{name})!", :name => name)
  end
  result
end

#inspectString

Returns:

  • (String)


317
318
319
# File 'lib/sparkle_formation/sparkle.rb', line 317

def inspect
  "<SparkleFormation::Sparkle [root: #{root.inspect}]>"
end

#registriesSmash<name:block>

Returns:

  • (Smash<name:block>)


271
272
273
274
275
# File 'lib/sparkle_formation/sparkle.rb', line 271

def registries
  memoize(:registries) do
    Smash.new
  end
end

#templatesSmash<name:path>

Returns:

  • (Smash<name:path>)


278
279
280
281
282
# File 'lib/sparkle_formation/sparkle.rb', line 278

def templates
  memoize(:templates) do
    Smash.new
  end
end