class AmsLayout::DelegateWriter

Attributes

class_name[W]
delegated_class_name[W]
source_file_name[W]

Public Instance Methods

aliases() click to toggle source

List of aliases for specified fields

# File lib/ams_layout/delegate_writer.rb, line 49
def aliases
  @aliases ||= {}
end
aliases=(data) click to toggle source
# File lib/ams_layout/delegate_writer.rb, line 53
def aliases=(data)
  @aliases = Hash(data)
end
class_name() click to toggle source

Name of this class

# File lib/ams_layout/delegate_writer.rb, line 33
def class_name
  @class_name ||= AmsLayout.configuration.delegate_class_name
end
delegated_class_name() click to toggle source

Name of class we will delegate to

# File lib/ams_layout/delegate_writer.rb, line 41
def delegated_class_name
  @delegated_class_name ||= AmsLayout.configuration.layout_class_name
end
source_file_name() click to toggle source

Name of this class’ source file

# File lib/ams_layout/delegate_writer.rb, line 25
def source_file_name
  class_name.ams_layout_snakecase + '.rb'
end
write(stream, layout) click to toggle source

Write a class file, based on the the layout.yml, to the provided stream

# File lib/ams_layout/delegate_writer.rb, line 61
def write stream, layout
  stream << header

  layout.each do |section_label, fields|
    stream << section(section_label)

    fields.each do |fld|
      stream << field(fld[:label], fld[:id], fld[:type])
      write_aliases stream, fld[:label], fld[:id], fld[:type]
    end # fields
  end # layout

  stream << footer
end

Private Instance Methods

checkbox_field_methods(label, id, type) click to toggle source

Emit checkbox field methods

# File lib/ams_layout/delegate_writer.rb, line 304
    def checkbox_field_methods label, id, type
      field_label = label.ams_layout_snakecase
      field_id = '#' + id

      text =<<TEXT

  def check_#{field_label}
    if ! checked?('#{field_id}')
      super()
    # else skip sending the value.
    end
  end

  def uncheck_#{field_label}
    if checked?('#{field_id}')
      super()
    # else skip sending the value.
    end
  end
TEXT
    end
delme_snakecase(str) click to toggle source

Convert a field name to snake_case

# File lib/ams_layout/delegate_writer.rb, line 342
def delme_snakecase str
  snake = str.gsub /[^a-zA-Z0-9]/, '_'
  snake = snake.gsub /_+/, '_'
  snake.downcase
end
field(label, id, type) click to toggle source

Emit field methods

# File lib/ams_layout/delegate_writer.rb, line 192
def field label, id, type
  case type
  when 'text'
    return text_field_methods(label, id, type)
  when 'textarea'
    return textarea_field_methods(label, id, type)
  when 'select'
    return select_field_methods(label, id, type)
  when 'checkbox'
    return checkbox_field_methods(label, id, type)
  else
    return "\n# unknown_field_type: #{label}, #{id}, #{type}\n"
  end
end
field_alias(label, id, type) click to toggle source

Emit field methods for aliased fields

# File lib/ams_layout/delegate_writer.rb, line 211
def field_alias label, id, type
  field label, id, type
end
float_field_methods(label, id, type) click to toggle source

Emit float field methods

# File lib/ams_layout/delegate_writer.rb, line 242
    def float_field_methods label, id, type
      field_label = label.ams_layout_snakecase
      field_id = '#' + id

      text =<<TEXT

  def #{field_label}=(value)
    current_value = get_text_element_value('#{field_id}')

    current_value = '0' if current_value.empty?
    value = '0' if value.empty?

    if ((Float(current_value) * 1000) != (Float(value) * 1000))
      super(value)
    # else skip sending the value.
    end
  end
TEXT
    end
header() click to toggle source

Emit a file header

# File lib/ams_layout/delegate_writer.rb, line 93
    def header
      text =<<TEXT
#############################################################################
# #{source_file_name}
#
# This file has been generated by AmsLayout.
# Do not modify this file manually.
#
#############################################################################

require_relative 'loan_entry_fields'
require 'nokogiri'

class #{class_name} < DelegateClass(#{delegated_class_name})

  ##
  # Capture and parse the page's raw HTML with Nokogiri
  # Returns a Nokogiri::HTML document
  #

  def html_doc
    @html_doc ||= Nokogiri::HTML(__getobj__.raw_html)
      #Nokogiri::HTML(html).css("table#ctl00_ContentPlaceHolder1_tblMain").each do |tbl|
  end

  ##
  # Return the first element matching the given ID
  # Returns a Nokogiri Element
  #

  def noko_element(id)
    html_doc.css(id).first
  end

  ##
  # Returns true if element is checked
  #

  def checked?(id)
    # el['type'] = 'checkbox'
    is_checked = noko_element(id)['checked']
    ! is_checked.nil?
  end

  ##
  # Returns a text element's value
  #

  def get_text_element_value(id)
    # el['type'] = 'text'
    value = noko_element(id)['value']
  end

  ##
  # Returns a textarea element's value
  #

  def get_textarea_element_value(id)
    noko_element(id).text
  end

  ##
  # Returns the selected option, '-- Please Select --' if no option is selected
  #

  def get_selected_option id
    elem = noko_element(id)
    elem.children.each do |c|
      if c.attributes.key? 'selected'
        return c.text
      end
    end

    # Return the default value if nothing is selected
    '-- Please Select --'
  end

  #
  # Fields (ordered by section as seen on screen)
  #
TEXT
    end
section(label) click to toggle source

Emit a section header

# File lib/ams_layout/delegate_writer.rb, line 180
    def section label
      text =<<TEXT


  # Section: #{label}
TEXT
    end
select_field_methods(label, id, type) click to toggle source

Emit select field methods

# File lib/ams_layout/delegate_writer.rb, line 285
    def select_field_methods label, id, type
      field_label = label.ams_layout_snakecase
      field_id = '#' + id

      text =<<TEXT

  def #{field_label}=(value)
    if get_selected_option('#{field_id}') != value
      super(value)
    # else skip sending the value.
    end
  end
TEXT
    end
text_field_methods(label, id, type) click to toggle source

Emit text field methods

# File lib/ams_layout/delegate_writer.rb, line 219
    def text_field_methods label, id, type
      field_label = label.ams_layout_snakecase
      field_id = '#' + id

      if field_id.include? 'PlaceHolder1_cn'
        return float_field_methods label, id, type
      end

      text =<<TEXT

  def #{field_label}=(value)
    if get_text_element_value('#{field_id}') != value
      super(value)
    # else skip sending the value.
    end
  end
TEXT
    end
textarea_field_methods(label, id, type) click to toggle source

Emit textarea field methods

# File lib/ams_layout/delegate_writer.rb, line 266
    def textarea_field_methods label, id, type
      field_label = label.ams_layout_snakecase
      field_id = '#' + id

      text =<<TEXT

  def #{field_label}=(value)
    if get_textarea_element_value('#{field_id}') != value
      super(value)
    # else skip sending the value.
    end
  end
TEXT
    end
write_aliases(stream, label, id, type) click to toggle source

Write a field’s aliases to the stream

# File lib/ams_layout/delegate_writer.rb, line 82
def write_aliases stream, label, id, type
  label_aliases = aliases.key?(label) ? aliases[label] : []
  label_aliases.each do |al|
    stream << field_alias(al, id, type)
  end
end