Class: IRuby::Input::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/iruby/input/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Builder

Returns a new instance of Builder.



6
7
8
9
10
# File 'lib/iruby/input/builder.rb', line 6

def initialize &block
  @processors = {}
  @fields, @buttons = [], []
  instance_eval &block
end

Instance Attribute Details

#buttonsObject (readonly)

Returns the value of attribute buttons.



4
5
6
# File 'lib/iruby/input/builder.rb', line 4

def buttons
  @buttons
end

#fieldsObject (readonly)

Returns the value of attribute fields.



4
5
6
# File 'lib/iruby/input/builder.rb', line 4

def fields
  @fields
end

Instance Method Details

#add_button(button) ⇒ Object



16
17
18
19
# File 'lib/iruby/input/builder.rb', line 16

def add_button button
  # see bit.ly/1Tsv6x4
  @buttons.unshift button
end

#add_field(field) ⇒ Object



12
13
14
# File 'lib/iruby/input/builder.rb', line 12

def add_field field
  @fields << field
end

#html(&block) ⇒ Object



21
22
23
24
25
# 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) ⇒ Object



31
32
33
# File 'lib/iruby/input/builder.rb', line 31

def password key='password', **params
  input key, **params.merge(type: 'password')
end

#process_result(result) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



27
28
29
# File 'lib/iruby/input/builder.rb', line 27

def text string
  html { label string }
end