Giter VIP home page Giter VIP logo

activerecordlite's Introduction

#Active Record Lite

##Summary ActiveRecordLite was built to explore how database changes occur using Ruby on Rails' ActiveRecord. This project further demonstrates a knowledge of ActiveRecord's meta-programming usage. This project does not include every feature of ActiveRecord, but contains many popular methods such as the find, where, has_many, belongs_to, has_one_through, and has_many_through of ActiveRecord::Base.

ActiveRecordLite uses SQLite for its database engine.

##Features The belongs_to #initialize method is written in such a way that an options hash can be passed as an argument so override ActiveRecordLite's default keys.

class BelongsToOptions < AssocOptions
  def initialize(name, options = {})
    defaults = {
      :foreign_key => options[:foreign_key] || "#{name}_id".to_sym,
      :class_name => options[:class_name] || name.to_s.camelcase,
      :primary_key => options[:primary_key] || :id
    }

    defaults.keys.each do |key|
      self.send("#{key}=", options[key] || defaults[key])
    end
  end
end

ActiveRecordLite includes the #where method to demonstrate understanding of how ActiveRecord translates queries into SQL. Parameters are passed as an argument and then used to filter the database through a SQL query.

def where(params)
  where_query = params.keys.map { |col| "#{col} = ?" }.join(" AND ")

  results = DBConnection.execute(<<-SQL, *params.values)
    SELECT
      *
    FROM
      #{table_name}
    WHERE
      #{where_query}
  SQL

  parse_all(results)
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.