Class: Unresponsys::Row

Inherits:
Object show all
Defined in:
lib/unresponsys/row.rb

Instance Method Summary collapse

Constructor Details

#initialize(table, fields) ⇒ Row



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/unresponsys/row.rb', line 3

def initialize(table, fields)
  @table  = table
  @fields = fields

  @fields.each_pair do |key, val|
    str = key.downcase.chomp('_')
    var = "@#{str}".to_sym
    val = val.to_ruby
    self.instance_variable_set(var, val)

    if key == 'ID_'
      self.class.send(:attr_reader, str)
    else
      self.class.send(:define_method, "#{str}=") do |val|
        val = val.to_ruby
        self.instance_variable_set(var, val)
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

allow to access custom fields on new record



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/unresponsys/row.rb', line 46

def method_missing(sym, *args, &block)
  setter  = sym.to_s.include?('=')
  str     = sym.to_s.chomp('=')
  var     = "@#{str}".to_sym
  val     = args.first

  if setter
    field_name = str.upcase
    @fields[field_name] = ''
    val = val.to_ruby
    self.instance_variable_set(var, val)
  else
    self.instance_variable_get(var)
  end
end

Instance Method Details

#deleteObject



39
40
41
42
43
# File 'lib/unresponsys/row.rb', line 39

def delete
  options = { query: { qa: 'ID_', id: @fields.primary_key } }
  r = Unresponsys::Client.delete("/folders/#{@table.folder.name}/suppData/#{@table.name}/members", options)
  r['recordData']['records'][0][0].exclude?('DELETEFAILED')
end

#saveObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/unresponsys/row.rb', line 24

def save
  record_data = { fieldNames: [], records: [[]], mapTemplateName: nil }
  @fields.each_pair do |key, val|
    record_data[:fieldNames] << key
    var = "@#{key.downcase.chomp('_')}".to_sym
    val = self.instance_variable_get(var)
    val = val.to_responsys
    record_data[:records][0] << val
  end

  options = { body: { recordData: record_data, insertOnNoMatch: true, updateOnMatch: 'REPLACE_ALL' }.to_json }
  r = Unresponsys::Client.post("/folders/#{@table.folder.name}/suppData/#{@table.name}/members", options)
  r['recordData']['records'][0][0].exclude?('MERGEFAILED')
end