Module: SalesforceRecord::Attributes::ClassMethods
- Defined in:
- lib/salesforce_record/attributes.rb
Instance Method Summary collapse
-
#encode_salesforce_field(name, value) ⇒ Object
Encode a field from salesforce.
-
#encode_salesforce_fields(fields) ⇒ Object
Encode fields for salesforce.
- #field_remote_name(name) ⇒ Object
- #field_type(name) ⇒ Object
-
#find_matching_alias(nested_hash) ⇒ Object
Tries to find a salesforce alias matching a nested hash.
-
#from_salesforce(fields) ⇒ Object
Create an imported record from salesforce.
- #get_field(name) ⇒ Object
- #has_field?(name) ⇒ Boolean
-
#parse_salesforce_field(name, value) ⇒ Object
Parse a field coming from salesforce (type).
-
#parse_salesforce_fields(sf_fields) ⇒ Object
Parse fields coming from salesforce.
-
#salesforce_attributes ⇒ Object
The attributes for this model.
-
#salesforce_fields ⇒ Object
The salesforce fields and options.
-
#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.
-
#sf_attributes(*attrs) ⇒ Object
Add salesforce attributes to the model.
Instance Method Details
#encode_salesforce_field(name, value) ⇒ Object
Encode a field from salesforce
98 99 100 101 102 103 |
# File 'lib/salesforce_record/attributes.rb', line 98 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
117 118 119 |
# File 'lib/salesforce_record/attributes.rb', line 117 def field_remote_name(name) has_field?(name) && self.salesforce_fields[name].remote_name end |
#field_type(name) ⇒ Object
113 114 115 |
# File 'lib/salesforce_record/attributes.rb', line 113 def field_type(name) has_field?(name) && self.salesforce_fields[name].type end |
#find_matching_alias(nested_hash) ⇒ Object
Tries to find a salesforce alias matching a nested hash. Returns the alias name and corresponding value if found
123 124 125 126 127 128 129 130 |
# File 'lib/salesforce_record/attributes.rb', line 123 def find_matching_alias(nested_hash) self.salesforce_fields.each do |name, field| if (value = field.is_alias_of(nested_hash)) return parse_salesforce_field(name, value) end end return nil 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
105 106 107 |
# File 'lib/salesforce_record/attributes.rb', line 105 def get_field(name) self.salesforce_fields[name] end |
#has_field?(name) ⇒ Boolean
109 110 111 |
# File 'lib/salesforce_record/attributes.rb', line 109 def has_field?(name) !!get_field(name) end |
#parse_salesforce_field(name, value) ⇒ Object
Parse a field coming from salesforce (type)
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/salesforce_record/attributes.rb', line 86 def parse_salesforce_field(name, value) # If existing, parse it depending on type if (field = get_field name) { field.local_name => field.parse(value) } # Else, try to find if it is an alias else find_matching_alias(name => value) end end |
#parse_salesforce_fields(sf_fields) ⇒ 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_fields) {}.tap do |parsed_fields| sf_fields.each do |name, value| parsed_field = parse_salesforce_field(name, value) unless value.nil? parsed_fields.merge! parsed_field if !parsed_field.nil? end end end |
#salesforce_attributes ⇒ Object
The attributes for this model
29 |
# File 'lib/salesforce_record/attributes.rb', line 29 def salesforce_attributes ; @salesforce_fields.keys ; end |
#salesforce_fields ⇒ Object
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 |