Class Simplepay::Support::Field
In: lib/simplepay/support/field.rb
Parent: Object
RuntimeError Error Service Standard Marketplace Donation Subscription MarketplacePolicy Interval SubscriptionPeriod BillingFrequency Comparable Field Service Epoch Currency Amount Boolean ResponseStatusCode Services Authentication FormHelper NotificationHelper RailsHelper Helpers Interval Currency Support Boolean Simplepay dot/f_2.png

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

Methods

<=>   new   required?   service_name   to_input   to_s  

Included Modules

Comparable

Constants

ALLOWED_OPTIONS = [:as, :class, :required, :value]

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

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)

[Source]

# File lib/simplepay/support/field.rb, line 37
      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

Sorting is based solely by service_name.

[Source]

# File lib/simplepay/support/field.rb, line 101
      def <=>(o)
        self.service_name <=> o.service_name
      end

Returns true if the field is required for the service.

[Source]

# File lib/simplepay/support/field.rb, line 65
      def required?
        @options[:required] ? true : false
      end

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.

[Source]

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

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

[Source]

# File lib/simplepay/support/field.rb, line 72
      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

[Validate]