Module: Helpers
- Defined in:
- lib/netlinx/erb/helpers.rb
Overview
A collection of helper methods for use in NetLinx ERB files.
Formatting (collapse)
-
- (String) group(hash, padding: nil) {|key, value| ... }
Generate a group of lines for a given hash.
-
- (String) justify(str, amount: nil, type: nil)
Left justify a block of code to line up on a type of character.
Code Generation (collapse)
-
- (String) auto_comment(str, condition)
Automatically comment out the input unless the condition is met.
-
- (String) button_event(buttons, device = @dvTP)
Generate button events for the given hash of buttons.
-
- (Object) button_event_block(buttons, **kwargs) {|value, name| ... }
Generate a button_event block for the given hash of buttons.
-
- (String) execute_erb(template_file)
Run ERB on the given template file.
-
- (Object) generate_constant_ivars(h, append_suffix: false)
Generate instance variables for the DEFINE_CONSTANTS section from the given hash keys.
-
- (Object) generate_variable_ivars(h, append_suffix: true)
Generate instance variables for the DEFINE_VARIABLES section from the given hash keys.
-
- (Object) hiqnet_empty
Generate an empty HiQnet address string.
-
- (String) print_constant_hash(h, justify: nil)
Print the list of constants.
-
- (String) print_device_hash(h, justify: nil)
Print the list of devices.
-
- (String) print_variable_hash(h)
Print the list of variables.
-
- (Object) show_control(control, show, device: @dvTP)
Send a touch panel command to show or hide a control.
Other (collapse)
-
- (Object) check_for_duplicate_values(*hashes)
Ensures button number constants haven't been unintentionally duplicated.
Instance Method Details
- (String) auto_comment(str, condition)
Automatically comment out the input unless the condition is met.
160 161 162 |
# File 'lib/netlinx/erb/helpers.rb', line 160 def auto_comment str, condition condition ? str : str.split("\n").map { |line| "// " + line }.join("\n") end |
- (String) button_event(buttons, device = @dvTP)
Generate button events for the given hash of buttons.
169 170 171 |
# File 'lib/netlinx/erb/helpers.rb', line 169 def , device = @dvTP .map { |name, _| "button_event[#{device}, #{name}]" }.join("\n") end |
- (Object) button_event_block(buttons, **kwargs) {|value, name| ... }
Generate a button_event block for the given hash of buttons.
219 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 |
# File 'lib/netlinx/erb/helpers.rb', line 219 def , **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 = .remap(remap) if remap case_string = justify group(, 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 #{ , 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 |
- (Object) check_for_duplicate_values(*hashes)
Ensures button number constants haven't been unintentionally duplicated.
331 332 333 |
# File 'lib/netlinx/erb/helpers.rb', line 331 def check_for_duplicate_values *hashes raise NotImplementedError end |
- (String) execute_erb(template_file)
Run ERB on the given template file.
319 320 321 322 |
# File 'lib/netlinx/erb/helpers.rb', line 319 def execute_erb template_file raise LoadError, "Template not found: #{template_file}" unless File.exists? template_file $AUTOGEN_HEADER + ERB.new(File.read(template_file), nil, '%<>-').result() end |
- (Object) generate_constant_ivars(h, append_suffix: false)
Generate instance variables for the DEFINE_CONSTANTS section from the given hash keys. Appends @tmpl_suffix if set.
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/netlinx/erb/helpers.rb', line 130 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 |
- (Object) generate_variable_ivars(h, append_suffix: true)
Generate instance variables for the DEFINE_VARIABLES section from the given hash keys. Appends @tmpl_var_suffix if set.
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/netlinx/erb/helpers.rb', line 145 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 |
- (String) group(hash, padding: nil) {|key, value| ... }
Generate a group of lines for a given hash.
20 21 22 23 |
# File 'lib/netlinx/erb/helpers.rb', line 20 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 |
- (Object) hiqnet_empty
Generate an empty HiQnet address string.
289 290 291 |
# File 'lib/netlinx/erb/helpers.rb', line 289 def hiqnet_empty '0x' + ('0' * 12) end |
- (String) justify(str, amount: nil, type: nil)
Left justify a block of code to line up on a type of character. Defaults to :equals (=).
31 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 |
# File 'lib/netlinx/erb/helpers.rb', line 31 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 |
- (String) print_constant_hash(h, justify: nil)
Print the list of constants.
99 100 101 102 103 |
# File 'lib/netlinx/erb/helpers.rb', line 99 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 |
- (String) print_device_hash(h, justify: nil)
Print the list of devices.
89 90 91 92 93 |
# File 'lib/netlinx/erb/helpers.rb', line 89 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 |
- (String) print_variable_hash(h)
Print the list of variables. Format:
{
var_name: { type: :integer, default: 0, comment: 'my var' }
}
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/netlinx/erb/helpers.rb', line 112 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 |
- (Object) show_control(control, show, device: @dvTP)
Send a touch panel command to show or hide a control.
301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/netlinx/erb/helpers.rb', line 301 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 |