Class: Loom::Pattern::DSL::PatternBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/loom/pattern/dsl.rb

Instance Method Summary collapse

Constructor Details

#initializePatternBuilder

Returns a new instance of PatternBuilder.



336
337
338
339
340
341
342
# File 'lib/loom/pattern/dsl.rb', line 336

def initialize
  @pattern_map = {}
  @fact_map = {}
  @let_map = {}
  @hooks = []
  @next_description = nil
end

Instance Method Details

#after(&block) ⇒ Object



411
412
413
# File 'lib/loom/pattern/dsl.rb', line 411

def after(&block)
  hook :after, &block
end

#before(&block) ⇒ Object



407
408
409
# File 'lib/loom/pattern/dsl.rb', line 407

def before(&block)
  hook :before, &block
end

#description(description) ⇒ Object Also known as: desc



347
348
349
# File 'lib/loom/pattern/dsl.rb', line 347

def description(description)
  @next_description = description
end

#factsObject



424
425
426
# File 'lib/loom/pattern/dsl.rb', line 424

def facts
  @fact_map
end

#hooksObject



420
421
422
# File 'lib/loom/pattern/dsl.rb', line 420

def hooks
  @hooks
end

#let(name, default: nil, &block) ⇒ Object



358
359
360
361
# File 'lib/loom/pattern/dsl.rb', line 358

def let(name, default: nil, &block)
  raise "malformed let expression: missing block" unless block_given?
  @let_map[name.to_sym] = LetMapEntry.new default, &block
end

#let_mapObject



428
429
430
# File 'lib/loom/pattern/dsl.rb', line 428

def let_map
  @let_map
end

#pattern(name, &block) ⇒ Object



363
364
365
# File 'lib/loom/pattern/dsl.rb', line 363

def pattern(name, &block)
  define_pattern_internal(name, kind: :pattern, &block)
end

#patternsObject

END DSL Implementation



416
417
418
# File 'lib/loom/pattern/dsl.rb', line 416

def patterns
  @pattern_map.values
end

#report(name, format: :yaml, &block) ⇒ Object

Parameters:

  • format (:yaml|:json|:raw) (defaults to: :yaml)

    default is :yaml



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/loom/pattern/dsl.rb', line 369

def report(name, format: :yaml, &block)
  define_pattern_internal(name, kind: :report) do |loom, facts|
    # TODO: I don't like all of this logic here. It feels like it belongs in
    # a mod.
    result = if block_given?
               Loom.log.debug(self) { "report[#{name}] from block" }
               instance_exec(loom, facts, &block)
             elsif !Loom::Facts.is_empty?(facts[name])
               Loom.log.debug(self) { "report[#{name}] from facts[#{name}]" }
               facts[name]
             elsif respond_to?(name) && !self.send(name).nil?
               Loom.log.debug(self) { "report[#{name}] from let{#{name}}" }
               self.send name
             else
               err_msg = "no facts to report for fact[#{name}:#{name.class}]"
               raise PatternDefinitionError, err_msg
             end
    result = result.stdout if result.is_a? Loom::Shell::CmdResult

    puts case format
         when :yaml then result.to_yaml
         when :json then result.to_json
         when :raw then result
         else
           err_msg = "invalid report format: #{format.inspect}"
           err_msg << "valid options: yaml,json,raw"
           raise PatternDefinitionError, err_msg
         end
  end
end

#weave(name, pattern_slugs) ⇒ Object



400
401
402
403
404
405
# File 'lib/loom/pattern/dsl.rb', line 400

def weave(name, pattern_slugs)
  unless @next_description
    @next_description = "Weave runs patterns: %s" % pattern_slugs.join(", ")
  end
  define_pattern_internal(name, kind: :weave, expanded_slugs: pattern_slugs) { true }
end

#with_facts(**new_facts, &block) ⇒ Object



352
353
354
355
356
# File 'lib/loom/pattern/dsl.rb', line 352

def with_facts(**new_facts, &block)
  @fact_map.merge! new_facts
  yield_result = yield @fact_map if block_given?
  @fact_map = yield_result if yield_result.is_a? Hash
end