Class: ActiveRecord::Base

Inherits:
Object show all
Includes:
Persistence
Defined in:
lib/jun/active_record/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

included, #new_record?, #persisted?, #save

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



10
11
12
13
# File 'lib/jun/active_record/base.rb', line 10

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/jun/active_record/base.rb', line 15

def method_missing(name, *args)
  if self.class.connection.columns(self.class.table_name).include?(name)
    @attributes[name]
  else
    super
  end
end

Class Method Details

.allObject



27
28
29
# File 'lib/jun/active_record/base.rb', line 27

def self.all
  ActiveRecord::Relation.new(self)
end

.connectionObject



48
49
50
# File 'lib/jun/active_record/base.rb', line 48

def self.connection
  @@connection ||= SqliteAdapter.new
end

.find(id) ⇒ Object



23
24
25
# File 'lib/jun/active_record/base.rb', line 23

def self.find(id)
  find_by_sql("SELECT * FROM #{table_name} WHERE id = #{id.to_i} LIMIT 1").first
end

.find_by_sql(sql) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/jun/active_record/base.rb', line 35

def self.find_by_sql(sql)
  connection.execute(sql).map do |attributes|
    object = new(attributes)
    object.instance_variable_set("@new_record", false)

    object
  end
end

.table_nameObject



44
45
46
# File 'lib/jun/active_record/base.rb', line 44

def self.table_name
  name.downcase.pluralize
end

.where(*args) ⇒ Object



31
32
33
# File 'lib/jun/active_record/base.rb', line 31

def self.where(*args)
  all.where(*args)
end