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 << "      def \#{r[:name]}\n        @\#{r[:name]}\n      end\n\n      def \#{r[:name]}=(records)\n        @\#{r[:name]} = records.map{|attributes| \#{r[:class_name]}.new(attributes)}\n      end\n    EOS\n  end\nend\n"

#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
  "    class << self\n      def field_names\n        @field_names ||= \#{ schema.field_names }\n      end\n\n      def parent_relations\n        @parent_relations ||= \#{ schema.parent_relations }\n      end\n\n      def children_relations\n        @children_relations ||= \#{ schema.children_relations }\n      end\n    end\n  EOS\nend\n"

#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 << "      def \#{name}\n        @\#{name}\n      end\n\n      def \#{name}=(value)\n        @\#{name} = value\n        return if %i[Id LastModifiedDate IsDeleted SystemModstamp CreatedById CreatedDate LastModifiedById].include?(:\#{name})\n\n        current_attributes[:\#{name}] = value\n        if current_attributes[:\#{name}] == original_attributes[:\#{name}]\n          updated_attributes[:\#{name}] = nil\n        else\n          updated_attributes[:\#{name}] = value\n        end\n      end\n    EOS\n  end\nend\n"

#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 << "      def \#{r[:name]}\n        @\#{r[:name]}\n      end\n\n      def \#{r[:name]}=(attributes)\n        @\#{r[:name]} = attributes.nil? ? nil : \#{r[:class_name]}.new(attributes)\n      end\n    EOS\n  end\nend\n"

#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
  "    Class.new do\n      include ::SfCli::Sf::Model::BaseMethods\n      include ::SfCli::Sf::Model::DmlMethods\n      include ::SfCli::Sf::Model::QueryMethods\n\n      attr_reader :original_attributes, :current_attributes, :updated_attributes\n\n      \#{ class_methods }\n\n      \#{ field_attribute_methods  }\n      \#{ parent_relation_methods }\n      \#{ children_relation_methods }\n    end\n  Klass\nend\n"