Sha256: f97e2f81dba249b031cb3372fecfd2db895d4691e42f34340ea42e32836efe69

Contents?: true

Size: 1.6 KB

Versions: 5

Compression:

Stored size: 1.6 KB

Contents

class Rtml::Widgets::Shorthand < Rtml::Widget
  affects :screen, :submit
  entry_point :submit, :getvar

  # Creates a TML +submit+ tag. Options include :target and :error.
  #
  #   screen :do_submit do
  #     submit :target => url_for(:action => 'proc_submit'), :error => 'submit_err' do
  #       getvar :name => 'amount'
  #     end
  #   end
  #
  # Note that the #submit method can only be called from a Screen element, even though #submit is a registered
  # target.
  def submit(*args)
    validate_parent :screen
    parent.build :submit, hash_for_args(args, :target, :error).rename(:target => :tgt, :error => :econn)
  end

  # Creates a TML +getvar+ tag for submission of data to the server.
  #
  #   screen :do_submit do
  #     submit :target => url_for(:action => 'proc_submit'), :error => 'submit_err' do
  #       getvar :name => 'amount'
  #     end
  #   end
  #
  # Note that the #getvar method can only be called from a Submit element, even though #screen is a registered
  # target.
  def getvar(*args)
    validate_parent :submit
    parent.build :getvar, hash_for_args(args, :name)
  end

  private
  # First, calls Array#extract_options! to create a standard options hash.
  # Then, iterates through *keys and maps values from args in the order they appear in keys. These
  # can overwrite options found in extract_options!
  #
  # Example:
  #   hash_for_args([1,2,3,{:a => 1}], :arg1, :arg2, :a)
  #     => { :arg1 => 1, :arg2 => 2, :a => 3 }
  #
  def hash_for_args(args, *keys)
    options = args.extract_options!
    keys.each { |key| options[key] = args.shift unless args.empty? }
    options
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rtml-2.0.4 builtin/widgets/shorthand.rb
rtml-2.0.3 builtin/widgets/shorthand.rb
rtml-2.0.2 builtin/widgets/shorthand.rb
rtml-2.0.1 builtin/widgets/shorthand.rb
rtml-2.0.0.alpha.1 builtin/widgets/shorthand.rb