Module: SfCli::Sf::Model::BaseMethods

Defined in:
lib/sf_cli/sf/model/base_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(c) ⇒ Object



6
7
8
# File 'lib/sf_cli/sf/model/base_methods.rb', line 6

def self.included(c)
  c.extend ClassMethods
end

Instance Method Details

#initialize(attributes = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sf_cli/sf/model/base_methods.rb', line 24

def initialize(attributes = {})
  @original_attributes = {}
  @current_attributes = {}
  @updated_attributes = {}

  attributes.each do |k, v|
    field_name = k.to_sym
    if self.class.field_names.include?(field_name)
      @original_attributes[field_name] = v
      __send__ (field_name.to_s + '='), v
    elsif self.class.parent_relations.find{|r| r[:name] == field_name}
      __send__ (field_name.to_s + '='), v
    elsif self.class.children_relations.find{|r| r[:name] == field_name}
      __send__ (field_name.to_s + '='), (v.nil? ? [] : v)
    end
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/sf_cli/sf/model/base_methods.rb', line 52

def new_record?
  self.Id.nil?
end

#persisted?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/sf_cli/sf/model/base_methods.rb', line 56

def persisted?
  new_record? == false
end

#to_h(keys: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/sf_cli/sf/model/base_methods.rb', line 42

def to_h(keys: nil)
  self.class.field_names.each_with_object({}) do |name, hash|
    if keys&.instance_of?(Array)
      hash[name] = __send__(name) if keys.include?(name)
    else
      hash[name] = __send__(name)
    end
  end
end