Module: Foobara::Entity::Concerns::Initialization::ClassMethods

Defined in:
foobara-0.0.110/projects/entity/src/concerns/initialization.rb

Instance Method Summary collapse

Instance Method Details

#build(attributes) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'foobara-0.0.110/projects/entity/src/concerns/initialization.rb', line 14

def build(attributes)
  record = __private_new__
  record.build(attributes)
  # TODO: rename to something like "detached"
  record.is_built = true

  record.fire(:initialized)
  record.fire(:initialized_built)

  record
end

#can_be_created_through_casting?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'foobara-0.0.110/projects/entity/src/concerns/initialization.rb', line 10

def can_be_created_through_casting?
  true
end

#create(attributes = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'foobara-0.0.110/projects/entity/src/concerns/initialization.rb', line 80

def create(attributes = {})
  record = __private_new__
  record.is_created = true

  defaults = attributes_type.declaration_data[:defaults]
  if defaults && !defaults.empty?
    record.write_attributes_without_callbacks(defaults)
  end

  record.write_attributes_without_callbacks(attributes)

  # TODO: delete :initialized if unused
  record.fire(:initialized)
  record.fire(:initialized_created)

  record
end

#loaded(attributes) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'foobara-0.0.110/projects/entity/src/concerns/initialization.rb', line 51

def loaded(attributes)
  attributes = attributes_type.process_value!(attributes)

  record_id = attributes[primary_key_attribute]

  record = current_transaction_table.find_tracked(record_id)

  if record
    # :nocov:
    raise "Already loaded for #{attributes}. Bug maybe?"
    # :nocov:
  end

  record = __private_new__

  record.successfully_loaded(attributes)

  unless record.primary_key
    # :nocov:
    raise "Expected primary key #{primary_key_attribute} to be present!"
    # :nocov:
  end

  record.fire(:initialized)
  record.fire(:initialized_loaded)

  record
end

#thunk(record_id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'foobara-0.0.110/projects/entity/src/concerns/initialization.rb', line 26

def thunk(record_id)
  record_id = primary_key_type.process_value!(record_id)

  # TODO: is this possible?
  if record_id.nil?
    # :nocov:
    raise ArgumentError, "Primary key cannot be blank"
    # :nocov:
  end

  # check if tracked already...
  record = current_transaction_table.find_tracked(record_id)

  return record if record

  record = __private_new__
  record.is_persisted = true
  record.write_attributes_without_callbacks(primary_key_attribute => record_id)

  record.fire(:initialized)
  record.fire(:initialized_thunk)

  record
end