Class: Tramp::Base

Inherits:
Object
  • Object
show all
Extended by:
Finders
Includes:
ActiveModel::Dirty, ActiveModel::Validations, AttributeMethods, Callbacks
Defined in:
lib/tramp/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Finders

[], arel_table

Methods included from AttributeMethods

#attributes=, #read_attribute, #write_attribute

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



32
33
34
35
36
# File 'lib/tramp/base.rb', line 32

def initialize(attributes = {})
  @new_record = true
  @attributes = {}.with_indifferent_access
  self.attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



30
31
32
# File 'lib/tramp/base.rb', line 30

def attributes
  @attributes
end

Class Method Details

.column_namesObject



15
16
17
# File 'lib/tramp/base.rb', line 15

def column_names
  columns.map(&:name)
end

.columnsObject



11
12
13
# File 'lib/tramp/base.rb', line 11

def columns
  @columns ||= arel_table.columns
end

.instantiate(record) ⇒ Object



23
24
25
26
27
# File 'lib/tramp/base.rb', line 23

def instantiate(record)
  object = allocate
  object.instance_variable_set("@attributes", record.with_indifferent_access)
  object
end

.primary_keyObject



19
20
21
# File 'lib/tramp/base.rb', line 19

def primary_key
  @primary_key ||= model_attributes.detect {|k, v| v.primary_key? }[0]
end

Instance Method Details

#destroy(callback = nil, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/tramp/base.rb', line 52

def destroy(callback = nil, &block)
  callback ||= block
  
  relation.delete do
    status = Status.new(self, true)
    after_destroy_callbacks status
    callback.arity == 1 ? callback.call(status) : callback.call if callback
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/tramp/base.rb', line 38

def new_record?
  @new_record
end

#save(callback = nil, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/tramp/base.rb', line 42

def save(callback = nil, &block)
  callback ||= block

  if valid?
    new_record? ? create_record(callback) : update_record(callback)
  else
    callback.arity == 1 ? callback.call(Status.new(self, false)) : callback.call if callback
  end
end