Class: Yamori::ClassDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/yamori/class_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ ClassDefinition

Returns a new instance of ClassDefinition.



10
11
12
# File 'lib/yamori/class_definition.rb', line 10

def initialize(schema)
  @schema = Schema.new(schema)
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



8
9
10
# File 'lib/yamori/class_definition.rb', line 8

def schema
  @schema
end

Instance Method Details

#child_relation_methodsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/yamori/class_definition.rb', line 86

def child_relation_methods
  schema.child_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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/yamori/class_definition.rb', line 32

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 child_relations
        @child_relations ||= #{ schema.child_relations }
      end
    end
  EOS
end

#field_attribute_methodsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/yamori/class_definition.rb', line 50

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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/yamori/class_definition.rb', line 72

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/yamori/class_definition.rb', line 14

def to_s
  <<~Klass
    Class.new do
      include ::Yamori::BaseMethods
      include ::Yamori::DmlMethods
      include ::Yamori::QueryMethods

      attr_reader :original_attributes, :current_attributes, :updated_attributes

      #{ class_methods }

      #{ field_attribute_methods }
      #{ parent_relation_methods }
      #{ child_relation_methods }
    end
  Klass
end