Class: SparkleFormation::SparkleCollection

Inherits:
Sparkle
  • Object
show all
Defined in:
lib/sparkle_formation/sparkle_collection.rb

Overview

TODO:

add unmemoize behavior on collection modification to prevent

Provides a collection of sparkles leak on long running processes with long lasting collections

Constant Summary

Constants inherited from Sparkle

SparkleFormation::Sparkle::DIRS, SparkleFormation::Sparkle::TYPES, SparkleFormation::Sparkle::VALID_ROOT_DIRS

Instance Attribute Summary

Attributes inherited from Sparkle

#raw_data, #root

Instance Method Summary collapse

Methods inherited from Sparkle

#eval_wrapper, #inspect, path, register!

Constructor Details

#initialize(*_) ⇒ self

Create a new collection of sparkles



12
13
14
15
# File 'lib/sparkle_formation/sparkle_collection.rb', line 12

def initialize(*_)
  @root = nil
  @sparkles = []
end

Instance Method Details

#add_sparkle(sparkle, precedence = :high) ⇒ self

Add new sparkle to collection

Parameters:

Returns:

  • (self)


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sparkle_formation/sparkle_collection.rb', line 30

def add_sparkle(sparkle, precedence=:high)
  unless(sparkle.is_a?(Sparkle))
    raise TypeError.new "Expected type `SparkleFormation::Sparkle` but received `#{sparkle.class}`!"
  end
  if(precedence == :high)
    @sparkles.push(sparkle).uniq!
  else
    @sparkles.unshift(sparkle).uniq!
  end
  self
end

#componentsSmash

Returns:

  • (Smash)


67
68
69
70
71
72
73
74
75
# File 'lib/sparkle_formation/sparkle_collection.rb', line 67

def components
  memoize("components_#{checksum}") do
    Smash.new.tap do |hsh|
      sparkles.each do |sprkl|
        hsh.merge!(sprkl.components)
      end
    end
  end
end

#dynamicsSmash

Returns:

  • (Smash)


78
79
80
81
82
83
84
85
86
# File 'lib/sparkle_formation/sparkle_collection.rb', line 78

def dynamics
  memoize("dynamics_#{checksum}") do
    Smash.new.tap do |hsh|
      sparkles.each do |sprkl|
        hsh.merge!(sprkl.dynamics)
      end
    end
  end
end

#empty?TrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)


62
63
64
# File 'lib/sparkle_formation/sparkle_collection.rb', line 62

def empty?
  size == 0
end

#get(*args) ⇒ Smash

Request item from the store

Parameters:

  • type (String, Symbol)

    item type (see: TYPES)

  • name (String, Symbol)

    name of item

Returns:

  • (Smash)

    requested item



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sparkle_formation/sparkle_collection.rb', line 116

def get(*args)
  result = nil
  error = nil
  sparkles.each do |sprkl|
    begin
      result = sprkl.get(*args)
    rescue Error::NotFound => error
    end
  end
  if(result)
    result
  else
    raise error
  end
end

#registriesSmash

Returns:

  • (Smash)


89
90
91
92
93
94
95
96
97
# File 'lib/sparkle_formation/sparkle_collection.rb', line 89

def registries
  memoize("registries_#{checksum}") do
    Smash.new.tap do |hsh|
      sparkles.each do |sprkl|
        hsh.merge!(sprkl.registries)
      end
    end
  end
end

#remove_sparkle(sparkle) ⇒ self

Remove sparkle from collection

Parameters:

Returns:

  • (self)


46
47
48
49
# File 'lib/sparkle_formation/sparkle_collection.rb', line 46

def remove_sparkle(sparkle)
  @sparkles.delete(sparkle)
  self
end

#set_root(sparkle) ⇒ self

Set the root sparkle which forces highest precedence

Parameters:

Returns:

  • (self)


21
22
23
24
# File 'lib/sparkle_formation/sparkle_collection.rb', line 21

def set_root(sparkle)
  @root = sparkle
  self
end

#sizeInteger

Returns:

  • (Integer)


57
58
59
# File 'lib/sparkle_formation/sparkle_collection.rb', line 57

def size
  sparkles.size
end

#sparkle_at(idx) ⇒ Sparkle, NilClass

Returns:



52
53
54
# File 'lib/sparkle_formation/sparkle_collection.rb', line 52

def sparkle_at(idx)
  sparkles.at(idx)
end

#templatesSmash

Returns:

  • (Smash)


100
101
102
103
104
105
106
107
108
# File 'lib/sparkle_formation/sparkle_collection.rb', line 100

def templates
  memoize("templates_#{checksum}") do
    Smash.new.tap do |hsh|
      sparkles.each do |sprkl|
        hsh.merge!(sprkl.templates)
      end
    end
  end
end