Class: SfCli::Sf::Model::ClassDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/sf_cli/sf/model/class_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ ClassDefinition

Returns a new instance of ClassDefinition.



12
13
14
# File 'lib/sf_cli/sf/model/class_definition.rb', line 12

def initialize(schema)
  @schema = schema
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



10
11
12
# File 'lib/sf_cli/sf/model/class_definition.rb', line 10

def schema
  @schema
end

Instance Method Details

#children_relation_methodsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sf_cli/sf/model/class_definition.rb', line 88

def children_relation_methods
  schema.children_relations.each_with_object('') do |r, s|
    s << <<~EOS
      def #{r[:name]}
        @#{r[:name]}
      end

      def #{r[:name]}=(records)
        @#{r[:name]} = records.map{|attributes| #{r[:class_name]}.new(attributes)}
      end
    EOS
  end
end

#class_methodsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sf_cli/sf/model/class_definition.rb', line 34

def class_methods
  <<~EOS
    class << self
      def field_names
        @field_names ||= #{ schema.field_names }
      end

      def parent_relations
        @parent_relations ||= #{ schema.parent_relations }
      end

      def children_relations
        @children_relations ||= #{ schema.children_relations }
      end
    end
  EOS
end

#field_attribute_methodsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sf_cli/sf/model/class_definition.rb', line 52

def field_attribute_methods
  schema.field_names.each_with_object('') do |name, s|
    s << <<~EOS
      def #{name}
        @#{name}
      end

      def #{name}=(value)
        @#{name} = value
        return if %i[Id LastModifiedDate IsDeleted SystemModstamp CreatedById CreatedDate LastModifiedById].include?(:#{name})

        current_attributes[:#{name}] = value
        if current_attributes[:#{name}] == original_attributes[:#{name}]
          updated_attributes[:#{name}] = nil
        else
          updated_attributes[:#{name}] = value
        end
      end
    EOS
  end
end

#parent_relation_methodsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sf_cli/sf/model/class_definition.rb', line 74

def parent_relation_methods
  schema.parent_relations.each_with_object('') do |r, s|
    s << <<~EOS
      def #{r[:name]}
        @#{r[:name]}
      end

      def #{r[:name]}=(attributes)
        @#{r[:name]} = attributes.nil? ? nil : #{r[:class_name]}.new(attributes)
      end
    EOS
  end
end

#to_sObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sf_cli/sf/model/class_definition.rb', line 16

def to_s
  <<~Klass
    Class.new do
      include ::SfCli::Sf::Model::BaseMethods
      include ::SfCli::Sf::Model::DmlMethods
      include ::SfCli::Sf::Model::QueryMethods

      attr_reader :original_attributes, :current_attributes, :updated_attributes

      #{ class_methods }

      #{ field_attribute_methods  }
      #{ parent_relation_methods }
      #{ children_relation_methods }
    end
  Klass
end