Class: SparkleFormation

Inherits:
Object
  • Object
show all
Includes:
Utils::AnimalStrings
Defined in:
lib/sparkle_formation/utils.rb,
lib/sparkle_formation/version.rb,
lib/sparkle_formation/sparkle_formation.rb

Defined Under Namespace

Modules: Utils Classes: Version

Constant Summary collapse

VERSION =
Version.new('0.1.0')

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::AnimalStrings

#camel, #snake

Constructor Details

#initialize(name, options = {}, &block) ⇒ SparkleFormation



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/sparkle_formation/sparkle_formation.rb', line 85

def initialize(name, options={}, &block)
  @name = name
  @sparkle_path = options[:sparkle_path] ||
    self.class.custom_paths[:sparkle_path] ||
    File.join(Dir.pwd, 'cloudformation/components')
  @dynamics_directory = options[:dynamics_directory] ||
    self.class.custom_paths[:dynamics_directory] ||
    File.join(File.dirname(@sparkle_path), 'dynamics')
  self.class.load_dynamics!(@dynamics_directory)
  @components = AttributeStruct.hashish.new
  @load_order = []
  if(block)
    load_block(block)
  end
end

Class Attribute Details

.components_pathObject

Returns the value of attribute components_path.



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

def components_path
  @components_path
end

.dynamicsObject (readonly)

Returns the value of attribute dynamics.



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

def dynamics
  @dynamics
end

.dynamics_pathObject

Returns the value of attribute dynamics_path.



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

def dynamics_path
  @dynamics_path
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



82
83
84
# File 'lib/sparkle_formation/sparkle_formation.rb', line 82

def components
  @components
end

#load_orderObject (readonly)

Returns the value of attribute load_order.



83
84
85
# File 'lib/sparkle_formation/sparkle_formation.rb', line 83

def load_order
  @load_order
end

#nameObject (readonly)

Returns the value of attribute name.



80
81
82
# File 'lib/sparkle_formation/sparkle_formation.rb', line 80

def name
  @name
end

#sparkle_pathObject (readonly)

Returns the value of attribute sparkle_path.



81
82
83
# File 'lib/sparkle_formation/sparkle_formation.rb', line 81

def sparkle_path
  @sparkle_path
end

Class Method Details

.build(&block) ⇒ Object



35
36
37
38
39
# File 'lib/sparkle_formation/sparkle_formation.rb', line 35

def build(&block)
  struct = AttributeStruct.new
  struct.instance_exec(&block)
  struct
end

.compile(path) ⇒ Object



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

def compile(path)
  formation = self.instance_eval(IO.read(path), path, 1)
  formation.compile._dump
end

.custom_pathsObject



17
18
19
20
# File 'lib/sparkle_formation/sparkle_formation.rb', line 17

def custom_paths
  @_paths ||= {}
  @_paths
end

.dynamic(name, &block) ⇒ Object



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

def dynamic(name, &block)
  @dynamics ||= Mash.new
  @dynamics[name] = block
end

.from_hash(hash) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/sparkle_formation/sparkle_formation.rb', line 71

def from_hash(hash)
  struct = AttributeStruct.new
  struct._camel_keys_set(:auto_discovery)
  struct._load(hash)
  struct._camel_keys_set(nil)
  struct
end

.insert(dynamic_name, struct, *args) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/sparkle_formation/sparkle_formation.rb', line 62

def insert(dynamic_name, struct, *args)
  if(@dynamics && @dynamics[dynamic_name])
    struct.instance_exec(*args, &@dynamics[dynamic_name])
    struct
  else
    raise "Failed to locate requested dynamic block for insertion: #{dynamic_name} (valid: #{@dynamics.keys.sort.join(', ')})"
  end
end

.load_component(path) ⇒ Object



41
42
43
# File 'lib/sparkle_formation/sparkle_formation.rb', line 41

def load_component(path)
  self.instance_eval(IO.read(path), path, 1)
end

.load_dynamics!(directory) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sparkle_formation/sparkle_formation.rb', line 45

def load_dynamics!(directory)
  @loaded_dynamics ||= []
  Dir.glob(File.join(directory, '*.rb')).each do |dyn|
    dyn = File.expand_path(dyn)
    next if @loaded_dynamics.include?(dyn)
    self.instance_eval(IO.read(dyn), dyn, 1)
    @loaded_dynamics << dyn
  end
  @loaded_dynamics.uniq!
  true
end

Instance Method Details

#compileObject

Returns compiled Mash instance



126
127
128
129
130
131
132
133
134
135
# File 'lib/sparkle_formation/sparkle_formation.rb', line 126

def compile
  compiled = AttributeStruct.new
  @load_order.each do |key|
    compiled._merge!(components[key])
  end
  if(@overrides)
    compiled._merge!(@overrides)
  end
  compiled
end

#load(*args) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sparkle_formation/sparkle_formation.rb', line 106

def load(*args)
  args.each do |thing|
    if(thing.is_a?(Symbol))
      path = File.join(sparkle_path, "#{thing}.rb")
    else
      path = thing
    end
    key = File.basename(path).sub('.rb', '')
    components[key] = self.class.load_component(path)
    @load_order << key
  end
  self
end

#load_block(block) ⇒ Object



101
102
103
104
# File 'lib/sparkle_formation/sparkle_formation.rb', line 101

def load_block(block)
  @components[:__base__] = self.class.build(&block)
  @load_order << :__base__
end

#overrides(&block) ⇒ Object



120
121
122
123
# File 'lib/sparkle_formation/sparkle_formation.rb', line 120

def overrides(&block)
  @overrides = self.class.build(&block)
  self
end