class IRuby::Input::Builder

Attributes

buttons[R]
fields[R]

Public Class Methods

new(&block) click to toggle source
# File lib/iruby/input/builder.rb, line 6
def initialize &block
  @processors = {}
  @fields, @buttons = [], []
  instance_eval &block
end

Public Instance Methods

add_button(button) click to toggle source
# File lib/iruby/input/builder.rb, line 16
def add_button button
  # see bit.ly/1Tsv6x4
  @buttons.unshift button
end
add_field(field) click to toggle source
# File lib/iruby/input/builder.rb, line 12
def add_field field
  @fields << field
end
html(&block) click to toggle source
# File lib/iruby/input/builder.rb, line 21
def html &block
  add_field Class.new(Widget) {
    define_method(:widget_html) { instance_eval &block }
  }.new 
end
password(key='password', **params) click to toggle source
# File lib/iruby/input/builder.rb, line 31
def password key='password', **params
  input key, **params.merge(type: 'password')
end
process_result(result) click to toggle source
# File lib/iruby/input/builder.rb, line 35
def process_result result
  unless result.nil?
    result.each_with_object({}) do |(k,v),h|
      if @processors.has_key? k
        @processors[k].call h, k, v
      else
        h[k.to_sym] = v
      end
    end
  end
end
text(string) click to toggle source
# File lib/iruby/input/builder.rb, line 27
def text string
  html { label string }
end

Private Instance Methods

process(key, &block) click to toggle source
# File lib/iruby/input/builder.rb, line 49
def process key, &block
  @processors[key.to_s] = block
end
unique_key(key) click to toggle source
# File lib/iruby/input/builder.rb, line 53
def unique_key key
  @keys ||= []
  
  if @keys.include? key
    (2..Float::INFINITY).each do |i|
      test = "#{key}#{i}"
      break key = test unless @keys.include? test
    end
  end
  
  @keys << key; key
end