Module: Foobara::ModelAttributeHelpers::Concerns::AttributeHelpers::ClassMethods

Defined in:
foobara-0.0.110/projects/model_attribute_helpers/src/attribute_helpers.rb

Instance Method Summary collapse

Instance Method Details

#foobara_attributes_for_aggregate_update(require_primary_key: true, initial: true) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'foobara-0.0.110/projects/model_attribute_helpers/src/attribute_helpers.rb', line 64

def foobara_attributes_for_aggregate_update(require_primary_key: true, initial: true)
  declaration = foobara_attributes_type.declaration_data
  declaration = Util.deep_dup(declaration)

  declaration.delete(:defaults)
  declaration.delete(:required)

  if initial && foobara_has_primary_key?
    if require_primary_key
      declaration[:required] = [foobara_primary_key_attribute]
    else
      declaration = TypeDeclarations::Attributes.reject(declaration, foobara_primary_key_attribute)
    end
  end

  foobara_associations.each_pair do |data_path, type|
    if type.extends?(BuiltinTypes[:entity])
      target_class = type.target_class

      entry = foobara_type_declaration_value_at(declaration, DataPath.new(data_path).path)
      entry.clear
      entry.merge!(target_class.foobara_attributes_for_aggregate_update(initial: false))
    end
  end

  declaration
end

#foobara_attributes_for_atom_update(require_primary_key: true) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'foobara-0.0.110/projects/model_attribute_helpers/src/attribute_helpers.rb', line 92

def foobara_attributes_for_atom_update(require_primary_key: true)
  declaration = foobara_attributes_type.declaration_data
  declaration = Util.deep_dup(declaration)

  declaration.delete(:defaults)

  if foobara_has_primary_key?
    if require_primary_key
      declaration[:required] = [foobara_primary_key_attribute]
    else
      declaration = TypeDeclarations::Attributes.reject(declaration, foobara_primary_key_attribute)
    end
  end

  # expect all associations to be expressed as primary key values
  # TODO: should we have a special type for encapsulating primary keys types??
  foobara_associations.each_pair do |data_path, type|
    if type.extends?(BuiltinTypes[:entity])
      target_class = type.target_class
      # TODO: do we really need declaration_data? Why cant we use the type directly?
      # TODO: make this work with the type directly for performance reasons.
      primary_key_type_declaration = target_class.foobara_primary_key_type.declaration_data
      entry = foobara_type_declaration_value_at(declaration, DataPath.new(data_path).path)
      entry.clear
      entry.merge!(primary_key_type_declaration)
    end
  end

  declaration
end

#foobara_attributes_for_create(includes_primary_key: false, include_private: true, include_delegates: false) ⇒ Object

TODO: we should have metadata on the entity about whether it required a primary key upon creation or not instead of an option here.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'foobara-0.0.110/projects/model_attribute_helpers/src/attribute_helpers.rb', line 34

def foobara_attributes_for_create(
  includes_primary_key: false, # usually the underlying data store creates this
  include_private: true, # we usually need to initialize these values to something but not always
  include_delegates: false # usually these are already set on the passed-in objects it delegates to
)
  if includes_primary_key && include_private
    if include_delegates || delegates.empty?
      return foobara_attributes_type
    end
  end

  declaration = foobara_attributes_type.declaration_data

  Namespace.use foobara_attributes_type.created_in_namespace do
    unless includes_primary_key
      declaration = Foobara::TypeDeclarations::Attributes.reject(declaration, foobara_primary_key_attribute)
    end

    unless include_private
      declaration = Foobara::TypeDeclarations::Attributes.reject(declaration, *private_attribute_names)
    end

    unless include_delegates
      declaration = Foobara::TypeDeclarations::Attributes.reject(declaration, *delegates.keys)
    end

    Domain.current.foobara_type_from_declaration(declaration)
  end
end

#foobara_attributes_for_find_byObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'foobara-0.0.110/projects/model_attribute_helpers/src/attribute_helpers.rb', line 123

def foobara_attributes_for_find_by
  element_type_declarations = {}

  foobara_attributes_type.element_types.each_pair do |attribute_name, attribute_type|
    element_type_declarations[attribute_name] = attribute_type.reference_or_declaration_data
  end

  handler = Domain.global.foobara_type_builder.handler_for_class(
    TypeDeclarations::Handlers::ExtendAttributesTypeDeclaration
  )

  handler.desugarize(
    type: "::attributes",
    element_type_declarations:
  )
end

#foobara_attributes_for_update(require_primary_key: true) ⇒ Object



28
29
30
# File 'foobara-0.0.110/projects/model_attribute_helpers/src/attribute_helpers.rb', line 28

def foobara_attributes_for_update(require_primary_key: true)
  foobara_attributes_for_aggregate_update(require_primary_key:)
end

#foobara_has_primary_key?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'foobara-0.0.110/projects/model_attribute_helpers/src/attribute_helpers.rb', line 24

def foobara_has_primary_key?
  respond_to?(:foobara_primary_key_attribute)
end