module Egalite

キャッシュテーブルのデータ定義:

CREATE TABLE controller_cache (

id SERIAL PRIMARY KEY,
inner_path TEXT NOT NULL,
query_string TEXT,
language TEXT,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
content TEXT NOT NULL

);

end

used with http parameters simpler version of HashWithIndifferentAccess of Ruby on Rails

Constants

VERSION

Public Class Methods

new(data = {}, param_name = nil, opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 121
def initialize(data = {}, param_name = nil, opts = {})
  @data = lambda { |k|
    if data.respond_to?(k)
      data.send(k)
    elsif data.respond_to?(:[])
      data[k]
    end
  }

  @param_name = param_name
  @form_opts = opts
end

Public Instance Methods

_text(value, name, opts) click to toggle source
# File lib/egalite/helper.rb, line 142
def _text(value, name, opts)
  attrs = opt_as_hash(opts)
  attrs[:value] = value if value
  attrs[:size] ||= 30
  attrs[:type] = 'text'
  attrs[:name] = expand_name(name)
  tag_solo(:input, attrs)
end
checkbox(name, value="true", opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 176
def checkbox(name, value="true", opts = {})
  checked = (@data[name] || opts[:default] || opts[:checked])
  checked = false if @data[name] == false
  
  attr_cb = opt_as_hash(opts)
  attr_cb[:type] = 'checkbox'
  attr_cb[:name] = expand_name(name)
  attr_cb[:value] = value
  attr_cb[:checked] = "checked" if checked

  ucv = opts[:uncheckedvalue] || 'false'
  attr_h = {:type => 'hidden', :name => expand_name(name), :value => ucv}
  hidden = opts[:nohidden] ? '' : tag_solo(:input, attr_h)
  
  raw "#{hidden}#{tag_solo(:input, attr_cb)}"
end
close() click to toggle source
# File lib/egalite/helper.rb, line 139
def close
  tag_close(:form,nil)
end
file(name, opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 208
def file(name, opts = {})
  attrs = opt_as_hash(opts)
  attrs[:name] = expand_name(name)
  attrs[:type] = 'file'
  tag_solo(:input, attrs)
end
form(method, url=nil) click to toggle source
# File lib/egalite/helper.rb, line 133
def form(method, url=nil)
  attrs = opt_as_hash(@form_opts)
  attrs[:method] = method.to_s.upcase
  attrs[:action] = url if url
  tag_open(:form,attrs)
end
hidden(name, opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 168
def hidden(name, opts = {})
  value = @data[name] || opts[:default]
  attrs = opt_as_hash(opts)
  attrs[:value] = value if value
  attrs[:type] = "hidden"
  attrs[:name] = expand_name(name)
  tag_solo(:input,attrs)
end
image() click to toggle source
# File lib/egalite/helper.rb, line 221
def image
end
opt_as_hash(opts) click to toggle source
# File lib/egalite/helper.rb, line 111
def opt_as_hash(opts)
  o = opts.dup
  o.each_key { |k|
    o.delete(k) if [:default,:checked,:selected, :nil].member?(k)
  }
  o
end
password(name, opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 160
def password(name, opts = {})
  value = @data[name] || opts[:default]
  attrs = opt_as_hash(opts)
  attrs[:value] = value if value
  attrs[:type] = "password"
  attrs[:name] = expand_name(name)
  tag_solo(:input,attrs)
end
radio(name, choice, opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 192
def radio(name, choice, opts = {})
  selected = (@data[name] == choice)
  selected = (opts[:default] == choice) || opts[:selected] || opts[:checked] if @data[name] == nil
  
  attrs = opt_as_hash(opts)
  attrs[:checked] = 'checked' if selected
  attrs[:name] = expand_name(name)
  attrs[:value] = choice
  attrs[:type] = 'radio'
  
  tag_solo(:input, attrs)
end
select_by_array(name, options, opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 223
def select_by_array(name, options, opts = {})
  optionstr = options.map {|o|
    (value,optname) = if o.is_a?(Array)
      o
    else
      [o,o]
    end
    flag = value == @data[name]
    a = {:value => value}
    a[:selected] = 'selected' if flag
    "#{tag_open(:option, a)}#{escape_html(optname)}</option>"
  }.join('')
  
  raw "<select name='#{expand_name(name)}'#{opt(opts)}>#{optionstr}</select>"
end
select_by_association(name, options, optname, opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 238
def select_by_association(name, options, optname, opts = {})
  idname = (opts[:idname] || "id").to_sym
  optionstr = options.map {|o|
    flag = o[idname] == @data[name]
    a = {:value => o[idname]}
    a[:selected] = 'selected' if flag
    "#{tag_open(:option, a)}#{escape_html(o[optname])}</option>"
  }.join('')
  
  if opts[:nil]
    selected = (@data[name] == nil) ? "selected='selected'" : ''
    optionstr = "<option value='' #{selected}>#{escape_html(opts[:nil])}</option>" + optionstr
  end
  
  raw "<select name='#{expand_name(name)}'#{opt(opts)}>#{optionstr}</select>"
end
submit(value = nil, name = nil, opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 214
def submit(value = nil, name = nil, opts = {})
  attrs = opt_as_hash(opts)
  attrs[:name] = expand_name(name) if name
  attrs[:value] = value if value
  attrs[:type] = 'submit'
  tag_solo(:input, attrs)
end
text(name, opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 150
def text(name, opts = {})
  _text(@data[name] || opts[:default], name, opts)
end
textarea(name, opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 204
def textarea(name, opts = {})
  value = escape_html(@data[name] || opts[:default])
  raw "<textarea name='#{expand_name(name)}'#{opt(opts)}>#{value}</textarea>"
end
timestamp_text(name, opts = {}) click to toggle source
# File lib/egalite/helper.rb, line 153
def timestamp_text(name, opts = {})
  # todo: enable locale
  # todo: could unify to text()
  value = @data[name] || opts[:default]
  value = value.strftime('%Y-%m-%d %H:%M:%S')
  _text(value,name,opts)
end