instance 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.index +62 -0
- data/HISTORY.md +10 -0
- data/LICENSE.txt +23 -0
- data/README.md +58 -0
- data/demo/applique/env.rb +1 -0
- data/demo/instance.md +149 -0
- data/etc/qed.rb +8 -0
- data/lib/instance.rb +303 -0
- data/lib/instance.yml +62 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cee2d02baf52220acdc856e29882e4f010ba2738
|
4
|
+
data.tar.gz: 431b52894e0f05ed07f01a659a32dfee9afd46d5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6db8dc0db1016e250ef002411b651027c934e627baf1feb292e1846c412897b35b975cad3de8a993a7f7c8b746dfa132d45d5e409c1e267e525851f02c86c35e
|
7
|
+
data.tar.gz: 36957724377899c20c5e5e604444b8e3da459522fea4c9154ec1781c89174354912e653a9a8848d3e464431c899e841b6b8ba188c284796cbae199ca3f379a49
|
data/.index
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
---
|
2
|
+
revision: 2013
|
3
|
+
type: ruby
|
4
|
+
sources:
|
5
|
+
- index/
|
6
|
+
authors:
|
7
|
+
- name: Thomas Sawyer
|
8
|
+
email: transfire@gmail.com
|
9
|
+
organizations: []
|
10
|
+
requirements:
|
11
|
+
- groups:
|
12
|
+
- test
|
13
|
+
development: true
|
14
|
+
name: qed
|
15
|
+
- groups:
|
16
|
+
- test
|
17
|
+
development: true
|
18
|
+
name: ae
|
19
|
+
- groups:
|
20
|
+
- test
|
21
|
+
development: true
|
22
|
+
name: simplecov
|
23
|
+
conflicts: []
|
24
|
+
alternatives: []
|
25
|
+
resources:
|
26
|
+
- type: home
|
27
|
+
uri: http://rubyworks.github.com/instance
|
28
|
+
label: Homepage
|
29
|
+
- type: code
|
30
|
+
uri: http://github.com/rubyworks/instance
|
31
|
+
label: Source Code
|
32
|
+
- type: bugs
|
33
|
+
uri: http://github.com/rubyworks/instance/issues
|
34
|
+
label: Issue Tracker
|
35
|
+
- type: docs
|
36
|
+
uri: http://rubydoc.info/gems/instance
|
37
|
+
label: Documentation
|
38
|
+
- type: mail
|
39
|
+
uri: http://groups.google.com/group/facets-universal
|
40
|
+
label: Mailing List
|
41
|
+
repositories:
|
42
|
+
- name: upstream
|
43
|
+
scm: git
|
44
|
+
uri: git@github.com:rubyworks/instance.git
|
45
|
+
categories: []
|
46
|
+
copyrights:
|
47
|
+
- holder: Rubyworks
|
48
|
+
year: '2014'
|
49
|
+
license: BSD-2-Clause
|
50
|
+
customs: []
|
51
|
+
paths:
|
52
|
+
load:
|
53
|
+
- lib
|
54
|
+
name: instance
|
55
|
+
title: Instance
|
56
|
+
summary: Object Instance API
|
57
|
+
created: '2014-01-31'
|
58
|
+
description: |-
|
59
|
+
Instance is a spin-off of Ruby Facets. It provides a simple convenient API
|
60
|
+
for accessing an object's state.
|
61
|
+
version: 0.1.0
|
62
|
+
date: '2014-02-01'
|
data/HISTORY.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
BSD-2-Clause License (http://spdx.org/licenses/BSD-2-Clause)
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are
|
4
|
+
permitted provided that the following conditions are met:
|
5
|
+
|
6
|
+
1. Redistributions of source code must retain the above copyright notice, this list of
|
7
|
+
conditions and the following disclaimer.
|
8
|
+
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list
|
10
|
+
of conditions and the following disclaimer in the documentation and/or other materials
|
11
|
+
provided with the distribution.
|
12
|
+
|
13
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
14
|
+
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
|
16
|
+
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
17
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
18
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
19
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
20
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
21
|
+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
22
|
+
|
23
|
+
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Instance
|
2
|
+
|
3
|
+
## What Is It?
|
4
|
+
|
5
|
+
Instance is a *convenient* and *safe* API for accessing and manipulating
|
6
|
+
an object's state.
|
7
|
+
|
8
|
+
## How Does It Work
|
9
|
+
|
10
|
+
Instance adds a method to all objects called `#instance`. It returns
|
11
|
+
an `Instance` delegator that provides the full interface to the
|
12
|
+
object's instance state.
|
13
|
+
|
14
|
+
Of course, without implementing this in C, directly in the Ruby source,
|
15
|
+
we are left to depend on the current provisions Ruby has for accessing
|
16
|
+
the state of an object. So there are some limitations here. However,
|
17
|
+
we implement the Ruby code in such a way as to minimize the downsides
|
18
|
+
by caching all the method definitions the Instance class will utilize.
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Let's use a very simple example class with which to demonstrate usage.
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class Song
|
26
|
+
attr_accessor :title
|
27
|
+
attr_accessor :author
|
28
|
+
attr_accessor :length
|
29
|
+
|
30
|
+
def initialize(title, author, length)
|
31
|
+
@title = title
|
32
|
+
@author = author
|
33
|
+
@length = length
|
34
|
+
end
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
song = Song.new("Paranoid", "Black Sabbath", 1970)
|
40
|
+
|
41
|
+
song.instance.variables #=> [:@author, :@title, :@length]
|
42
|
+
|
43
|
+
song.instance.get(:name) #=> "Black Sabbath"
|
44
|
+
|
45
|
+
song.instance[:name] #=> "Black Sabbath"
|
46
|
+
```
|
47
|
+
|
48
|
+
For more a more complete set of usage examples see the QED documentation.
|
49
|
+
|
50
|
+
|
51
|
+
## Copyrights
|
52
|
+
|
53
|
+
Copyright 2014 Rubyworks
|
54
|
+
|
55
|
+
BSD-2-Clause License
|
56
|
+
|
57
|
+
See LICENSE.txt file for license details.
|
58
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'ae'
|
data/demo/instance.md
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
# Instance
|
2
|
+
|
3
|
+
First thing we need to do, of course, is load the library.
|
4
|
+
|
5
|
+
require 'instance'
|
6
|
+
|
7
|
+
Now we can create an example class with which to work.
|
8
|
+
|
9
|
+
class ::Friend
|
10
|
+
attr_accessor :name, :age, :phone
|
11
|
+
|
12
|
+
def initialize(name, age, phone)
|
13
|
+
@name, @age, @phone = name, age, phone
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
And now demonstrate the available API.
|
18
|
+
|
19
|
+
## Instance#variables
|
20
|
+
|
21
|
+
f1 = Friend.new("John", 30, "555-1212")
|
22
|
+
f1.instance.variables.assert == [:@name, :@age, :@phone]
|
23
|
+
|
24
|
+
## Instance#names
|
25
|
+
|
26
|
+
f1 = Friend.new("John", 30, "555-1212")
|
27
|
+
f1.instance.names.assert == ["name", "age", "phone"]
|
28
|
+
|
29
|
+
## Instance#keys
|
30
|
+
|
31
|
+
f1 = Friend.new("John", 30, "555-1212")
|
32
|
+
f1.instance.keys.assert == [:name, :age, :phone]
|
33
|
+
|
34
|
+
## Instance#values
|
35
|
+
|
36
|
+
f1 = Friend.new("John", 30, "555-1212")
|
37
|
+
f1.instance.values.assert == ["John", 30, "555-1212"]
|
38
|
+
|
39
|
+
## Instance#size
|
40
|
+
|
41
|
+
The `#size` method reports how many instance variables are defined.
|
42
|
+
|
43
|
+
f = Friend.new("John", 30, "555-1212")
|
44
|
+
f.instance.size.assert == 3
|
45
|
+
|
46
|
+
This method is primarily of use to the Enumerable mixin.
|
47
|
+
|
48
|
+
## Instance#variable_defined?
|
49
|
+
|
50
|
+
f = Friend.new("John", 30, "555-1212")
|
51
|
+
f.instance.assert.variable_defined?(:@name)
|
52
|
+
f.instance.assert.variable_defined?(:name)
|
53
|
+
|
54
|
+
## Instance#update and #assign
|
55
|
+
|
56
|
+
f1 = Friend.new("John", 30, "555-1212")
|
57
|
+
f1.name.assert == 'John'
|
58
|
+
|
59
|
+
f1.instance.update(:name=>'Jerry')
|
60
|
+
f1.name.assert == 'Jerry'
|
61
|
+
|
62
|
+
f1.instance.assign(:name=>'John')
|
63
|
+
f1.name.assert == 'John'
|
64
|
+
|
65
|
+
## Instance#to_h
|
66
|
+
|
67
|
+
We can convert the object's state, i.e. it's instance variable names and values
|
68
|
+
into a Hash very easily with the `#to_h` method.
|
69
|
+
|
70
|
+
f1 = Friend.new("John", 30, "555-1212")
|
71
|
+
f1.instance.to_h.assert == {:name=>"John", :age=>30, :phone=>"555-1212"}
|
72
|
+
|
73
|
+
Notice that the `@` has beenn removed from the instance variable names. If we
|
74
|
+
want the `@` to stay simply pass `true` to the `#to_h` method.
|
75
|
+
|
76
|
+
f1 = Friend.new("John", 30, "555-1212")
|
77
|
+
f1.instance.to_h(true).assert == {:@name=>"John", :@age=>30, :@phone=>"555-1212"}
|
78
|
+
|
79
|
+
## Instance#class
|
80
|
+
|
81
|
+
f = Friend.new("John", 30, "555-1212")
|
82
|
+
f.instance.class.assert == ::Friend
|
83
|
+
|
84
|
+
## Instance#id
|
85
|
+
|
86
|
+
f = Friend.new("John", 30, "555-1212")
|
87
|
+
f.instance.id == f.object_id
|
88
|
+
|
89
|
+
## Instance#of?
|
90
|
+
|
91
|
+
f = Friend.new("John", 30, "555-1212")
|
92
|
+
f.instance.assert.of?(::Friend)
|
93
|
+
|
94
|
+
## Instance#get and #set
|
95
|
+
|
96
|
+
f = Friend.new("John", 30, "555-1212")
|
97
|
+
f.instance.get(:name).assert == "John"
|
98
|
+
f.instance.set(:name, "Bill")
|
99
|
+
f.name.assert == "Bill"
|
100
|
+
|
101
|
+
## Instance#remove
|
102
|
+
|
103
|
+
f = Friend.new("John", 30, "555-1212")
|
104
|
+
f.instance.remove(:name)
|
105
|
+
f.instance.refute.variable_defined?(:@name)
|
106
|
+
|
107
|
+
## Instance#method
|
108
|
+
|
109
|
+
The Instance class provides a `method` method for getting a Method object for
|
110
|
+
any of the target object's methods by name.
|
111
|
+
|
112
|
+
f1 = Friend.new("John", 30, "555-1212")
|
113
|
+
m = f1.instance.method(:name)
|
114
|
+
m.assert.is_a?(::Method)
|
115
|
+
|
116
|
+
This can seem a little confusing because Ruby's `instance_method` method actually
|
117
|
+
returns an `UnboundMethod` object. Unfortunately Ruby's use of the term `instance_method`
|
118
|
+
is a complete misnomer. It should be something like `method_definition` instead.
|
119
|
+
The actual *instance method* is the object's method.
|
120
|
+
|
121
|
+
## Instance#methods
|
122
|
+
|
123
|
+
f = Friend.new("John", 30, "555-1212")
|
124
|
+
f.instance.methods
|
125
|
+
f.instance.methods(:private)
|
126
|
+
f.instance.methods(:protected)
|
127
|
+
f.instance.methods(:public)
|
128
|
+
f.instance.methods(:public, :private)
|
129
|
+
|
130
|
+
## Instance#eval and #exec
|
131
|
+
|
132
|
+
f = Friend.new("John", 30, "555-1212")
|
133
|
+
f.instance.eval("@name").assert == "John"
|
134
|
+
f.instance.exec{ @name }.assert == "John"
|
135
|
+
|
136
|
+
## Instance#send
|
137
|
+
|
138
|
+
Sending a message to an object as if within the object itself and thus by-passing
|
139
|
+
method visibility is somehting that should only be done as an act of *metaprogramming*.
|
140
|
+
Hence it makes sense for it to be done via the `instance` interface.
|
141
|
+
|
142
|
+
f = Friend.new("John", 30, "555-1212")
|
143
|
+
f.send(:name).assert == "John"
|
144
|
+
|
145
|
+
## Instance#delegate
|
146
|
+
|
147
|
+
f = Friend.new("John", 30, "555-1212")
|
148
|
+
f.instance.delegate.assert == f
|
149
|
+
|
data/etc/qed.rb
ADDED
data/lib/instance.rb
ADDED
@@ -0,0 +1,303 @@
|
|
1
|
+
# Instance class is a delgator for any object which provides
|
2
|
+
# an elegant and protected interface to an object's state, i.e.
|
3
|
+
# its *instance*.
|
4
|
+
#
|
5
|
+
# Examples
|
6
|
+
#
|
7
|
+
# class Friend
|
8
|
+
# attr_accessor :name, :age, :phone
|
9
|
+
# def initialize(name, age, phone)
|
10
|
+
# @name, @age, @phone = name, age, phone
|
11
|
+
# end
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# f1 = Friend.new("John", 30, "555-1212")
|
15
|
+
# f1.instance
|
16
|
+
#
|
17
|
+
# f1.instance.update({:name=>'Jerry'})
|
18
|
+
# f1.instance
|
19
|
+
#
|
20
|
+
# TODO: Should we add `is_a?` and `kind_of?` too?
|
21
|
+
|
22
|
+
class Instance
|
23
|
+
include Enumerable
|
24
|
+
|
25
|
+
# Store Object methods so they cannot be overriden by the delegate class.
|
26
|
+
METHODS = {}
|
27
|
+
|
28
|
+
def self.freeze_method(name)
|
29
|
+
METHODS[name.to_sym] = Object.instance_method(name)
|
30
|
+
end
|
31
|
+
|
32
|
+
freeze_method :object_id
|
33
|
+
freeze_method :class
|
34
|
+
freeze_method :instance_of?
|
35
|
+
freeze_method :method
|
36
|
+
freeze_method :methods
|
37
|
+
freeze_method :public_methods
|
38
|
+
freeze_method :protected_methods
|
39
|
+
freeze_method :private_methods
|
40
|
+
freeze_method :instance_eval
|
41
|
+
freeze_method :instance_exec
|
42
|
+
freeze_method :instance_variables
|
43
|
+
freeze_method :instance_variable_get
|
44
|
+
freeze_method :instance_variable_set
|
45
|
+
freeze_method :instance_variable_defined?
|
46
|
+
freeze_method :remove_instance_variable
|
47
|
+
freeze_method :send
|
48
|
+
|
49
|
+
#module Kernel
|
50
|
+
# # Returns an instance of Instance for +self+,
|
51
|
+
# # which allows convenient access to an object's
|
52
|
+
# # internals.
|
53
|
+
# def instance
|
54
|
+
# Instance.instance(self)
|
55
|
+
# end
|
56
|
+
#end
|
57
|
+
|
58
|
+
# Instance cache acts as a global cache for instances of Instance.
|
59
|
+
@cache = {}
|
60
|
+
|
61
|
+
# Instance is multiton. Use this method instead of #new to get a
|
62
|
+
# cached instance.
|
63
|
+
def self.instance(delegate)
|
64
|
+
@cache[delegate] ||= Instance.new(delegate)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Initialize new Instance instance.
|
68
|
+
def initialize(delegate)
|
69
|
+
@delegate = delegate
|
70
|
+
end
|
71
|
+
|
72
|
+
# The delegated object.
|
73
|
+
def delegate
|
74
|
+
@delegate
|
75
|
+
end
|
76
|
+
|
77
|
+
# Iterate over instance variables.
|
78
|
+
def each
|
79
|
+
variables.each do |name|
|
80
|
+
yield(name[1..-1].to_sym, get(name))
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Number of instance variables.
|
85
|
+
def size
|
86
|
+
variables.size
|
87
|
+
end
|
88
|
+
|
89
|
+
# Get instance variables with values as a hash.
|
90
|
+
#
|
91
|
+
# Examples
|
92
|
+
#
|
93
|
+
# class X
|
94
|
+
# def initialize(a,b)
|
95
|
+
# @a, @b = a, b
|
96
|
+
# end
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# x = X.new(1,2)
|
100
|
+
#
|
101
|
+
# x.instance.to_h #=> { :a=>1, :b=>2 }
|
102
|
+
#
|
103
|
+
# Returns [Hash].
|
104
|
+
def to_h(at=false)
|
105
|
+
h = {}
|
106
|
+
if at
|
107
|
+
variables.each do |name|
|
108
|
+
h[name] = get(name)
|
109
|
+
end
|
110
|
+
else
|
111
|
+
each do |key, value|
|
112
|
+
h[key] = value
|
113
|
+
end
|
114
|
+
end
|
115
|
+
h
|
116
|
+
end
|
117
|
+
|
118
|
+
# TODO: Not sure if this should be used.
|
119
|
+
alias_method :to_hash, :to_h
|
120
|
+
|
121
|
+
#
|
122
|
+
def get(name)
|
123
|
+
name = atize(name)
|
124
|
+
#@delegate.instance_variable_get(name)
|
125
|
+
METHODS[:instance_variable_get].bind(@delegate).call(name)
|
126
|
+
end
|
127
|
+
alias :[] :get
|
128
|
+
|
129
|
+
#
|
130
|
+
def set(name, value)
|
131
|
+
name = atize(name)
|
132
|
+
#@delegate.instance_variable_set(name,value)
|
133
|
+
METHODS[:instance_variable_set].bind(@delegate).call(name,value)
|
134
|
+
end
|
135
|
+
alias :[]= :set
|
136
|
+
|
137
|
+
#
|
138
|
+
def <<(pair)
|
139
|
+
name, value = *pair
|
140
|
+
name = atize(name)
|
141
|
+
set(name, value)
|
142
|
+
end
|
143
|
+
|
144
|
+
# Remove instance variable.
|
145
|
+
def remove(name)
|
146
|
+
name = atize(name)
|
147
|
+
METHODS[:remove_instance_variable].bind(@delegate).call(name)
|
148
|
+
end
|
149
|
+
|
150
|
+
# Set instance variables given a +hash+.
|
151
|
+
#
|
152
|
+
# instance.update('@a'=>1, '@b'=>2)
|
153
|
+
# @a #=> 1
|
154
|
+
# @b #=> 2
|
155
|
+
#
|
156
|
+
# Also, +@+ sign is not neccessary.
|
157
|
+
#
|
158
|
+
# instance.update(:a=>1, :b=>2)
|
159
|
+
# @a #=> 1
|
160
|
+
# @b #=> 2
|
161
|
+
#
|
162
|
+
def update(hash)
|
163
|
+
hash.each do |pair|
|
164
|
+
self << pair
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
# A hold-over from the the old #instance_assign method.
|
169
|
+
alias_method :assign, :update
|
170
|
+
|
171
|
+
# Same as #instance_variables.
|
172
|
+
def variables
|
173
|
+
#@delegate.instance_variables
|
174
|
+
METHODS[:instance_variables].bind(@delegate).call
|
175
|
+
end
|
176
|
+
|
177
|
+
# Instance vairable names as symbols.
|
178
|
+
#
|
179
|
+
# Returns [Array<Symbols>].
|
180
|
+
def keys
|
181
|
+
variables.collect do |name|
|
182
|
+
name[1..-1].to_sym
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
# Instance variable names as strings.
|
187
|
+
#
|
188
|
+
# Returns [Array<String>].
|
189
|
+
def names
|
190
|
+
variables.collect do |name|
|
191
|
+
name[1..-1]
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
# Instance variable values.
|
196
|
+
#
|
197
|
+
# Returns [Array<Object>].
|
198
|
+
def values
|
199
|
+
variables.collect do |name|
|
200
|
+
get(name)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# Instance evaluation.
|
205
|
+
def eval(*a,&b)
|
206
|
+
#@delegate.instance_eval(*a,&b)
|
207
|
+
METHODS[:instance_eval].bind(@delegate).call(*a,&b)
|
208
|
+
end
|
209
|
+
|
210
|
+
# Instance execution.
|
211
|
+
def exec(*a,&b)
|
212
|
+
#@delegate.instance_exec(*a,&b)
|
213
|
+
METHODS[:instance_exec].bind(@delegate).call(*a,&b)
|
214
|
+
end
|
215
|
+
|
216
|
+
# Get method. Usage of this might seem strange because Ruby's own
|
217
|
+
# `instance_method` method is a misnomer. It should be something
|
218
|
+
# like `definition` or `method_definition`. In Ruby the acutal
|
219
|
+
# "instance" method is accessed via the unadorned `method` method.
|
220
|
+
#
|
221
|
+
# Returns [Method].
|
222
|
+
def method(name)
|
223
|
+
#@delegate.instance_exec(*a,&b)
|
224
|
+
METHODS[:method].bind(@delegate).call(name)
|
225
|
+
end
|
226
|
+
|
227
|
+
# Returns list of method names.
|
228
|
+
#
|
229
|
+
# Returns [Array<Symbol>].
|
230
|
+
def methods(*selection)
|
231
|
+
list = []
|
232
|
+
|
233
|
+
if selection.empty?
|
234
|
+
list.concat METHODS[:methods].bind(@delegate).call
|
235
|
+
end
|
236
|
+
|
237
|
+
selection.each do |s|
|
238
|
+
case s
|
239
|
+
when :public
|
240
|
+
list.concat METHODS[:public_methods].bind(@delegate).call
|
241
|
+
when :protected
|
242
|
+
list.concat METHODS[:protected_methods].bind(@delegate).call
|
243
|
+
when :private
|
244
|
+
list.concat METHODS[:private_methods].bind(@delegate).call
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
return list
|
249
|
+
end
|
250
|
+
|
251
|
+
# Is the object an instance of a given class?
|
252
|
+
#
|
253
|
+
# Returns [Boolean]
|
254
|
+
def of?(a_class)
|
255
|
+
#@delegate.instance_of?(aclass)
|
256
|
+
METHODS[:instance_of?].bind(@delegate).call(a_class)
|
257
|
+
end
|
258
|
+
|
259
|
+
#
|
260
|
+
def variable_defined?(name)
|
261
|
+
name = atize(name)
|
262
|
+
#@delegate.variable_defined?(name)
|
263
|
+
METHODS[:instance_variable_defined?].bind(@delegate).call(name)
|
264
|
+
end
|
265
|
+
|
266
|
+
# Get object's instance id.
|
267
|
+
#
|
268
|
+
# Returns [Integer]
|
269
|
+
def id
|
270
|
+
#@delegate.variable_defined?(name)
|
271
|
+
METHODS[:object_id].bind(@delegate).call
|
272
|
+
end
|
273
|
+
|
274
|
+
# Get object's instance id.
|
275
|
+
#
|
276
|
+
# Returns [Class]
|
277
|
+
def class
|
278
|
+
#@delegate.variable_defined?(name)
|
279
|
+
METHODS[:class].bind(@delegate).call
|
280
|
+
end
|
281
|
+
|
282
|
+
# Send message to instance.
|
283
|
+
def send(*a, &b)
|
284
|
+
METHODS[:send].bind(@delegate).call(*a, &b)
|
285
|
+
end
|
286
|
+
|
287
|
+
private
|
288
|
+
|
289
|
+
def atize(name)
|
290
|
+
name.to_s !~ /^@/ ? "@#{name}" : name
|
291
|
+
end
|
292
|
+
|
293
|
+
end
|
294
|
+
|
295
|
+
|
296
|
+
class BasicObject
|
297
|
+
# Returns an instance of Instance for `self`, which allows convenient
|
298
|
+
# access to an object's internals.
|
299
|
+
def instance
|
300
|
+
::Instance.instance(self)
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
data/lib/instance.yml
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
---
|
2
|
+
revision: 2013
|
3
|
+
type: ruby
|
4
|
+
sources:
|
5
|
+
- index/
|
6
|
+
authors:
|
7
|
+
- name: Thomas Sawyer
|
8
|
+
email: transfire@gmail.com
|
9
|
+
organizations: []
|
10
|
+
requirements:
|
11
|
+
- groups:
|
12
|
+
- test
|
13
|
+
development: true
|
14
|
+
name: qed
|
15
|
+
- groups:
|
16
|
+
- test
|
17
|
+
development: true
|
18
|
+
name: ae
|
19
|
+
- groups:
|
20
|
+
- test
|
21
|
+
development: true
|
22
|
+
name: simplecov
|
23
|
+
conflicts: []
|
24
|
+
alternatives: []
|
25
|
+
resources:
|
26
|
+
- type: home
|
27
|
+
uri: http://rubyworks.github.com/instance
|
28
|
+
label: Homepage
|
29
|
+
- type: code
|
30
|
+
uri: http://github.com/rubyworks/instance
|
31
|
+
label: Source Code
|
32
|
+
- type: bugs
|
33
|
+
uri: http://github.com/rubyworks/instance/issues
|
34
|
+
label: Issue Tracker
|
35
|
+
- type: docs
|
36
|
+
uri: http://rubydoc.info/gems/instance
|
37
|
+
label: Documentation
|
38
|
+
- type: mail
|
39
|
+
uri: http://groups.google.com/group/facets-universal
|
40
|
+
label: Mailing List
|
41
|
+
repositories:
|
42
|
+
- name: upstream
|
43
|
+
scm: git
|
44
|
+
uri: git@github.com:rubyworks/instance.git
|
45
|
+
categories: []
|
46
|
+
copyrights:
|
47
|
+
- holder: Rubyworks
|
48
|
+
year: '2014'
|
49
|
+
license: BSD-2-Clause
|
50
|
+
customs: []
|
51
|
+
paths:
|
52
|
+
load:
|
53
|
+
- lib
|
54
|
+
name: instance
|
55
|
+
title: Instance
|
56
|
+
summary: Object Instance API
|
57
|
+
created: '2014-01-31'
|
58
|
+
description: |-
|
59
|
+
Instance is a spin-off of Ruby Facets. It provides a simple convenient API
|
60
|
+
for accessing an object's state.
|
61
|
+
version: 0.1.0
|
62
|
+
date: '2014-02-01'
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: instance
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thomas Sawyer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: qed
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: ae
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: simplecov
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: |-
|
56
|
+
Instance is a spin-off of Ruby Facets. It provides a simple convenient API
|
57
|
+
for accessing an object's state.
|
58
|
+
email:
|
59
|
+
- transfire@gmail.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files:
|
63
|
+
- LICENSE.txt
|
64
|
+
- README.md
|
65
|
+
- HISTORY.md
|
66
|
+
files:
|
67
|
+
- .index
|
68
|
+
- etc/qed.rb
|
69
|
+
- lib/instance.rb
|
70
|
+
- lib/instance.yml
|
71
|
+
- demo/applique/env.rb
|
72
|
+
- demo/instance.md
|
73
|
+
- README.md
|
74
|
+
- HISTORY.md
|
75
|
+
- LICENSE.txt
|
76
|
+
homepage: http://rubyworks.github.com/instance
|
77
|
+
licenses:
|
78
|
+
- BSD-2-Clause
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.0.3
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Object Instance API
|
100
|
+
test_files: []
|
101
|
+
has_rdoc:
|