class Olelo::Attributes::Attribute::Enum

Public Class Methods

new(parent, name, values = {}) click to toggle source
Calls superclass method Olelo::Attributes::Attribute::new
# File lib/olelo/attributes.rb, line 84
def initialize(parent, name, values = {})
  super(parent, name)
  raise 'Values must be Proc, Hash or Array' unless Proc === values || Hash === values || Array === values
  @values = values
end

Public Instance Methods

field_tag(attr) click to toggle source
# File lib/olelo/attributes.rb, line 90
def field_tag(attr)
  html = %{<select class="observe" id="#{key}" name="#{key}">
           <option#{values.any? {|value,label| attr == value} ? '' : ' selected="selected"'}></option>}
  values.sort_by(&:last).each do |value,label|
    value_attr = value == label ? '' : %{ value="#{escape_html value}"}
    selected_attr = attr == value ? ' selected="selected"' : ''
    html << %{<option#{value_attr}#{selected_attr}>#{escape_html label}</option>}
  end
  html << '</select>'
end
parse(params) click to toggle source
# File lib/olelo/attributes.rb, line 101
def parse(params)
  value = params[key]
  value if values.include?(value)
end

Private Instance Methods

values() click to toggle source
# File lib/olelo/attributes.rb, line 108
def values
  if Proc === @values
    @values = @values.call
    raise 'Values must be Hash or Array' unless Hash === @values || Array === @values
  end
  @values = Hash[*@values.zip(@values)] if Array === @values
  @values
end