Class: Reins::Model::Base

Inherits:
Object
  • Object
show all
Includes:
Associations, Callbacks, Persistence, Validations, Schema
Defined in:
lib/reins/model/base.rb

Constant Summary

Constants included from Callbacks

Callbacks::KINDS

Constants included from Schema

Schema::SKIP_TABLES

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations

included

Methods included from Persistence

#destroy, included, #new_record?, #persisted?, #reload, #save, #save!, #update, #update!

Methods included from Validations

#errors, included, #valid?

Methods included from Callbacks

included, #run_callbacks

Methods included from Schema

current_version, define, dump, dump_string, list_columns, list_tables, record_version, sql_type_to_dsl, table_definition_lines

Constructor Details

#initialize(attrs = {}) ⇒ Base

Returns a new instance of Base.



63
64
65
66
67
68
# File 'lib/reins/model/base.rb', line 63

def initialize(attrs = {})
  @attributes = {}
  @persisted = false
  attrs.each { |k, v| @attributes[k.to_s] = v }
  run_callbacks(:after_initialize)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/reins/model/base.rb', line 91

def method_missing(name, *args)
  attr = name.to_s.chomp("=")
  return super unless self.class.column_names.include?(attr)

  if name.to_s.end_with?("=")
    @attributes[attr] = args[0]
  else
    self.class.cast(attr, @attributes[attr])
  end
end

Class Attribute Details

.table_nameObject



21
22
23
# File 'lib/reins/model/base.rb', line 21

def table_name
  @table_name ||= compute_table_name
end

Class Method Details

.allObject



29
30
31
# File 'lib/reins/model/base.rb', line 29

def all
  Relation.new(self)
end

.countObject



37
# File 'lib/reins/model/base.rb', line 37

def count           = all.count

.firstObject



39
# File 'lib/reins/model/base.rb', line 39

def first           = all.first

.lastObject



40
# File 'lib/reins/model/base.rb', line 40

def last            = all.last

.limit(value) ⇒ Object



35
# File 'lib/reins/model/base.rb', line 35

def limit(value)    = all.limit(value)

.offset(value) ⇒ Object



36
# File 'lib/reins/model/base.rb', line 36

def offset(value)   = all.offset(value)

.orderObject



34
# File 'lib/reins/model/base.rb', line 34

def order(*)    = all.order(*)

.pluck(field) ⇒ Object



38
# File 'lib/reins/model/base.rb', line 38

def pluck(field)    = all.pluck(field)

.primary_keyObject



25
26
27
# File 'lib/reins/model/base.rb', line 25

def primary_key
  "id"
end

.whereObject



33
# File 'lib/reins/model/base.rb', line 33

def where(*, &) = all.where(*, &)

Instance Method Details

#==(other) ⇒ Object



82
83
84
# File 'lib/reins/model/base.rb', line 82

def ==(other)
  other.is_a?(self.class) && id && id == other.id
end

#[](name) ⇒ Object



70
71
72
# File 'lib/reins/model/base.rb', line 70

def [](name)
  @attributes[name.to_s]
end

#[]=(name, value) ⇒ Object



74
75
76
# File 'lib/reins/model/base.rb', line 74

def []=(name, value)
  @attributes[name.to_s] = value
end

#idObject



78
79
80
# File 'lib/reins/model/base.rb', line 78

def id
  @attributes[self.class.primary_key]
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/reins/model/base.rb', line 86

def respond_to_missing?(name, include_private = false)
  attr = name.to_s.chomp("=")
  self.class.column_names.include?(attr) || super
end