Class: Dupe::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/superdupe/model.rb,
lib/superdupe/schema.rb,
lib/superdupe/attribute_template.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Schema

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Model

Returns a new instance of Model.



7
8
9
10
11
# File 'lib/superdupe/model.rb', line 7

def initialize(name)
  @schema = Dupe::Model::Schema.new
  @name   = name.to_sym
  @id_sequence = Sequence.new
end

Instance Attribute Details

#id_sequenceObject (readonly)

Returns the value of attribute id_sequence.



5
6
7
# File 'lib/superdupe/model.rb', line 5

def id_sequence
  @id_sequence
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/superdupe/model.rb', line 4

def name
  @name
end

#schemaObject (readonly)

Returns the value of attribute schema.



3
4
5
# File 'lib/superdupe/model.rb', line 3

def schema
  @schema
end

Instance Method Details

#create(attributes = {}) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/superdupe/model.rb', line 17

def create(attributes={})
  Database::Record.new.tap do |record|
    record.__model__ = self
    record.id = @id_sequence.next
    record.merge! default_record
    record.merge! transform(attributes)
  end
end

#define(definition_proc) ⇒ Object



13
14
15
# File 'lib/superdupe/model.rb', line 13

def define(definition_proc)
  definition_proc.call @schema
end

#run_after_create_callbacks(record) ⇒ Object

called by the Dupe::Database#insert method



27
28
29
30
31
# File 'lib/superdupe/model.rb', line 27

def run_after_create_callbacks(record)
  @schema.after_create_callbacks.each do |callback|
    callback.call record
  end
end