lib/rollerskates/base_model.rb in rollerskates-0.1.0 vs lib/rollerskates/base_model.rb in rollerskates-0.1.1
- old
+ new
@@ -1,116 +1,63 @@
-require "rollerskates/helpers/model_helper"
+require "rollerskates/orm/helpers/database_table_helper"
+require "rollerskates/orm/associable"
+require "rollerskates/orm/query_builder"
module Rollerskates
- class BaseModel < Rollerskates::ModelHelper
+ class BaseModel
+ extend Rollerskates::Associable
+ extend Rollerskates::DatabaseTableHelper
+
def initialize(values = {})
hash_to_properties(values) unless values.empty?
end
- def hash_to_properties(hash)
- hash.each do |column, value|
- instance_variable_set("@#{column}", value)
- end
- end
-
- def update(hash)
- hash_to_properties(hash)
- @updated_at = Time.now.to_s
- end
-
def save
- create_columns_placeholders_values
- if id
- database.execute(update_query, values_for_update)
- else
- add_created_at_and_updated_at
- database.execute "INSERT INTO #{table_name} (#{@columns.join(', ')})\
- VALUES (#{@placeholders.join(', ')})", @values
- end
+ self.class.query.build(to_hash).save
end
- def placeholders_for_update
- placeholders = @columns.map { |col| col + " = ?" }
- placeholders.join(", ")
+ def self.find(value)
+ query.where({ id: value }, true)
end
- def values_for_update
- @values << id
+ def self.find_by(find_conditions)
+ query.where(find_conditions, true)
end
- def create_columns_placeholders_values
- @model = self
- @columns = []
- @placeholders = []
- @values = []
-
- all_columns.each do |column|
- value = @model.send(column)
- next unless value
- @columns << column.to_s
- @placeholders << "?"
- @values << value
- end
+ def self.last(number = nil)
+ query.order("id DESC").first(number)
end
- def add_created_at_and_updated_at
- @columns << %w(created_at updated_at)
- @placeholders << ["?", "?"]
- @values << [Time.now.to_s, Time.now.to_s]
+ def self.create(create_parameters)
+ query.build(create_parameters).save
end
- def self.create(values)
- new(values).save
- end
-
def self.all
- data = database.execute "SELECT #{all_columns.join(', ')}\
- FROM #{table_name}"
- data.map do |row|
- row_to_object(row)
- end
+ query
end
- def self.count
- data = database.execute "SELECT COUNT(*) FROM #{table_name}"
- data.flatten.first
+ def self.query
+ Rollerskates::QueryBuilder.new self
end
- def self.first
- data = database.execute "SELECT #{all_columns.join(', ')} \
- FROM #{table_name} ORDER BY id ASC LIMIT 1"
- row_to_object(data.flatten)
+ def self.method_missing(method, *args, &block)
+ query.send(method, *args, &block)
end
- def self.last
- data = database.execute "SELECT #{all_columns.join(', ')} \
- FROM #{table_name} ORDER BY id DESC LIMIT 1"
- row_to_object(data.flatten)
- end
+ private
- def self.find(id)
- data = database.execute "SELECT #{all_columns.join(', ')}\
- FROM #{table_name} WHERE id = ?", id
- row_to_object data.flatten
+ def hash_to_properties(hash)
+ hash.each do |column, value|
+ instance_variable_set("@#{column}", value)
+ end
end
- def destroy
- database.execute "DELETE FROM #{table_name} WHERE id = ?", id
- end
-
- def self.destroy(id)
- database.execute "DELETE FROM #{table_name} WHERE id = ?", id
- end
-
- def self.destroy_all
- database.execute "DELETE FROM #{table_name}"
- end
-
- def self.row_to_object(row)
- model = model_name.new
- all_columns.each_with_index do |attribute, index|
- model.send("#{attribute}=", row[index])
+ def to_hash
+ hashed_object = {}
+ instance_variables.each do |property|
+ hashed_object[property[1..-1].to_sym] =
+ instance_variable_get(property.to_s)
end
- model
+ hashed_object
end
end
end