Class: Hammer::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer/builder.rb

Overview

Context object for the block DSL - what runs inside a ‘Hammerfile`, a `Hammer.run do … end` block, or any other top-level fragment. All methods proxy to the target Hammer subclass.

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Builder

Returns a new instance of Builder.



6
7
8
# File 'lib/hammer/builder.rb', line 6

def initialize(klass)
  @klass = klass
end

Instance Method Details

#before(&block) ⇒ Object

Per-target / per-namespace pre-hook. Same semantics as ‘Hammer.before` at the class level - see lux-hammer.rb.



20
21
22
# File 'lib/hammer/builder.rb', line 20

def before(&block)
  @klass.before(&block)
end

#dotenv(flag = true) ⇒ Object

Opt out of auto ‘.env` loading in `Hammer.cli`. Default is on.



25
26
27
# File 'lib/hammer/builder.rb', line 25

def dotenv(flag = true)
  @klass.dotenv(flag)
end

#evaluate(source = nil, path = nil, &block) ⇒ Object

instance_eval wrapper that also publishes the current Hammer target via Thread.current. That lets ‘*_hammer.rb` files pulled in through Ruby’s own ‘require` (e.g. `Dir.require_all ’lib/tasks’‘ inside a Hammerfile) call `task`/`namespace`/`before` at top-level scope and have them register on the right target.



42
43
44
45
46
# File 'lib/hammer/builder.rb', line 42

def evaluate(source = nil, path = nil, &block)
  Hammer.with_target(@klass) do
    block ? instance_eval(&block) : instance_eval(source, path)
  end
end

#load(*paths, **kwargs) ⇒ Object

Same surface as ‘Hammer.load`. Resolved relative to the file that called us, so `load auto: true` inside a Hammerfile picks up *_hammer.rb under the Hammerfile’s directory.



32
33
34
35
# File 'lib/hammer/builder.rb', line 32

def load(*paths, **kwargs)
  anchor = Loader.caller_anchor(caller_locations(1, 1).first)
  @klass.loader.load(anchor, paths, kwargs)
end

#namespace(name, &block) ⇒ Object



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

def namespace(name, &block)
  @klass.namespace(name, &block)
end

#task(name, &block) ⇒ Object



10
11
12
# File 'lib/hammer/builder.rb', line 10

def task(name, &block)
  @klass.task(name, &block)
end