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



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

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

.coderObject



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

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

.coder=(klass) ⇒ Object



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

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)”



63
64
65
66
# File 'lib/form_core/virtual_model.rb', line 63

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

.nameObject



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

def name
  @_name ||= "Form"
end

.name=(value) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

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