Class: ROM::Factory::TupleEvaluator Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/factory/tuple_evaluator.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, relation, traits = {}) ⇒ TupleEvaluator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of TupleEvaluator.



25
26
27
28
29
30
31
# File 'lib/rom/factory/tuple_evaluator.rb', line 25

def initialize(attributes, relation, traits = {})
  @attributes = attributes
  @relation = relation.with(auto_struct: true)
  @traits = traits
  @model = @relation.combine(*assoc_names).mapper.model
  @sequence = Sequences[relation]
end

Instance Attribute Details

#attributesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
# File 'lib/rom/factory/tuple_evaluator.rb', line 10

def attributes
  @attributes
end

#modelObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/rom/factory/tuple_evaluator.rb', line 19

def model
  @model
end

#relationObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
# File 'lib/rom/factory/tuple_evaluator.rb', line 13

def relation
  @relation
end

#sequenceObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
# File 'lib/rom/factory/tuple_evaluator.rb', line 22

def sequence
  @sequence
end

#traitsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/rom/factory/tuple_evaluator.rb', line 16

def traits
  @traits
end

Instance Method Details

#assoc_names(traits = []) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
# File 'lib/rom/factory/tuple_evaluator.rb', line 74

def assoc_names(traits = [])
  assocs(traits).map(&:name)
end

#assocs(traits_names = []) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



78
79
80
81
82
83
# File 'lib/rom/factory/tuple_evaluator.rb', line 78

def assocs(traits_names = [])
  traits
    .values_at(*traits_names)
    .map(&:associations).flat_map(&:elements)
    .inject(AttributeRegistry.new(attributes.associations.elements), :<<)
end

#defaults(traits, attrs, opts = EMPTY_HASH) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
# File 'lib/rom/factory/tuple_evaluator.rb', line 34

def defaults(traits, attrs, opts = EMPTY_HASH)
  evaluate(traits, attrs, opts).merge(attrs)
end

#extract_tuple(args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



96
97
98
99
100
101
102
103
104
# File 'lib/rom/factory/tuple_evaluator.rb', line 96

def extract_tuple(args)
  if !args.empty? && args.last.is_a?(::Hash)
    *traits, attrs = args

    [traits, attrs]
  else
    [args, EMPTY_HASH]
  end
end

#has_associations?(traits = []) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


86
87
88
# File 'lib/rom/factory/tuple_evaluator.rb', line 86

def has_associations?(traits = [])
  !assoc_names(traits).empty?
end

#persist_associations(tuple, parent, traits = []) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



66
67
68
69
70
71
# File 'lib/rom/factory/tuple_evaluator.rb', line 66

def persist_associations(tuple, parent, traits = [])
  assoc_names(traits).each do |name|
    assoc = tuple[name]
    assoc.call(parent, persist: true) if assoc.is_a?(Proc)
  end
end

#primary_keyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



91
92
93
# File 'lib/rom/factory/tuple_evaluator.rb', line 91

def primary_key
  relation.primary_key
end

#struct(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rom/factory/tuple_evaluator.rb', line 39

def struct(*args)
  traits, attrs = extract_tuple(args)
  merged_attrs = struct_attrs.merge(defaults(traits, attrs, persist: false))
  is_callable = proc { |_name, value| value.respond_to?(:call) }

  callables = merged_attrs.select(&is_callable)
  attributes = merged_attrs.reject(&is_callable)

  materialized_callables = {}
  callables.each do |_name, callable|
    materialized_callables.merge!(callable.call(attributes, persist: false))
  end

  attributes.merge!(materialized_callables)

  associations = assoc_names
    .map { |key| [key, attributes[key]] if attributes.key?(key) }
    .compact
    .to_h

  attributes = relation.output_schema[attributes]
  attributes.update(associations)

  model.new(attributes)
end