Class: Simplepay::Support::Field
- Inherits:
-
Object
- Object
- Simplepay::Support::Field
- Includes:
- ActionView::Helpers::FormTagHelper, 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)
39 40 41 42 43 44 |
# File 'lib/simplepay/support/field.rb', line 39 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)
21 22 23 |
# File 'lib/simplepay/support/field.rb', line 21 def name @name end |
#service ⇒ Object (readonly)
The parent service of the field
18 19 20 |
# File 'lib/simplepay/support/field.rb', line 18 def service @service end |
Instance Method Details
#<=>(o) ⇒ Object
Sorting is based solely by service_name
.
103 104 105 |
# File 'lib/simplepay/support/field.rb', line 103 def <=>(o) self.service_name <=> o.service_name end |
#==(o) ⇒ Object
:nodoc:
107 108 109 110 |
# File 'lib/simplepay/support/field.rb', line 107 def ==(o) #:nodoc: self.service == o.service && self.name == o.name end |
#clone_for(o) ⇒ Object
:nodoc:
116 117 118 |
# File 'lib/simplepay/support/field.rb', line 116 def clone_for(o) #:nodoc: self.class.new(o, deep_clone(@name), deep_clone(@options)) end |
#delegate ⇒ Object
:nodoc:
120 121 122 |
# File 'lib/simplepay/support/field.rb', line 120 def delegate #:nodoc: @delegate end |
#delegated? ⇒ Boolean
:nodoc:
79 80 81 |
# File 'lib/simplepay/support/field.rb', line 79 def delegated? #:nodoc: @delegate end |
#inspect ⇒ Object
:nodoc:
112 113 114 |
# File 'lib/simplepay/support/field.rb', line 112 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.
67 68 69 |
# File 'lib/simplepay/support/field.rb', line 67 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.
53 54 55 56 57 58 59 60 61 |
# 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 |
#to_input ⇒ Object
Converts a Field into an HTML HIDDEN INPUT element as a string.
74 75 76 77 |
# 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 |
#value ⇒ Object
:nodoc:
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/simplepay/support/field.rb', line 87 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:
83 84 85 |
# File 'lib/simplepay/support/field.rb', line 83 def value=(v) #:nodoc: @value = delegated? ? @delegate.new(v) : v end |