Class: REDCap::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/red_cap/form.rb,
lib/red_cap/form/fields.rb

Defined Under Namespace

Classes: Checkboxes, CheckboxesWithCheckboxesOrOther, CheckboxesWithOther, CheckboxesWithRadioButtonsOrOther, Descriptive, Dropdown, Field, File, Notes, RadioButtons, Sql, Text, Yesno

Constant Summary collapse

Radio =

default “radio” implementation

RadioButtons
Checkbox =

default “checkbox” implementation

CheckboxesWithOther

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_dictionary, responses) ⇒ Form

Returns a new instance of Form.



6
7
8
9
# File 'lib/red_cap/form.rb', line 6

def initialize data_dictionary, responses
  @data_dictionary = data_dictionary
  @responses = responses
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ Object

field accessors



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/red_cap/form.rb', line 14

def method_missing method, *args, **kwargs, &block
  key = method.to_s
  field_class = kwargs[:as]
  if field_class.is_a?(Symbol)
    field_class = lookup_field_class(field_class.to_s)
  end
  if field = find_field(key, field_class)
    field.value
  else
    super
  end
end

Instance Attribute Details

#data_dictionaryObject (readonly)

Returns the value of attribute data_dictionary.



11
12
13
# File 'lib/red_cap/form.rb', line 11

def data_dictionary
  @data_dictionary
end

#responsesObject (readonly)

Returns the value of attribute responses.



11
12
13
# File 'lib/red_cap/form.rb', line 11

def responses
  @responses
end

Instance Method Details

#fieldsObject



33
34
35
36
37
38
# File 'lib/red_cap/form.rb', line 33

def fields
  @fields ||= data_dictionary.map do |attributes|
    klass = lookup_field_class(attributes["field_type"])
    klass.new(self, attributes, responses)
  end
end

#find_field(key, field_class = nil) ⇒ Object



27
28
29
30
31
# File 'lib/red_cap/form.rb', line 27

def find_field key, field_class=nil
  field = fields.find { |field| field.field_name == key }
  field = field_class.new(self, field.attributes, responses) if field_class
  field
end