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) ⇒ Form

Returns a new instance of Form.



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

def initialize data_dictionary
  @data_dictionary = data_dictionary
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

field accessors



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

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

Instance Attribute Details

#data_dictionaryObject

Returns the value of attribute data_dictionary.



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

def data_dictionary
  @data_dictionary
end

#responsesObject

Returns the value of attribute responses.



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

def responses
  @responses
end

Instance Method Details

#fieldsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/red_cap/form.rb', line 34

def fields
  @fields ||= begin
    fs = data_dictionary.map do |attributes|
      klass = lookup_field_class(attributes["field_type"])
      klass.new(attributes)
    end
    fs.each do |field|
      field.associated_fields = fs.select do |f|
        f.branching_logic =~ /^\[#{field.field_name}\(.+\)\]="1"$/
      end
    end
    fs
  end
end

#find_field(key, field_class, options) ⇒ Object



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

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