Class: FormCore::VirtualModel

Inherits:
ActiveEntity::Base
  • Object
show all
Defined in:
lib/form_core/virtual_model.rb

Constant Summary collapse

ARRAY_WITHOUT_BLANK_PATTERN =

Hack

"!ruby/array:ArrayWithoutBlank"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._embeds_reflectionsObject



71
72
73
# File 'lib/form_core/virtual_model.rb', line 71

def _embeds_reflections
  _reflections.select { |_, v| v.is_a? ::ActiveEntity::Reflection::EmbeddedAssociationReflection }
end

.build(name = nil) ⇒ Object



59
60
61
62
63
# File 'lib/form_core/virtual_model.rb', line 59

def build(name = nil)
  klass = Class.new(self)
  klass.name = name
  klass
end

.coderObject



47
48
49
# File 'lib/form_core/virtual_model.rb', line 47

def coder
  @_coder ||= FormCore.virtual_model_coder_class.new(self)
end

.coder=(klass) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
# File 'lib/form_core/virtual_model.rb', line 51

def coder=(klass)
  raise ArgumentError, "#{klass} should be sub-class of #{Coder}." unless klass && klass < Coder

  @_coder = klass.new(self)
end

.inspectObject

Returns a string like “Post(id:integer, title:string, body:text)”



66
67
68
69
# File 'lib/form_core/virtual_model.rb', line 66

def inspect
  attr_list = attribute_types.map { |name, type| "#{name}: #{type.type}" } * ", "
  "#<VirtualModel:#{name}:#{object_id} #{attr_list}>"
end

.nameObject



36
37
38
# File 'lib/form_core/virtual_model.rb', line 36

def name
  @_name ||= "Form"
end

.name=(value) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
# File 'lib/form_core/virtual_model.rb', line 40

def name=(value)
  value = value.classify
  raise ArgumentError, "`value` isn't a valid class name" if value.blank?

  @_name = value
end

Instance Method Details

#dumpObject



31
32
33
# File 'lib/form_core/virtual_model.rb', line 31

def dump
  self.class.dump(self).gsub(ARRAY_WITHOUT_BLANK_PATTERN, "")
end

#inspectObject

Returns the contents of the record as a nicely formatted string.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/form_core/virtual_model.rb', line 8

def inspect
  # We check defined?(@attributes) not to issue warnings if the object is
  # allocated but not initialized.
  inspection =
    if defined?(@attributes) && @attributes
      self.class.attribute_names.collect do |name|
        "#{name}: #{attribute_for_inspect(name)}" if has_attribute?(name)
      end.compact.join(", ")
    else
      "not initialized"
    end

  "#<VirtualModel:#{self.class.name}:#{object_id} #{inspection}>"
end

#serializable_hash(options = {}) ⇒ Object



23
24
25
26
# File 'lib/form_core/virtual_model.rb', line 23

def serializable_hash(options = {})
  options = (options || {}).reverse_merge include: self.class._embeds_reflections.keys
  super(**options)
end