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.



344
345
346
347
348
349
350
# File 'lib/loom/pattern/dsl.rb', line 344

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

Instance Method Details

#after(&block) ⇒ Object



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

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

#before(&block) ⇒ Object



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

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

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



355
356
357
# File 'lib/loom/pattern/dsl.rb', line 355

def description(description)
  @next_description = description
end

#factsObject



432
433
434
# File 'lib/loom/pattern/dsl.rb', line 432

def facts
  @fact_map
end

#hooksObject



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

def hooks
  @hooks
end

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



366
367
368
369
# File 'lib/loom/pattern/dsl.rb', line 366

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



436
437
438
# File 'lib/loom/pattern/dsl.rb', line 436

def let_map
  @let_map
end

#pattern(name, &block) ⇒ Object



371
372
373
# File 'lib/loom/pattern/dsl.rb', line 371

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

#patternsObject

END DSL Implementation



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

def patterns
  @pattern_map.values
end

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

Parameters:

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

    default is :yaml



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/loom/pattern/dsl.rb', line 377

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



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

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



360
361
362
363
364
# File 'lib/loom/pattern/dsl.rb', line 360

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