Project

instance

0.01
No commit activity in last 3 years
No release in over 3 years
Instance is a spin-off of Ruby Facets. It provides a simple convenient API for accessing an object's state.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
 Project Readme

Instance

What Is It?

Instance is a convenient and safe API for accessing and manipulating an object's state.

How Does It Work

Instance adds a method to all objects called #instance. It returns an Instance delegator that provides the full interface to the object's state.

Of course, without implementing this in C, directly in the Ruby source, we are left to depend on the current provisions Ruby has for accessing the state of an object. So there are some limitations here. However, we implement the Ruby code in such a way as to minimize the downsides by caching all the method definitions the Instance class will utilize.

Usage

Let's use a very simple example class with which to demonstrate usage.

    class Song
      attr_accessor :title
      attr_accessor :artist
      attr_accessor :year

      def initialize(title, artist, year)
        @title  = title
        @artist = artist
        @year   = year
      end
    end

Now we can create an instance of Song and work with it's state.

    song = Song.new("Paranoid", "Black Sabbath", 1970)

    song.instance.variables
    # => [:@title, :@artist, :@year]

    song.instance.get(:title)
    # => "Paranoid"

    song.instance[:artist]
    # => "Black Sabbath"

    song.instance.to_h        
    # => {:name => "Paranoid", :author => "Black Sabbath", :year => 1970)

For a more complete set of usage examples see the QED documentation.

Copyrights

Copyright © 2014 Rubyworks

BSD-2-Clause License

See LICENSE.txt file for license details.