Sha256: 3f2eb3642f20dce8fd0f8d2a9e9c0d4c5b4440710b5ef0b9339ab9ed60fe75f1

Contents?: true

Size: 1.93 KB

Versions: 21

Compression:

Stored size: 1.93 KB

Contents

require 'glimmer-dsl-libui'
require 'facets'

include Glimmer

Address = Struct.new(:street, :p_o_box, :city, :state, :zip_code)

def form_field(model, property)
  property = property.to_s
  entry { |e|
    label property.underscore.split('_').map(&:capitalize).join(' ')
    text model.send(property).to_s

    on_changed do
      model.send("#{property}=", e.text)
    end
  }
end

def address_form(address)
  form {
    form_field(address, :street)
    form_field(address, :p_o_box)
    form_field(address, :city)
    form_field(address, :state)
    form_field(address, :zip_code)
  }
end

def label_pair(model, attribute, value)
  name_label = nil
  value_label = nil
  horizontal_box {
    name_label = label(attribute.to_s.underscore.split('_').map(&:capitalize).join(' '))
    value_label = label(value.to_s)
  }
  Glimmer::DataBinding::Observer.proc do
    value_label.text = model.send(attribute)
  end.observe(model, attribute)
end

def address(address)
  vertical_box {
    address.each_pair do |attribute, value|
      label_pair(address, attribute, value)
    end
  }
end

address1 = Address.new('123 Main St', '23923', 'Denver', 'Colorado', '80014')
address2 = Address.new('2038 Park Ave', '83272', 'Boston', 'Massachusetts', '02101')

window('Method-Based Custom Keyword') {
  margined true
  
  horizontal_box {
    vertical_box {
      label('Address 1') {
        stretchy false
      }
      
      address_form(address1)
      
      horizontal_separator {
        stretchy false
      }
      
      label('Address 1 (Saved)') {
        stretchy false
      }
      
      address(address1)
    }
    
    vertical_separator {
      stretchy false
    }
    
    vertical_box {
      label('Address 2') {
        stretchy false
      }
      
      address_form(address2)
      
      horizontal_separator {
        stretchy false
      }
      
      label('Address 2 (Saved)') {
        stretchy false
      }
      
      address(address2)
    }
  }
}.show

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
glimmer-dsl-libui-0.4.0 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.3.5 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.3.4 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.3.3 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.3.2 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.3.1 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.3.0 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.24 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.23 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.22 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.21 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.20 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.19 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.18 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.17 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.16 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.15 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.14 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.13 examples/method_based_custom_keyword.rb
glimmer-dsl-libui-0.2.12 examples/method_based_custom_keyword.rb