Class: Salesforce::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/salesforce/column.rb

Constant Summary collapse

SUPPORTED_DATE_RANGE =
Date.parse("1700-01-01")..Date.parse("4000-12-31")
SUPPORTED_TIME_RANGE =
Time.parse("1902-01-01 00:00:00 UTC")..Time.parse("2037-12-31 00:00:00 UTC")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ Column

Returns a new instance of Column.



8
9
10
11
12
13
14
# File 'lib/salesforce/column.rb', line 8

def initialize(field)
  self.original_name = field["name"]
  self.name          = field["name"].gsub(/\_\_c$/, '').underscore
  self.type          = field["type"].to_sym
  self.createable    = field['createable']
  self.updateable    = field["updateable"]
end

Instance Attribute Details

#createableObject

Returns the value of attribute createable.



3
4
5
# File 'lib/salesforce/column.rb', line 3

def createable
  @createable
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/salesforce/column.rb', line 3

def name
  @name
end

#original_nameObject

Returns the value of attribute original_name.



3
4
5
# File 'lib/salesforce/column.rb', line 3

def original_name
  @original_name
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/salesforce/column.rb', line 3

def type
  @type
end

#updateableObject

Returns the value of attribute updateable.



3
4
5
# File 'lib/salesforce/column.rb', line 3

def updateable
  @updateable
end

Class Method Details

.to_csv_value(obj) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/salesforce/column.rb', line 50

def self.to_csv_value(obj)
  case (obj)
    when Date;
      obj.strftime("%Y-%m-%d")
    when TrueClass;
      'TRUE'
    when FalseClass;
      'FALSE'
    when Time;
      obj.xmlschema
    when NilClass;
      '#N/A'
    else
      "#{obj.to_s}"
  end
end

.to_soql_value(obj) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/salesforce/column.rb', line 29

def self.to_soql_value(obj)
  case (obj)
    when Date
      obj.strftime("%Y-%m-%d")
    when TrueClass
      'TRUE'
    when FalseClass
      'FALSE'
    when Time
      obj.xmlschema
    when nil
      'NULL'
    when Numeric
      "#{obj.to_s}"
    when Array
      "(#{obj.map { |sobj| to_soql_value(sobj) }.join(',')})"
    else
      "'#{obj.to_s}'"
  end
end

.typecast(type, value) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/salesforce/column.rb', line 67

def self.typecast(type, value)
  case (type)
    when :id, :reference
      if Config.use_full_length_ids?
        value
      else
        value.to_s.size == 15 ? value : value.to_s[0..14]
      end
    when :date
      begin
        parsed_date = Date.parse(value)
        if SUPPORTED_DATE_RANGE.cover?(parsed_date)
          parsed_date
        else
          nil
        end
      rescue ArgumentError
        nil
      rescue
        value if value.is_a?(Date)
      end
    when :datetime
      begin
        parsed_time = Time.parse(value)
        if SUPPORTED_TIME_RANGE.cover?(parsed_time)
          parsed_time
        else
          nil
        end
      rescue ArgumentError
        Time.now
      rescue
        value if value.is_a?(Time)
      end
    when :double
      begin
        value.to_s.to_d
      rescue
        value if value.is_a?(Numeric)
      end
    else
      value
  end
end

Instance Method Details

#==(other) ⇒ Object



112
113
114
115
# File 'lib/salesforce/column.rb', line 112

def ==(other)
  return false unless other
  self.name == other.name && self.original_name == other.original_name
end

#createable?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/salesforce/column.rb', line 16

def createable?
  createable
end

#editable?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/salesforce/column.rb', line 24

def editable?
  createable? || updateable?
end

#updateable?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/salesforce/column.rb', line 20

def updateable?
  updateable
end