Class: SparkleFormation::Sparkle
- Inherits:
-
Object
- Object
- SparkleFormation::Sparkle
- Includes:
- Bogo::Memoization
- Defined in:
- lib/sparkle_formation/sparkle.rb
Direct Known Subclasses
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
-
#raw_data ⇒ Smash
readonly
Raw part data.
-
#root ⇒ String
readonly
Path to sparkle directories.
Class Method Summary collapse
-
.path(name) ⇒ String
Return the path to the SparkePack registered with the given name.
-
.register!(name = nil, path = nil) ⇒ Array<String:name, String:path>
Register a SparklePack for short name access.
Instance Method Summary collapse
- #components ⇒ Smash<name:block>
- #dynamics ⇒ Smash<name:block>
-
#eval_wrapper ⇒ Object
Wrapper for evaluating sfn files to store within sparkle container and remove global application.
-
#get(type, name) ⇒ Smash
Request item from the store.
-
#initialize(args = {}) ⇒ self
constructor
Create new sparkle instance.
- #inspect ⇒ String
- #registries ⇒ Smash<name:block>
- #templates ⇒ Smash<name:path>
Constructor Details
#initialize(args = {}) ⇒ self
Create new sparkle instance
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/sparkle_formation/sparkle.rb', line 204 def initialize(args={}) if(args[:name]) @root = self.class.path(args[:name]) else @root = args.fetch(:root, locate_root) end unless(File.directory?(@root)) raise Errno::ENOENT.new("No such directory - #{@root}") end @raw_data = Smash.new( :dynamic => [], :component => [], :registry => [] ) @wrapper = eval_wrapper.new wrapper.part_data(raw_data) load_parts! end |
Instance Attribute Details
#raw_data ⇒ Smash (readonly)
Returns raw part data.
196 197 198 |
# File 'lib/sparkle_formation/sparkle.rb', line 196 def raw_data @raw_data end |
#root ⇒ String (readonly)
Returns path to sparkle directories.
194 195 196 |
# File 'lib/sparkle_formation/sparkle.rb', line 194 def root @root end |
Class Method Details
.path(name) ⇒ String
Return the path to the SparkePack registered with the given name
52 53 54 55 56 57 58 |
# File 'lib/sparkle_formation/sparkle.rb', line 52 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
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 |
# 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 if(idx) file = caller[idx.next].split(':', 2).first path = File.join(File.dirname(file), 'sparkleformation') unless(File.directory?(path)) path = nil end unless(name) name = File.basename(caller[idx.next].split(':', 2).first) name.sub!(File.extname(name), '') end 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
#components ⇒ Smash<name:block>
224 225 226 227 228 |
# File 'lib/sparkle_formation/sparkle.rb', line 224 def components memoize(:components) do Smash.new end end |
#dynamics ⇒ Smash<name:block>
231 232 233 234 235 |
# File 'lib/sparkle_formation/sparkle.rb', line 231 def dynamics memoize(:dynamics) do Smash.new end end |
#eval_wrapper ⇒ Object
Wrapper for evaluating sfn files to store within sparkle container and remove global application
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 |
# File 'lib/sparkle_formation/sparkle.rb', line 64 def eval_wrapper klass = Class.new(BasicObject) klass.class_eval(" def require(*args)\n ::Kernel.require *args\n end\n\n class SparkleFormation\n\n attr_accessor :sparkle_path\n\n class << self\n\n def insert(*args, &block)\n ::SparkleFormation.insert(*args, &block)\n end\n\n def part_data(data=nil)\n if(data)\n @data = data\n else\n @data\n end\n end\n\n def dynamic(name, args={}, &block)\n part_data[:dynamic].push(\n ::Smash.new(\n :name => name,\n :block => block,\n :args => Smash[\n args.map(&:to_a)\n ],\n :type => :dynamic\n )\n ).last\n end\n\n def build(&block)\n part_data[:component].push(\n ::Smash.new(\n :block => block,\n :type => :component\n )\n ).last\n end\n\n def component(name, &block)\n part_data[:component].push(\n ::Smash.new(\n :name => name,\n :block => block,\n :type => :component\n )\n ).last\n end\n\n def dynamic_info(*args)\n Smash.new(:metadata => {}, :args => {})\n end\n\n end\n\n def initialize(*args)\n SparkleFormation.part_data[:template].push(\n ::Smash.new(\n :name => args.first\n )\n )\n raise TypeError\n end\n\n class Registry\n\n def self.register(name, &block)\n SparkleFormation.part_data[:registry].push(\n ::Smash.new(\n :name => name,\n :block => block,\n :type => :registry\n )\n ).last\n end\n\n end\n SfnRegistry = Registry\n\n end\n ::Object.constants.each do |const|\n unless(self.const_defined?(const))\n next if const == :Config # prevent warning output\n self.const_set(const, ::Object.const_get(const))\n end\n end\n\n def part_data(arg)\n SparkleFormation.part_data(arg)\n end\n EOS\n )\n klass\nend\n" |
#get(type, name) ⇒ Smash
Request item from the store
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/sparkle_formation/sparkle.rb', line 282 def get(type, name) unless(TYPES.keys.include?(type.to_s)) raise NameError.new "Invalid type requested (#{type})! Valid types: #{TYPES.join(', ')}" end result = send(TYPES[type])[name] if(result.nil? && TYPES[type] == 'templates') result = ( send(TYPES[type]).detect{|k,v| name = name.to_s short_name = v[:path].sub(/#{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 |
#inspect ⇒ String
307 308 309 |
# File 'lib/sparkle_formation/sparkle.rb', line 307 def inspect "<SparkleFormation::Sparkle [root: #{root.inspect}]>" end |
#registries ⇒ Smash<name:block>
238 239 240 241 242 |
# File 'lib/sparkle_formation/sparkle.rb', line 238 def registries memoize(:registries) do Smash.new end end |
#templates ⇒ Smash<name:path>
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/sparkle_formation/sparkle.rb', line 245 def templates memoize(:templates) do Smash.new.tap do |hash| Dir.glob(File.join(root, '**', '**', '*.{json,rb}')) do |path| slim_path = path.sub("#{root}/", '') next if DIRS.include?(slim_path.split('/').first) data = Smash.new(:template => []) t_wrap = eval_wrapper.new t_wrap.part_data(data) if(slim_path.end_with?('.rb')) begin t_wrap.instance_eval(IO.read(path), path, 1) rescue TypeError end end data = data[:template].first || Smash.new unless(data[:name]) data[:name] = slim_path.tr('/', '__').sub(/\.(rb|json)$/, '') end hash[data[:name]] = data.merge( Smash.new( :type => :template, :path => path, :serialized => !path.end_with?('.rb') ) ) end end end end |