Class: LessActiveRecord

Inherits:
Object
  • Object
show all
Extended by:
RecordFinders
Defined in:
lib/less_active_record.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RecordFinders

all, find, where

Constructor Details

#initialize(attributes = {}) ⇒ LessActiveRecord

Returns a new instance of LessActiveRecord.



55
56
57
# File 'lib/less_active_record.rb', line 55

def initialize(attributes = {})
  self.attributes = attributes
end

Class Attribute Details

.attribute_namesObject



28
29
30
# File 'lib/less_active_record.rb', line 28

def attribute_names
  (@attribute_names ||= []).clone
end

.validationsObject



41
42
43
# File 'lib/less_active_record.rb', line 41

def validations
  (@validations ||= []).clone
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/less_active_record.rb', line 7

def id
  @id
end

Class Method Details

.attribute(name) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/less_active_record.rb', line 18

def attribute(name)
  symbolized_name = name.to_sym
  unless attribute_names.include? symbolized_name
    attr_accessor symbolized_name
    self.attribute_names <<= symbolized_name
  end

  self
end

.create(attributes = {}) ⇒ Object



10
11
12
# File 'lib/less_active_record.rb', line 10

def create(attributes = {})
  new(attributes).tap(&:save)
end

.storage_nameObject



14
15
16
# File 'lib/less_active_record.rb', line 14

def storage_name
  "#{ self.to_s }Table"
end

.validate(method_name) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/less_active_record.rb', line 32

def validate(method_name)
  symbolized_name = method_name.to_sym
  unless validations.include? symbolized_name
    self.validations <<= symbolized_name
  end

  self
end

Instance Method Details

#==(other) ⇒ Object



114
115
116
# File 'lib/less_active_record.rb', line 114

def ==(other)
  id == other.id
end

#attributesObject



80
81
82
83
84
# File 'lib/less_active_record.rb', line 80

def attributes
  self.class.attribute_names.each_with_object({}) do |name, attributes|
    attributes[name] = send(name)
  end
end

#attributes=(attributes) ⇒ Object



86
87
88
89
90
# File 'lib/less_active_record.rb', line 86

def attributes=(attributes)
  self.class.attribute_names.each do |name|
    send("#{ name }=", attributes[name]) if attributes.key?(name)
  end
end

#cloneObject



110
111
112
# File 'lib/less_active_record.rb', line 110

def clone
  self.class.new(attributes).tap { |clone| clone.id = id }
end

#destroyObject



76
77
78
# File 'lib/less_active_record.rb', line 76

def destroy
  _adapter.destroy(id)
end

#new_record?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/less_active_record.rb', line 106

def new_record?
  id.nil?
end

#persisted?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/less_active_record.rb', line 102

def persisted?
  not new_record?
end

#saveObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/less_active_record.rb', line 59

def save
  return false unless valid?

  if new_record?
    @id = _adapter.insert(attributes)

    true
  else
    _adapter.update(id, attributes)
  end
end

#update(attributes = {}) ⇒ Object



71
72
73
74
# File 'lib/less_active_record.rb', line 71

def update(attributes = {})
  self.attributes = attributes
  save
end

#valid?Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
# File 'lib/less_active_record.rb', line 92

def valid?
  self.class.validations.each do |validation|
    return false unless send(validation)
  end

  true
rescue
  false
end