Class: SparkleFormation
- Inherits:
-
Object
- Object
- SparkleFormation
- Extended by:
- Utils::AnimalStrings
- Includes:
- Utils::AnimalStrings
- Defined in:
- lib/sparkle_formation/utils.rb,
lib/sparkle_formation.rb,
lib/sparkle_formation/aws.rb,
lib/sparkle_formation/version.rb,
lib/sparkle_formation/translation.rb,
lib/sparkle_formation/sparkle_struct.rb,
lib/sparkle_formation/translation/heat.rb,
lib/sparkle_formation/sparkle_attribute.rb,
lib/sparkle_formation/sparkle_formation.rb,
lib/sparkle_formation/translation/rackspace.rb
Overview
Formation container
Defined Under Namespace
Modules: SparkleAttribute, Utils Classes: Aws, Cache, Registry, SparkleStruct, Translation, Version
Constant Summary collapse
- VERSION =
Version.new('0.2.0')
Instance Attribute Summary collapse
-
#components ⇒ Array
readonly
Components to load.
-
#components_directory ⇒ String
readonly
Components path.
-
#dynamics_directory ⇒ String
readonly
Dynamics path.
-
#load_order ⇒ Array
readonly
Order of loading.
-
#name ⇒ Symbol
readonly
Name of formation.
-
#registry_directory ⇒ String
readonly
Registry path.
-
#sparkle_path ⇒ String
readonly
Base path.
Class Method Summary collapse
-
.build(base = nil) { ... } ⇒ SparkleStruct
Execute given block within struct context.
-
.builtin_insert(dynamic_name, struct, *args, &block) ⇒ SparkleStruct
Insert a builtin dynamic into a context.
-
.compile(path, *args) ⇒ Hashish, SparkleStruct
Compile file.
-
.components_path=(path = nil) ⇒ String
(also: components_path)
Get/set path to component files.
-
.custom_paths ⇒ Hashish
Custom paths.
-
.dynamic(name, args = {}) { ... } ⇒ TrueClass
Define and register new dynamic.
-
.dynamic_info(name) ⇒ Hashish
(also: dynamic_information)
Metadata for dynamic.
-
.dynamics ⇒ Hashish
Loaded dynamics.
-
.dynamics_path=(path = nil) ⇒ String
(also: dynamics_path)
Get/set path to dynamic files.
-
.from_hash(hash) ⇒ SparkleStruct
Convert hash to SparkleStruct instance.
-
.insert(dynamic_name, struct, *args, &block) ⇒ SparkleStruct
Insert a dynamic into a context.
-
.load_component(path) ⇒ SparkleStruct
Load component.
-
.load_dynamics!(directory) ⇒ TrueClass
Load all dynamics within a directory.
-
.load_registry!(directory) ⇒ TrueClass
Load all registry entries within a directory.
-
.registry_path=(path = nil) ⇒ String
(also: registry_path)
Get/set path to registry files.
-
.sparkle_path=(path = nil) ⇒ String
(also: sparkle_path)
Get/set path to sparkle directory.
Instance Method Summary collapse
-
#block(block) ⇒ TrueClass
(also: #load_block)
Add block to load order.
-
#compile ⇒ SparkleStruct
Compile the formation.
-
#dump ⇒ Hash
Dumped hash.
-
#initialize(name, options = {}) { ... } ⇒ SparkleFormation
constructor
Create new instance.
-
#load(*args) ⇒ self
Load components into instance.
-
#overrides(args = {}) { ... } ⇒ Object
Registers block into overrides.
-
#to_json ⇒ String
Dumped hash JSON.
Methods included from Utils::AnimalStrings
Constructor Details
#initialize(name, options = {}) { ... } ⇒ SparkleFormation
Create new instance
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 277 def initialize(name, ={}, &block) @name = name.to_sym @sparkle_path = [:sparkle_path] || self.class.custom_paths[:sparkle_path] || File.join(Dir.pwd, 'cloudformation') @components_directory = [:components_directory] || self.class.custom_paths[:components_directory] || File.join(sparkle_path, 'components') @dynamics_directory = [:dynamics_directory] || self.class.custom_paths[:dynamics_directory] || File.join(sparkle_path, 'dynamics') @registry_directory = [:registry_directory] || self.class.custom_paths[:registry_directory] || File.join(sparkle_path, 'registry') self.class.load_dynamics!(@dynamics_directory) self.class.load_registry!(@registry_directory) unless([:disable_aws_builtins]) require 'sparkle_formation/aws' SfnAws.load! end @components = SparkleStruct.hashish.new @load_order = [] @overrides = [] if(block) load_block(block) end end |
Instance Attribute Details
#components ⇒ Array (readonly)
263 264 265 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 263 def components @components end |
#components_directory ⇒ String (readonly)
257 258 259 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 257 def components_directory @components_directory end |
#dynamics_directory ⇒ String (readonly)
259 260 261 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 259 def dynamics_directory @dynamics_directory end |
#load_order ⇒ Array (readonly)
265 266 267 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 265 def load_order @load_order end |
#name ⇒ Symbol (readonly)
253 254 255 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 253 def name @name end |
#registry_directory ⇒ String (readonly)
261 262 263 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 261 def registry_directory @registry_directory end |
#sparkle_path ⇒ String (readonly)
255 256 257 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 255 def sparkle_path @sparkle_path end |
Class Method Details
.build(base = nil) { ... } ⇒ SparkleStruct
Execute given block within struct context
110 111 112 113 114 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 110 def build(base=nil, &block) struct = base || SparkleStruct.new struct.instance_exec(&block) @_struct = struct end |
.builtin_insert(dynamic_name, struct, *args, &block) ⇒ SparkleStruct
Insert a builtin dynamic into a context
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 213 def builtin_insert(dynamic_name, struct, *args, &block) if(defined?(SfnAws) && lookup_key = SfnAws.registry_key(dynamic_name)) _name, _config = *args _config ||= {} return unless _name new_resource = struct.resources.__send__("#{_name}_#{dynamic_name}".to_sym) new_resource.type lookup_key properties = new_resource.properties SfnAws.resource(dynamic_name, :properties).each do |prop_name| value = [prop_name, snake(prop_name)].map do |key| _config[key] || _config[key.to_sym] end.compact.first if(value) if(value.is_a?(Proc)) properties.__send__(prop_name).instance_exec(&value) else properties.__send__(prop_name, value) end end end new_resource.instance_exec(&block) if block new_resource end end |
.compile(path, *args) ⇒ Hashish, SparkleStruct
Compile file
100 101 102 103 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 100 def compile(path, *args) formation = self.instance_eval(IO.read(path), path, 1) args.include?(:sparkle) ? formation : formation.compile._dump end |
.components_path=(path = nil) ⇒ String Also known as: components_path
Get/set path to component files
63 64 65 66 67 68 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 63 def components_path=(path=nil) if(path) custom_paths[:components_directory] = path end custom_paths[:components_directory] end |
.custom_paths ⇒ Hashish
39 40 41 42 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 39 def custom_paths @_paths ||= SparkleStruct.hashish.new @_paths end |
.dynamic(name, args = {}) { ... } ⇒ TrueClass
Define and register new dynamic
163 164 165 166 167 168 169 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 163 def dynamic(name, args={}, &block) @dynamics ||= SparkleStruct.hashish.new dynamics[name] = SparkleStruct.hashish[ :block, block, :args, SparkleStruct.hashish[args.map(&:to_a)] ] true end |
.dynamic_info(name) ⇒ Hashish Also known as: dynamic_information
Metadata for dynamic
175 176 177 178 179 180 181 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 175 def dynamic_info(name) if(dynamics[name]) dynamics[name][:args] ||= SparkleStruct.hashish.new else raise KeyError.new("No dynamic registered with provided name (#{name})") end end |
.dynamics ⇒ Hashish
34 35 36 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 34 def dynamics @dynamics ||= SparkleStruct.hashish.new end |
.dynamics_path=(path = nil) ⇒ String Also known as: dynamics_path
Get/set path to dynamic files
75 76 77 78 79 80 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 75 def dynamics_path=(path=nil) if(path) custom_paths[:dynamics_directory] = path end custom_paths[:dynamics_directory] end |
.from_hash(hash) ⇒ SparkleStruct
will do best effort on camel key auto discovery
Convert hash to SparkleStruct instance
243 244 245 246 247 248 249 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 243 def from_hash(hash) struct = SparkleStruct.new struct._camel_keys_set(:auto_discovery) struct._load(hash) struct._camel_keys_set(nil) struct end |
.insert(dynamic_name, struct, *args, &block) ⇒ SparkleStruct
Insert a dynamic into a context
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 190 def insert(dynamic_name, struct, *args, &block) result = false if(@dynamics && @dynamics[dynamic_name]) result = struct.instance_exec(*args, &@dynamics[dynamic_name][:block]) if(block_given?) result.instance_exec(&block) end result = struct else result = builtin_insert(dynamic_name, struct, *args, &block) end unless(result) raise "Failed to locate requested dynamic block for insertion: #{dynamic_name} (valid: #{(@dynamics || {}).keys.sort.join(', ')})" end result end |
.load_component(path) ⇒ SparkleStruct
Load component
120 121 122 123 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 120 def load_component(path) self.instance_eval(IO.read(path), path, 1) @_struct end |
.load_dynamics!(directory) ⇒ TrueClass
Load all dynamics within a directory
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 129 def load_dynamics!(directory) @loaded_dynamics ||= [] Dir.glob(File.join(directory, '*.rb')).each do |dyn| dyn = File.(dyn) next if @loaded_dynamics.include?(dyn) self.instance_eval(IO.read(dyn), dyn, 1) @loaded_dynamics << dyn end @loaded_dynamics.uniq! true end |
.load_registry!(directory) ⇒ TrueClass
Load all registry entries within a directory
145 146 147 148 149 150 151 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 145 def load_registry!(directory) Dir.glob(File.join(directory, '*.rb')).each do |reg| reg = File.(reg) require reg end true end |
.registry_path=(path = nil) ⇒ String Also known as: registry_path
Get/set path to registry files
87 88 89 90 91 92 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 87 def registry_path=(path=nil) if(path) custom_paths[:registry_directory] = path end custom_paths[:registry_directory] end |
.sparkle_path=(path = nil) ⇒ String Also known as: sparkle_path
Get/set path to sparkle directory
48 49 50 51 52 53 54 55 56 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 48 def sparkle_path=(path=nil) if(path) custom_paths[:sparkle_path] = path custom_paths[:components_directory] ||= File.join(path, 'components') custom_paths[:dynamics_directory] ||= File.join(path, 'dynamics') custom_paths[:registry_directory] ||= File.join(path, 'registry') end custom_paths[:sparkle_path] end |
Instance Method Details
#block(block) ⇒ TrueClass Also known as: load_block
Add block to load order
309 310 311 312 313 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 309 def block(block) @components[:__base__] = self.class.build(&block) @load_order << :__base__ true end |
#compile ⇒ SparkleStruct
Compile the formation
346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 346 def compile compiled = SparkleStruct.new @load_order.each do |key| compiled._merge!(components[key]) end @overrides.each do |override| if(override[:args] && !override[:args].empty?) compiled._set_state(override[:args]) end self.class.build(compiled, &override[:block]) end compiled end |
#dump ⇒ Hash
361 362 363 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 361 def dump MultiJson.load(self.to_json) end |
#load(*args) ⇒ self
Load components into instance
320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 320 def load(*args) args.each do |thing| if(thing.is_a?(Symbol)) path = File.join(components_directory, "#{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 |
#overrides(args = {}) { ... } ⇒ Object
Registers block into overrides
338 339 340 341 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 338 def overrides(args={}, &block) @overrides << {:args => args, :block => block} self end |
#to_json ⇒ String
366 367 368 |
# File 'lib/sparkle_formation/sparkle_formation.rb', line 366 def to_json MultiJson.dump(compile.dump!) end |