class Simplepay::Support::Field

Represents a data field which will ultimately populate a Simple Pay form. These fields are often unique to their service.

Constants

ALLOWED_OPTIONS

Attributes

name[R]

The name of the field used in your local program (may be different than the service name)

service[R]

The parent service of the field

Public Class Methods

new(service, name, options) click to toggle source
name

Name which is used when referring to this field in the code.

options

An optional hash of field options.

Options

as

Overrides the name when used by the service to the exact string or symbol (camelized) given.

class

Delegate the value mechanics to a separate class.

required

Tells whether or not this field is required for its service.

value

Set the value of the field at initialization.

Example

Field.new(:example, :required => true, :as => 'ExAmplE')
Field.new(:delegated, :class => Simplepay::Support::Boolean, :value => true)
# File lib/simplepay/support/field.rb, line 39
def initialize(service, name, options)
  @service, @name, @options = service, name, normalize_options!(options)
  @delegate   = options[:class]
  @value      = nil
  self.value  = options[:value] if options[:value]
end

Public Instance Methods

<=>(o) click to toggle source

Sorting is based solely by service_name.

# File lib/simplepay/support/field.rb, line 103
def <=>(o)
  self.service_name <=> o.service_name
end
required?() click to toggle source

Returns true if the field is required for the service.

# File lib/simplepay/support/field.rb, line 67
def required?
  @options[:required] ? true : false
end
service_name() click to toggle source

Returns the name of the field used by the service. This may be different than the Ruby name given to the field.

Symbol names (or :as options) are camelized. If this is not desired, use a String, instead.

# File lib/simplepay/support/field.rb, line 53
def service_name
  source = @options[:as] || @name
  case source
  when Symbol
    source.to_s.camelize(:lower)
  else
    source
  end
end
Also aliased as: to_s
to_input() click to toggle source

Converts a Field into an HTML HIDDEN INPUT element as a string.

# File lib/simplepay/support/field.rb, line 74
def to_input
  raise(RequiredFieldMissing, "Missing Required Field value for #{name}") if required? && value.blank?
  value.blank? ? '' : html_input_tag
end
to_s()
Alias for: service_name

Private Instance Methods

deep_clone(o) click to toggle source
# File lib/simplepay/support/field.rb, line 137
def deep_clone(o)
  Marshal::load(Marshal.dump(o))
end