Class: Simplepay::Support::Field
- Inherits:
-
Object
- Object
- Simplepay::Support::Field
- Includes:
- Comparable
- Defined in:
- lib/simplepay/support/field.rb
Overview
Represents a data field which will ultimately populate a Simple Pay form. These fields are often unique to their service.
Constant Summary collapse
- ALLOWED_OPTIONS =
[:as, :class, :required, :value]
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The name of the field used in your local program (may be different than the service name).
-
#service ⇒ Object
readonly
The parent service of the field.
Instance Method Summary collapse
-
#<=>(o) ⇒ Object
Sorting is based solely by
service_name
. -
#==(o) ⇒ Object
:nodoc:.
-
#clone_for(o) ⇒ Object
:nodoc:.
-
#delegate ⇒ Object
:nodoc:.
-
#delegated? ⇒ Boolean
:nodoc:.
-
#initialize(service, name, options) ⇒ Field
constructor
- name
-
Name which is used when referring to this field in the code.
-
#inspect ⇒ Object
:nodoc:.
-
#required? ⇒ Boolean
Returns
true
if the field is required for the service. -
#service_name ⇒ Object
(also: #to_s)
Returns the name of the field used by the service.
-
#to_input ⇒ Object
Converts a Field into an HTML HIDDEN INPUT element as a string.
-
#value ⇒ Object
:nodoc:.
-
#value=(v) ⇒ Object
:nodoc:.
Constructor Details
#initialize(service, name, options) ⇒ Field
- 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)
37 38 39 40 41 42 |
# File 'lib/simplepay/support/field.rb', line 37 def initialize(service, name, ) @service, @name, @options = service, name, () @delegate = [:class] @value = nil self.value = [:value] if [:value] end |
Instance Attribute Details
#name ⇒ Object (readonly)
The name of the field used in your local program (may be different than the service name)
19 20 21 |
# File 'lib/simplepay/support/field.rb', line 19 def name @name end |
#service ⇒ Object (readonly)
The parent service of the field
16 17 18 |
# File 'lib/simplepay/support/field.rb', line 16 def service @service end |
Instance Method Details
#<=>(o) ⇒ Object
Sorting is based solely by service_name
.
101 102 103 |
# File 'lib/simplepay/support/field.rb', line 101 def <=>(o) self.service_name <=> o.service_name end |
#==(o) ⇒ Object
:nodoc:
105 106 107 108 |
# File 'lib/simplepay/support/field.rb', line 105 def ==(o) #:nodoc: self.service == o.service && self.name == o.name end |
#clone_for(o) ⇒ Object
:nodoc:
114 115 116 |
# File 'lib/simplepay/support/field.rb', line 114 def clone_for(o) #:nodoc: self.class.new(o, deep_clone(@name), deep_clone(@options)) end |
#delegate ⇒ Object
:nodoc:
118 119 120 |
# File 'lib/simplepay/support/field.rb', line 118 def delegate #:nodoc: @delegate end |
#delegated? ⇒ Boolean
:nodoc:
77 78 79 |
# File 'lib/simplepay/support/field.rb', line 77 def delegated? #:nodoc: @delegate end |
#inspect ⇒ Object
:nodoc:
110 111 112 |
# File 'lib/simplepay/support/field.rb', line 110 def inspect #:nodoc: %|#<#{self.class.name} Name:"#{@name}"#{" ServiceName:\"#{service_name}\"" if service_name != @name}#{" REQUIRED" if required?}>| end |
#required? ⇒ Boolean
Returns true
if the field is required for the service.
65 66 67 |
# File 'lib/simplepay/support/field.rb', line 65 def required? @options[:required] ? true : false end |
#service_name ⇒ Object Also known as: to_s
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.
51 52 53 54 55 56 57 58 59 |
# 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 |
#to_input ⇒ Object
Converts a Field into an HTML HIDDEN INPUT element as a string.
72 73 74 75 |
# 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 |
#value ⇒ Object
:nodoc:
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/simplepay/support/field.rb', line 85 def value #:nodoc: if delegated? @value.to_s else case @value when Proc @value.call(self) else @value end end end |
#value=(v) ⇒ Object
:nodoc:
81 82 83 |
# File 'lib/simplepay/support/field.rb', line 81 def value=(v) #:nodoc: @value = delegated? ? @delegate.new(v) : v end |