Module: Opto::Model

Defined in:
lib/opto/model.rb,
lib/opto/model/version.rb,
lib/opto/model/attribute.rb,
lib/opto/model/association.rb,
lib/opto/model/initializer.rb,
lib/opto/model/attribute_collection.rb,
lib/opto/model/attribute_declaration.rb,
lib/opto/model/association/has_one/proxy.rb,
lib/opto/model/association/has_many/proxy.rb,
lib/opto/model/association/has_one/declaration.rb,
lib/opto/model/association/has_many/declaration.rb

Defined Under Namespace

Modules: Association, AttributeDeclaration, Initializer Classes: Attribute, AttributeCollection

Constant Summary collapse

VERSION =
"0.2.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(where) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/opto/model.rb', line 10

def self.included(where)
  where.send :prepend, Initializer
  where.send :prepend, AttributeDeclaration
  where.send :prepend, Association
  where.send :prepend, Association::HasOne::Declaration
  where.send :prepend, Association::HasMany::Declaration
end

Instance Method Details

#errors(children = true) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/opto/model.rb', line 30

def errors(children = true)
  if children
    result = collection.errors
    self.class.relations.each do |relation|
      result.merge!(self.send(relation).errors)
    end
    result
  else
    collection.errors
  end
end

#inspectObject



18
19
20
# File 'lib/opto/model.rb', line 18

def inspect
  super.gsub(/\>\z/, " " + collection.members.map {|n, m| "#{m.handler.type}:#{n}: #{m.value.inspect}"}.join(', ') + ">")
end

#to_h(children = true) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/opto/model.rb', line 42

def to_h(children = true)
  if children
    result = to_h(false)
    self.class.relations.each do |r|
      result.merge!(self.send(r).to_h)
    end
    result
  else
    collection.to_h(values_only: true)
  end
end

#valid?(children = true) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/opto/model.rb', line 22

def valid?(children = true)
  if children
    ([collection] + self.class.relations.map { |r| self.send(r) }).all?(&:valid?)
  else
    collection.valid?
  end
end