Module: SalesforceRecord::Attributes::ClassMethods

Defined in:
lib/salesforce_record/attributes.rb

Instance Method Summary collapse

Instance Method Details

#encode_salesforce_field(name, value) ⇒ Object

Encode a field from salesforce



86
87
88
89
90
91
# File 'lib/salesforce_record/attributes.rb', line 86

def encode_salesforce_field(name, value)
  # If existing, encode it depending on type and alias
  if (field = get_field name) && (!field.alias?)
    { field.remote_name => field.encode(value) }
  end
end

#encode_salesforce_fields(fields) ⇒ Object

Encode fields for salesforce



76
77
78
79
80
81
82
83
# File 'lib/salesforce_record/attributes.rb', line 76

def encode_salesforce_fields(fields)
  {}.tap do |encoded_fields|
    fields.each do |name, value|
      encoded_field = encode_salesforce_field(name, value)
      encoded_fields.merge! encoded_field if !encoded_field.nil?
    end
  end
end

#field_remote_name(name) ⇒ Object



105
106
107
# File 'lib/salesforce_record/attributes.rb', line 105

def field_remote_name(name)
  has_field?(name) && self.salesforce_fields[name].remote_name
end

#field_type(name) ⇒ Object



101
102
103
# File 'lib/salesforce_record/attributes.rb', line 101

def field_type(name)
  has_field?(name) && self.salesforce_fields[name].type
end

#from_salesforce(fields) ⇒ Object

Create an imported record from salesforce



61
62
63
# File 'lib/salesforce_record/attributes.rb', line 61

def from_salesforce(fields)
  new parse_salesforce_fields(fields)
end

#get_field(name) ⇒ Object



93
94
95
# File 'lib/salesforce_record/attributes.rb', line 93

def get_field(name)
  self.salesforce_fields[name]
end

#has_field?(name) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/salesforce_record/attributes.rb', line 97

def has_field?(name)
  !!get_field(name)
end

#parse_salesforce_fields(sf_response) ⇒ Object

Parse fields coming from salesforce



66
67
68
69
70
71
72
73
# File 'lib/salesforce_record/attributes.rb', line 66

def parse_salesforce_fields(sf_response)
  res = {}
  self.salesforce_fields.each do |_, field|
    value = field.find_value_in(sf_response)
    res[field.local_name] = value if !value.nil?
  end
  res
end

#salesforce_attributesObject

The attributes for this model



29
# File 'lib/salesforce_record/attributes.rb', line 29

def salesforce_attributes ; @salesforce_fields.keys ; end

#salesforce_fieldsObject

The salesforce fields and options



26
# File 'lib/salesforce_record/attributes.rb', line 26

def salesforce_fields ; @salesforce_fields ; end

#sf_attribute(attribute, opts = {}) ⇒ Object

Add a salesforce attribute to the model Possible options : :from => the remote key to fetch to populate the field :type => one of :date, :float, :integer, :boolean, :id



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/salesforce_record/attributes.rb', line 42

def sf_attribute(attribute, opts={})

  case opts[:type]
  when :date
    @salesforce_fields[attribute] = Fields::DateField.new(attribute, opts)
  when :float
    @salesforce_fields[attribute] = Fields::FloatField.new(attribute, opts)
  when :integer
    @salesforce_fields[attribute] = Fields::IntegerField.new(attribute, opts)
  else
    @salesforce_fields[attribute] = Fields::BaseField.new(attribute, opts)
  end

  # Set its accessor
  attr_reader attribute
end

#sf_attributes(*attrs) ⇒ Object

Add salesforce attributes to the model



32
33
34
35
36
# File 'lib/salesforce_record/attributes.rb', line 32

def sf_attributes(*attrs)
  attrs.each do |attr|
    sf_attribute attr
  end
end