https://github.com/rubyworks/instance
Slick Object Instance API
https://github.com/rubyworks/instance
Last synced: 6 months ago
JSON representation
Slick Object Instance API
- Host: GitHub
- URL: https://github.com/rubyworks/instance
- Owner: rubyworks
- License: bsd-2-clause
- Created: 2014-02-01T04:19:05.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2015-05-29T00:20:27.000Z (over 10 years ago)
- Last Synced: 2024-11-29T12:22:06.495Z (about 1 year ago)
- Language: Ruby
- Size: 309 KB
- Stars: 37
- Watchers: 5
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE.txt
Awesome Lists containing this 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.
```ruby
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.
```ruby
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](http://rubyworks.github.com/instance/qed.html) documentation.
## Copyrights
Copyright © 2014 [Rubyworks](http://rubyworks.github.io)
BSD-2-Clause License
See [LICENSE.txt](LICENSE.txt) file for license details.