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
-
#group(hash, padding: nil) {|key, value| ... } ⇒ String
Generate a group of lines for a given hash.
-
#justify(str, amount: nil, type: nil) ⇒ String
Left justify a block of code to line up on a type of character.
Code Generation collapse
-
#auto_comment(str, condition) ⇒ String
Automatically comment out the input unless the condition is met.
-
#button_event(buttons, device = @dvTP) ⇒ String
Generate button events for the given hash of buttons.
-
#button_event_block(buttons, **kwargs) {|value, name| ... } ⇒ Object
Generate a button_event block for the given hash of buttons.
-
#generate_constant_ivars(h, append_suffix: false) ⇒ Object
Generate instance variables for the DEFINE_CONSTANTS section from the given hash keys.
-
#generate_variable_ivars(h, append_suffix: true) ⇒ Object
Generate instance variables for the DEFINE_VARIABLES section from the given hash keys.
-
#hiqnet_empty ⇒ Object
Generate an empty HiQnet address string.
-
#print_constant_hash(h, justify: nil) ⇒ String
Print the list of constants.
-
#print_device_hash(h, justify: nil) ⇒ String
Print the list of devices.
-
#print_variable_hash(h) ⇒ String
Print the list of variables.
-
#show_control(control, show, device: @dvTP) ⇒ Object
Send a touch panel command to show or hide a control.
Other collapse
-
#check_for_duplicate_values(*hashes) ⇒ Object
Ensures button number constants haven’t been unintentionally duplicated.
Instance Method Details
#auto_comment(str, condition) ⇒ String
Automatically comment out the input unless the condition is met.
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.
170 171 172 |
# File 'lib/netlinx/erb/helpers.rb', line 170 def , device = @dvTP .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.
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 , **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 |
#check_for_duplicate_values(*hashes) ⇒ Object
Ensures button number constants haven’t been unintentionally duplicated.
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.
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_empty ⇒ Object
Generate an empty HiQnet address string.
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 (=).
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_constant_hash(h, justify: nil) ⇒ String
Print the list of constants.
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_device_hash(h, justify: nil) ⇒ String
Print the list of devices.
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_variable_hash(h) ⇒ String
Print the list of variables. Format:
{
var_name: { type: :integer, default: 0, comment: 'my var' }
}
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.
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 |