Module: NetLinx::ERB::Helpers

Defined in:
lib/netlinx/erb/helpers.rb

Overview

A collection of code generation helper methods for use in NetLinx ERB files.

Formatting collapse

Code Generation collapse

Other collapse

Instance Method Details

#auto_comment(str, condition) ⇒ String

Automatically comment out the input unless the condition is met.

Parameters:

  • str (String)

    NetLinx source code string

  • condition

    comments out the source code string if evaluates to false or nil

Returns:

  • (String)

    NetLinx source code



161
162
163
# File 'lib/netlinx/erb/helpers.rb', line 161

def auto_comment str, condition
  condition ? str : str.split("\n").map { |line| "// " + line }.join("\n")
end

#button_event(buttons, device = @dvTP) ⇒ String

Generate button events for the given hash of buttons.

Parameters:

  • buttons (Hash)

    button constants

  • device (String) (defaults to: @dvTP)

    touch panel device constant. @dvTP comes from ERB template.

Returns:

  • (String)

    [dev, chan] source code



170
171
172
# File 'lib/netlinx/erb/helpers.rb', line 170

def button_event buttons, device = @dvTP
  buttons.map { |name, _| "button_event[#{device}, #{name}]" }.join("\n")
end

#button_event_block(buttons, **kwargs) {|value, name| ... } ⇒ Object

Generate a button_event block for the given hash of buttons.

Examples:

button_event_block bluray_key_constants.remap(:key),
  function: 'bluray_key', momentary: true

button_event_block channel_strip_constants.select {|k,_| k.to_s.end_with? "_UP"},
  function: 'audio_increment_volume', remap: :audio, hold_block: true, momentary: true

# Use block to create function string.
# :function not set
button_event_block(video_source_constants) { |h|
  "video_patch(#{h[:input]}, #{h[:dest]})"
}

# Use block to create parameters string.
# :function is set
button_event_block(video_source_constants, function: 'video_patch') { |h|
  "#{h[:input]}, #{h[:dest]}"
}

# Use block to specify an array of parameters.
# :function is set
button_event_block(video_source_constants, function: 'video_patch') { |h|
  [h[:input], h[:dest]]
}

Parameters:

  • buttons (Hash)

    button constants

  • kwargs (Hash)

Options Hash (**kwargs):

  • :function (String)

    name of the function to call in the switch block

  • :remap (Symbol, nil)

    name of symbol to use to Hash#remap the hash. This is a convenience arg for readability; the hash can also be remapped before passed into this method.

  • :device (String)

    touch panel device constant. @dvTP comes from ERB template.

  • :padding (String, Numeric) — default: 3

    whitespace placed in front of the string. Can be a string of spaces, or a number representing the indentation level.

  • :momentary (Boolean) — default: false

    adds a ‘to` statement for momentary button feedback

  • :hold_block (Boolean) — default: false

    create a hold block for the event

  • :hold_time (Boolean) — default: 0.6

    repeat time for the hold block

Yields:

  • (value, name)

    option to create a custom function call string. Modifies the function arguments if :function is set, otherwise modifies the entire function call string.

Yield Parameters:

  • value

    value of the buttons hash for the given key. Accounts for remap.

  • name (Symbol)

    key of the buttons hash

Yield Returns:

  • (String)

    function string



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/netlinx/erb/helpers.rb', line 220

def button_event_block buttons, **kwargs, &block
  function   = kwargs.fetch :function, nil
  device     = kwargs.fetch :device, @dvTP
  remap      = kwargs.fetch :remap, nil
  padding    = kwargs.fetch :padding, 3
  momentary  = kwargs.fetch :momentary, false
  hold_block = kwargs.fetch :hold_block, false
  hold_time  = kwargs.fetch :hold_time, 0.6
  
  buttons = buttons.remap(remap) if remap
  
  case_string = justify group(buttons, padding: padding) { |name, value|
    str = if block_given?
      block_val = block.call value, name
      block_val = block_val.join ', ' if block_val.is_a? Array
    
      if function
        "#{function}(#{block_val})"
      else
        "#{block_val}"
      end
    else
      "#{function}(#{value})"
    end
    
    auto_comment "case #{name}: #{str};", value
  }
  
  momentary_string = momentary ? "\n        to[button.input];\n        " : ''
  
  output = <<EOS
#{button_event buttons, device}
{
    push:
    {#{momentary_string}
  switch (button.input.channel)
  {
#{          case_string }
  }
    }
    
EOS
  
  if hold_block
    hold_string = <<EOS
    hold[#{hold_time}, repeat]:
    {
  switch (button.input.channel)
  {
#{          case_string }
  }
    }
    
EOS
    output += hold_string
  end
  
  output +=
<<EOS
    release: {}
}
EOS
  output.chomp
end

#check_for_duplicate_values(*hashes) ⇒ Object

Ensures button number constants haven’t been unintentionally duplicated.

Parameters:

Raises:

  • if two keys have the same value.



323
324
325
# File 'lib/netlinx/erb/helpers.rb', line 323

def check_for_duplicate_values *hashes
  raise NotImplementedError
end

#generate_constant_ivars(h, append_suffix: false) ⇒ Object

Generate instance variables for the DEFINE_CONSTANTS section from the given hash keys. Appends @tmpl_suffix if set.



131
132
133
134
135
136
137
138
139
140
# File 'lib/netlinx/erb/helpers.rb', line 131

def generate_constant_ivars h, append_suffix: false
  h.each_key do |key|
    value = key.to_s.upcase
    value = "#{value}_#{@tmpl_suffix.to_s.upcase}" if @tmpl_suffix and append_suffix
    
    instance_variable_set :"@#{key.to_sym}", value
  end
  
  h
end

#generate_variable_ivars(h, append_suffix: true) ⇒ Object

Generate instance variables for the DEFINE_VARIABLES section from the given hash keys. Appends @tmpl_var_suffix if set.



146
147
148
149
150
151
152
153
154
155
# File 'lib/netlinx/erb/helpers.rb', line 146

def generate_variable_ivars h, append_suffix: true
  h.each_key do |key|
    value = key.to_s.downcase
    value = "#{value}_#{@tmpl_suffix.to_s.downcase}" if @tmpl_suffix and append_suffix
    
    instance_variable_set :"@#{key.to_sym}", value
  end
  
  h
end

#group(hash, padding: nil) {|key, value| ... } ⇒ String

Generate a group of lines for a given hash.

Parameters:

  • hash (Hash)
  • padding (String, Numeric) (defaults to: nil)

    whitespace placed in front of the string. Can be a string of spaces, or a number representing the indentation level.

Yields:

  • (key, value)

    elements from hash

Returns:

  • (String)

    lines of source code



21
22
23
24
# File 'lib/netlinx/erb/helpers.rb', line 21

def group hash, padding: nil, &block
  padding = ' ' * (4 * padding) if padding.is_a?(Numeric)
  hash.map { |key, value| block.call key, value }.compact.map { |str| padding.to_s + str }.join("\n")
end

#hiqnet_emptyObject

Generate an empty HiQnet address string.

Examples:

# Generates:
'0x000000000000'


290
291
292
# File 'lib/netlinx/erb/helpers.rb', line 290

def hiqnet_empty
  '0x' + ('0' * 12)
end

#justify(str, amount: nil, type: nil) ⇒ String

Left justify a block of code to line up on a type of character. Defaults to :equals (=).

Parameters:

  • amount (Numeric) (defaults to: nil)

    value of number of spaces, or the longest key in a hash.

  • type (:equals, :colon, :comma, :comma_2, :semicolon) (defaults to: nil)

    character to justify on.

Returns:

  • (String)

    a group of justified lines of source code.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/netlinx/erb/helpers.rb', line 32

def justify str, amount: nil, type: nil
  # justification_amount = amount.is_a?(Numeric) ? amount : amount.map { |key, _| key.to_s.size }.max
  
  lines = str.split "\n"
  
  justify_types = {
    equals:    lines.map { |line| line.index "=" }.compact.max,
    colon:     lines.map { |line| line.index ":" }.compact.max,
    comma:     lines.map { |line| line.index "," }.compact.max,
    comma_2:   lines.map { |line| line.index "," }.compact.max,
    semicolon: lines.map { |line| line.index ";" }.compact.max,
  }
  
  # Types that will be chosen from automatically if no type is specified
  auto_justify_types = [:equals, :colon]
  
  justify_by = justify_types.select { |k,v|
    auto_justify_types.include?(k) && !v.nil?
  }.min
  justify_by = justify_by.first if justify_by
  justify_by = type if type
  
  justify_amount = amount || justify_types[justify_by] || 0
  
  # Rebuild each line with the appropriate justification.
  lines.map! { |line|
    output = ''
    
    case justify_by
    when :equals
      line =~ /(.*?)(=.*)/
      output = $2.nil? ? line : $1.ljust(justify_amount) + $2
    when :colon
      line =~ /(.*?\:)\s*(.*)/
      output = $2.nil? ? line : $1.ljust(justify_amount + 1) + '    ' + $2
    when :comma
      line =~ /(.*?\,)\s*(.*)/
      output = $2.nil? ? line : $1.ljust(justify_amount + 1) + ' ' + $2
    when :comma_2
      line =~ /(.*?\,.*?\,)\s*(.*)/
      output = $2.nil? ? line : $1.ljust(justify_amount + 1) + ' ' + $2
    when :semicolon
      line =~ /(.*?\;)\s*(.*)/
      output = $2.nil? ? line : $1.ljust(justify_amount + 1) + '  ' + $2
    else
      line
    end
  }.join "\n"
end

Print the list of constants.

Parameters:

  • h (Hash)

    constant names as keys.

  • justify (Numeric) (defaults to: nil)

    column number to justify equals symbol (=) on.

Returns:

  • (String)

    a group of justified lines of source code.



100
101
102
103
104
# File 'lib/netlinx/erb/helpers.rb', line 100

def print_constant_hash h, justify: nil
  # TODO: Refactor to use #justify.
  max_len = h.map { |name, value| name.size }.max
  h.map { |name, value| "#{name.to_s.upcase.ljust justify || max_len} = #{value};" }.join("\n")
end

Print the list of devices.

Parameters:

  • h (Hash)

    device names as keys.

  • justify (Numeric) (defaults to: nil)

    column number to justify equals symbol (=) on.

Returns:

  • (String)

    a group of justified lines of source code.



90
91
92
93
94
# File 'lib/netlinx/erb/helpers.rb', line 90

def print_device_hash h, justify: nil
  # TODO: Refactor to use #justify.
  max_len = h.map { |name, value| name.size }.max
  h.map { |name, value| "dv#{name.to_s.upcase.ljust justify || max_len} = #{value};" }.join("\n")
end

Print the list of variables. Format:

{
  var_name: { type: :integer, default: 0, comment: 'my var' }
}

Parameters:

  • h (Hash)

    variable names as keys.

Returns:

  • (String)

    a group of justified lines of source code.



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/netlinx/erb/helpers.rb', line 113

def print_variable_hash h
  justify group(h) { |name, params|
    type     = params.fetch :type,     :integer
    default  = params.fetch :default,  nil
    comment = params.fetch :comment, nil
    
    output = "#{type} #{name.to_s.downcase}"
    output += " = #{default}" if default
    output += ";"
    output += "  // #{comment}" if comment
    output
  }
end

#show_control(control, show, device: @dvTP) ⇒ Object

Send a touch panel command to show or hide a control.

Examples:

^SHO-<control>,<0|1>

Parameters:

  • control (Symbol, String)

    address or constant of a control

  • show (Boolean, Numeric, String)

    0/1 print verbatim. true/fase convert to 0/1. string expression is wrapped in itoa().



302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/netlinx/erb/helpers.rb', line 302

def show_control control, show, device: @dvTP
  str = "send_command #{device}, \"'^SHO-', itoa(#{control}), ',"
  
  if show.is_a? Fixnum
    str += "#{show}'"
  elsif show == true or show == false or show == nil
    str += "#{show ? 1 : 0}'"
  else
    str += "', itoa(#{show})"
  end
  
  str += "\";"
end