Class: Scat::Field
- Inherits:
-
Object
- Object
- Scat::Field
- Defined in:
- lib/scat/field.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
If the field type is
checkbox
, then this flag determines whether the box is initially checked. -
#help ⇒ Object
readonly
The optional help text displayed in a web hover pop-up.
-
#input_id ⇒ Object
readonly
The HTML input element id.
-
#label ⇒ Object
readonly
The element label attribute.
-
#name ⇒ Object
(also: #to_s)
readonly
The underscore label with any other special characters removed, converted to a symbol.
-
#properties ⇒ Object
readonly
The (class,
Jinx::Property
) tuples which determine which property is set by the input value. -
#type ⇒ Object
readonly
The
checkbox
ortext
element type. -
#value ⇒ Object
readonly
This field’s value if the field type is
checkbox
, otherwise nil.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(label, spec) ⇒ Field
constructor
Parses the given field specification.
-
#input_attributes(value) ⇒ {Symbol => String}
Returns this field’s HTML input element attributes and target caTissue properties.
-
#pretty_print(q) ⇒ String
The formatted content of this field.
Constructor Details
#initialize(label, spec) ⇒ Field
Parses the given field specification.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/scat/field.rb', line 43 def initialize(label, spec) @name = Field.name_for(label) @input_id = @name + '_input' @label = label # Parse the property list. @properties = spec['properties'].delete(' ').split(',').map do |pspec| klass_s, attr_s = pspec.split('.') begin klass = CaTissue.module_with_name(klass_s) [klass, klass.property(attr_s.to_sym)] rescue raise ScatError.new("Scat configuration field not recognized: #{pspec} - " + $!) end end @type = spec['type'] # The default type is checkbox if the label ends in ?, text otherwise. @type ||= label[-1, 1] == '?' ? 'checkbox' : 'text' # A checkbox has a value. if @type == 'checkbox' then @value = spec['value'] @value ||= label[-1, 1] == '?' ? label[0...-1] : label end @help = spec['help'] @default = spec['default'] end |
Instance Attribute Details
#default ⇒ Object (readonly)
If the field type is checkbox
, then this flag determines whether the box is initially checked.
29 30 31 |
# File 'lib/scat/field.rb', line 29 def default @default end |
#help ⇒ Object (readonly)
The optional help text displayed in a web hover pop-up.
22 23 24 |
# File 'lib/scat/field.rb', line 22 def help @help end |
#input_id ⇒ Object (readonly)
The HTML input element id.
15 16 17 |
# File 'lib/scat/field.rb', line 15 def input_id @input_id end |
#label ⇒ Object (readonly)
The element label attribute.
12 13 14 |
# File 'lib/scat/field.rb', line 12 def label @label end |
#name ⇒ Object (readonly) Also known as: to_s
The underscore label with any other special characters removed, converted to a symbol. For example, an element with label Tissue Site has name :tissue_site
.
6 7 8 |
# File 'lib/scat/field.rb', line 6 def name @name end |
#properties ⇒ Object (readonly)
The (class, Jinx::Property
) tuples which determine which property is set by the input value.
19 20 21 |
# File 'lib/scat/field.rb', line 19 def properties @properties end |
#type ⇒ Object (readonly)
The checkbox
or text
element type.
9 10 11 |
# File 'lib/scat/field.rb', line 9 def type @type end |
#value ⇒ Object (readonly)
This field’s value if the field type is checkbox
, otherwise nil.
25 26 27 |
# File 'lib/scat/field.rb', line 25 def value @value end |
Class Method Details
.name_for(label) ⇒ Object
33 34 35 36 37 |
# File 'lib/scat/field.rb', line 33 def self.name_for(label) name = label.gsub(' ', '_').gsub(/[^\w]/, '').downcase # Strip the trailing ?, if any. name[-1, 1] == '?' ? name[0...-1] : name end |
Instance Method Details
#input_attributes(value) ⇒ {Symbol => String}
Returns this field’s HTML input element attributes and target caTissue properties.
The attributes are determined by the field configuration as follows:
-
id
: this field’s HTML input element id -
type
: this field’s type -
checked
:true
if the field type ischeckbox
and the field default is set -
name
: if the field type is notcheckbox
, then this field’s name
79 80 81 82 83 84 85 86 |
# File 'lib/scat/field.rb', line 79 def input_attributes(value) params = {:id => input_id, :type => type, :name => name, :value => value } if type == 'checkbox' then params[:value] ||= self.value params[:checked] = true if default end params end |
#pretty_print(q) ⇒ String
Returns the formatted content of this field.
90 91 92 93 94 95 |
# File 'lib/scat/field.rb', line 90 def pretty_print(q) q.text(name) avh = [:label, :type, :help, :value, :default].to_compact_hash { |a| send(a) } avh[:properties] = properties.map { |k, p| [k.name.demodulize, p.to_s].join('.') } q.pp_hash(avh) end |