Sha256: 865e5f1ea2e4c7ad04d2b05f85f6d5f76b60e3981939b65d74b1bf770038ccde

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

module Peictt
  class DatabaseMapper
    extend Peictt::Orm::QueryHelpers

    class << self
      attr_accessor :table
    end

    def initialize(model, action = :create)
      self.class.table = model.class.to_s.downcase.pluralize
      @model = model
      @action = action
      @variables = model.instance_variables
      set_columns_and_values
      add_timestamps if create?
      self
    end

    def save
      Database.execute_query query, @values
      true
    end

    private

    def query
      if create?
        return build_create_query
      else
        return build_update_query
      end
    end

    def create?
      @action == :create
    end

    def set_columns_and_values
      @placeholders, @columns = self.class.set_columns_for_instance @variables
      @values = self.class.get_values_for_instance(@model, @variables)
    end

    def prepare_columns_and_values_for_update
      id_index = @columns.index "id"
      @columns.delete_at(id_index)
      id_value = @values.delete_at id_index
      @values << id_value
    end

    def add_timestamps
      @placeholders << "?"
      @placeholders << "?"
      @columns << "created_at"
      @columns << "updated_at"
      @values << Time.now.to_s
      @values << Time.now.to_s
    end

    def build_create_query
      "INSERT INTO #{self.class.table} (#{@columns.join(', ')})"\
      "VALUES (#{@placeholders.join(', ')})"
    end

    def build_update_query
      prepare_columns_and_values_for_update
      columns_and_placeholders = @columns.zip(@placeholders[1..-1]).
                                 map { |pair| pair.join " = " }
      "UPDATE #{self.class.table} SET #{columns_and_placeholders.join(', ')} "\
      "WHERE id = ?"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
peictt-0.1.1 lib/peictt/orm/database_mapper.rb