Module: Formatron::Util::DSL

Overview

utilities for generating DSL classes

Instance Method Summary collapse

Instance Method Details

#dsl_array(symbol) ⇒ Object

rubocop:enable Metrics/MethodLength



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/formatron/util/dsl.rb', line 74

def dsl_array(symbol)
  iv = "@#{symbol}"
  define_method symbol do |value = nil|
    array = instance_variable_get(iv)
    if array.nil?
      array = []
      instance_variable_set iv, array
    end
    array.push value unless value.nil?
    array
  end
end

#dsl_block(symbol, cls, &params_block) ⇒ Object

rubocop:disable Metrics/MethodLength



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/formatron/util/dsl.rb', line 30

def dsl_block(symbol, cls, &params_block)
  iv = "@#{symbol}"
  define_method symbol do |&block|
    value = instance_variable_get(iv)
    if value.nil?
      params = {}
      params = instance_eval(
        &params_block
      ) unless params_block.nil?
      value = self.class.const_get(cls).new(**params)
      instance_variable_set iv, value
    end
    block.call value unless block.nil?
    instance_variable_get iv
  end
end

#dsl_block_array(symbol, cls) ⇒ Object

rubocop:disable Metrics/MethodLength



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/formatron/util/dsl.rb', line 88

def dsl_block_array(symbol, cls)
  iv = "@#{symbol}"
  define_method symbol do |&block|
    array = instance_variable_get(iv)
    if array.nil?
      array = []
      instance_variable_set iv, array
    end
    unless block.nil?
      value = self.class.const_get(cls).new
      array.push value
      block.call value
    end
    array
  end
end

#dsl_hash(symbol, cls, &params_block) ⇒ Object

rubocop:disable Metrics/MethodLength



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/formatron/util/dsl.rb', line 49

def dsl_hash(symbol, cls, &params_block)
  iv = "@#{symbol}"
  define_method symbol do |dsl_key = nil, &block|
    hash = instance_variable_get(iv)
    if hash.nil?
      hash = {}
      instance_variable_set iv, hash
    end
    unless dsl_key.nil?
      params = {}
      params = instance_exec(
        dsl_key, &params_block
      ) unless params_block.nil?
      value = self.class.const_get(cls).new(
        dsl_key,
        **params
      )
      hash[dsl_key] = value
      block.call value unless block.nil?
    end
    hash
  end
end

#dsl_initialize_block(&block) ⇒ Object



5
6
7
8
9
# File 'lib/formatron/util/dsl.rb', line 5

def dsl_initialize_block(&block)
  define_method :initialize do |**params|
    instance_exec(**params, &block) unless block.nil?
  end
end

#dsl_initialize_hash(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/formatron/util/dsl.rb', line 11

def dsl_initialize_hash(&block)
  define_method :initialize do |dsl_key, **params|
    instance_exec(
      dsl_key,
      **params,
      &block
    ) unless block.nil?
  end
end

#dsl_property(symbol) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/formatron/util/dsl.rb', line 21

def dsl_property(symbol)
  iv = "@#{symbol}"
  define_method symbol do |value = nil|
    instance_variable_set iv, value unless value.nil?
    instance_variable_get iv
  end
end