Class: FormCore::VirtualModel

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(name = nil) ⇒ Object



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

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

.coderObject



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

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

.coder=(klass) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/form_core/virtual_model.rb', line 56

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

  @_coder = klass.new(self)
end

.inspectObject

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



73
74
75
76
# File 'lib/form_core/virtual_model.rb', line 73

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

.nameObject



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

def name
  @_name ||= "Form"
end

.name=(value) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

def dump
  self.class.dump(self)
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
22
23
# 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|
        if has_attribute?(name)
          "#{name}: #{attribute_for_inspect(name)}"
        end
      end.compact.join(", ")
    else
      "not initialized"
    end

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

#serializable_hash(options = {}) ⇒ Object



25
26
27
28
# File 'lib/form_core/virtual_model.rb', line 25

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