Sha256: 3e6392f1d01aae5354e72d880f978eea0943153d2cc7d1f933f46a70a03c79f6

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

module Nitro

# A helper mixin for programmatically building XHTML 
# blocks. 

module XhtmlMixin

  # Render select options. The parameter is a hash of options.
  #
  # [+labels+]
  #   The option labels.
  #
  # [+values+]
  #    The corresponding values.
  #
  #  [+selected+]
  #    The value of the selected option.
  #
  # === Examples
  #
  # labels = ['Male', 'Female']
  # o.select(:name => 'sex') { 
  #    o.options(:labels => labels, :selected => 1) 
  # }
  #
  # or
  #
  # #{options :labels => labels, :values => [..], :selected => 1}
  # #{build :options, :labels => labels, :values => [..], :selected => 1}
  
  def options(options = {})
    if labels = options[:labels]
      str = ''
      
      unless values = options[:values]
        values = (0...labels.size).to_a
      end
      
      selected = (options[:selected] || -1).to_i
      
      labels.each_with_index do |label, idx|
        value = values[idx]
        if value == selected
          str << %|<option value="#{value}" selected="1">#{label}</option>|
        else
          str << %|<option value="#{value}">#{label}</option>|
        end
      end
      
      return str
    else
      raise ArgumentError.new('No labels provided')
    end
  end

  # Render a submit input.
  
  def submit(options = nil)
    str = ''
    
    if options
      opts = options.collect { |k, v| %[#{k}="#{v}"] }.join(' ')
      str << %[<input type="submit" #{opts} />]
    else
      str << %|<input type="submit" />|
    end

    return str
  end
  
end

end

# * George Moschovitis <gm@navel.gr>

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nitro-0.21.0 lib/nitro/mixin/xhtml.rb
nitro-0.21.2 lib/nitro/mixin/xhtml.rb
nitro-0.22.0 lib/nitro/mixin/xhtml.rb