Code Monkey home page Code Monkey logo

activedata's Introduction

Active Data

Active Data provides a way for Ruby objects to connect with relational database tables and allows for associations between different class models.

Objects created using Active Data will inherit from a SQLObject class and use strict naming conventions to directly tie that class model to a table of the same name. For example, a Cat object will relate to a table named cats.

DEMO: navigate to /lib folder and run ruby explorer.rb

Associations

  • Two models can relate by a has_many or belongs_to association:
class Guitar < SQLObject
  belongs_to :musician
end

class Musician < SQLObject
  has_many :guitars
  belongs_to :band
end

class Band < SQLObject
  has_many :musicians
end
  • Running Musician.first.guitars will take the first Musician from the musicians table and return a list of Guitar objects that are owned by that Musician.

SQLObject methods

Class Methods

  • ::all - Returns a list of every row in a particular table
  • ::columns - Returns a list of each column name in a particular table
  • ::finalize! - Creates setter and getter methods for each table column:
  Band.finalize!
  Band.columns # => [:id, :name]
  b = Band.new
  b.name = "Radiohead"
  b.name #=> "Radiohead"
  • ::find(id) - Fetches a row with a matching id and returns a new object
  • ::first, ::last - Returns the first or last row in a table as a new object

Instance Methods

  • #attributes - Returns a hash of attributes for a given instance
  • #attribute_values - Returns an array of attribute values
  • #insert - Creates a new row in the data table for a given instance
  • #update - Updates the row in the data table that matches the given id
  • #save - Calls either insert or update based on the object's current status in the database

Searchable Module

  • #where - The SQLObject class extends the Searchable module and uses where to search the database for specific values
  Band.where( { name: "Radiohead" } )
  #=> [#<Band:0x007ff8d373a248 @attributes={:id=>3, :name=>"Radiohead"}>]

activedata's People

Contributors

sdeluc1 avatar

Stargazers

Dzheni Dilcheva avatar

Watchers

James Cloos avatar  avatar

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.