Module: Assembler

Defined in:
lib/assembler.rb,
lib/assembler/builder.rb,
lib/assembler/version.rb,
lib/assembler/parameter.rb,
lib/assembler/initializer.rb

Defined Under Namespace

Modules: Initializer Classes: Builder, Parameter

Constant Summary collapse

VERSION =
"1.2.0"

Instance Method Summary collapse

Instance Method Details

#after_assembly(&block) ⇒ Object



45
46
47
48
49
# File 'lib/assembler.rb', line 45

def after_assembly(&block)
  assembly_setup do
    after_assembly_blocks << block
  end
end

#after_assembly_blocksObject



51
52
53
# File 'lib/assembler.rb', line 51

def after_assembly_blocks
  @after_assembly_blocks ||= []
end

#assemble_from(*args) ⇒ Object Also known as: assemble_with



18
19
20
21
22
23
24
25
26
# File 'lib/assembler.rb', line 18

def assemble_from(*args)
  assembly_setup do
    optional = args.last.is_a?(Hash) ? args.pop : {}
    required = args

    optional.each { |k,v| assemble_from_options(k, default: v) }
    required.each { |k| assemble_from_options(k) }
  end
end

#assemble_from_options(*args) ⇒ Object Also known as: assemble_with_options



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/assembler.rb', line 5

def assemble_from_options(*args)
  assembly_setup do
    options = args.last.is_a?(Hash) ? args.pop : {}
    param_names = args

    param_names.each do |param_name|
      param = Parameter.new(param_name, options)
      assembly_parameters_hash[param.name] = param
    end
  end
end

#assembler_initializer(*args) ⇒ Object



29
30
31
32
33
# File 'lib/assembler.rb', line 29

def assembler_initializer(*args)
  caller_file, caller_line, _ = caller.first.split(':')
  warn "The `assembler_initializer` method is deprecated and will be phased out in version 2.0. Please use `assemble_from` instead. Called from #{caller_file}:#{caller_line}."
  assemble_from(*args)
end

#assembly_parametersObject



55
56
57
# File 'lib/assembler.rb', line 55

def assembly_parameters
  assembly_parameters_hash.values
end

#assembly_parameters_hashObject



59
60
61
# File 'lib/assembler.rb', line 59

def assembly_parameters_hash
  @assembly_parameters_hash ||= {}
end

#assembly_setupObject



63
64
65
66
67
68
69
# File 'lib/assembler.rb', line 63

def assembly_setup
  yield
ensure
  include Assembler::Initializer
  attr_reader *assembly_parameters.map(&:name)
  private *assembly_parameters.map(&:name)
end

#before_assembly(&block) ⇒ Object



35
36
37
38
39
# File 'lib/assembler.rb', line 35

def before_assembly(&block)
  assembly_setup do
    before_assembly_blocks << block
  end
end

#before_assembly_blocksObject



41
42
43
# File 'lib/assembler.rb', line 41

def before_assembly_blocks
  @before_assembly_blocks ||= []
end