Class: Scat::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/scat/field.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, spec) ⇒ Field

Parses the given field specification.

Parameters:

  • label (String)

    the configuration field specification key

  • spec ({String => String})

    the configuration field specification value



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

#defaultObject (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

#helpObject (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_idObject (readonly)

The HTML input element id.



15
16
17
# File 'lib/scat/field.rb', line 15

def input_id
  @input_id
end

#labelObject (readonly)

The element label attribute.



12
13
14
# File 'lib/scat/field.rb', line 12

def label
  @label
end

#nameObject (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

#propertiesObject (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

#typeObject (readonly)

The checkbox or text element type.



9
10
11
# File 'lib/scat/field.rb', line 9

def type
  @type
end

#valueObject (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

Parameters:

  • label (String)

    the field label

  • name (Symbol)

    the field name



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 is checkbox and the field default is set

  • name: if the field type is not checkbox, then this field’s name

Parameters:

  • value (String, nil)

    the request parameter value

Returns:

  • ({Symbol => String})

    the form input element attributes



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.

Parameters:

  • q

    the PrettyPrint queue

Returns:

  • (String)

    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