Class: REDCap::InstrumentTable

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/red_cap/instrument_table.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pull(client, filter = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/red_cap/instrument_table.rb', line 23

def self.pull client, filter={}
  filter.reverse_merge!({
    content: "record",
    events: Array(config[:events] || config[:event]).join(","),
    fields: [config[:key]] + config[:fields],
  })

   = client.cached_json_api_request({
    content: "metadata",
    fields: config[:fields],
  })

  client.json_api_request(filter).map do |response|
    if config[:repeating]
      next unless response["redcap_repeat_instrument"] == config[:instrument_name]
      repeat_instance = response["redcap_repeat_instance"]
    end
    record = where({
      instrument_name: config[:instrument_name],
      repeat_instance: repeat_instance,
      event: response.fetch("redcap_event_name"),
      key: response.fetch(config[:key].to_s),
    }).first_or_initialize
    record.update!({
      fields: response,
      metadata: ,
    })
    record
  end.compact
end

.pulls_from(config) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/red_cap/instrument_table.rb', line 6

def self.pulls_from config
  self.config = config.reverse_merge({
    key: :study_id,
    repeating: false,
  })

  config[:fields].each do |field|
    define_method field do
      value = form.send(field)
      value.define_singleton_method :var do
        "#{config[:instrument_name]}.#{field}"
      end
      value
    end
  end
end

Instance Method Details

#formObject



54
55
56
# File 'lib/red_cap/instrument_table.rb', line 54

def form
  @form ||= REDCap::Form.new(, fields)
end