accord 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +19 -0
- data/README.rst +88 -0
- data/lib/accord/version.rb +1 -1
- metadata +4 -2
data/LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2013 Rafael Cabral Coutinho
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.rst
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
Accord - object interfaces and adaptation for Ruby
|
2
|
+
==================================================
|
3
|
+
|
4
|
+
Accord is a gem providing basic object interface and adaptation support for
|
5
|
+
Ruby. An interface represents a protocol or contract (an API), and this gem
|
6
|
+
allows you to label your objects/classes/modules as being compliant with a
|
7
|
+
given API by declaring them as implementing/providing interfaces. Thus, one
|
8
|
+
would call this an implementation of `Design by Contract
|
9
|
+
<http://en.wikipedia.org/wiki/Design_by_contract>`_ in Ruby.
|
10
|
+
|
11
|
+
Object adaptation is also supported. Given an object which claims to provide
|
12
|
+
some API, and in a specific point of the code another API is needed, Accord
|
13
|
+
will allow adapting from the object API to the desired one (provided that an
|
14
|
+
*adapter* which can adapt the APIs is known by Accord).
|
15
|
+
|
16
|
+
This gem was largely inspired by the Python package ``zope.interface``,
|
17
|
+
maintained by the `Zope Toolkit Project <http://docs.zope.org/zopetoolkit/>`_.
|
18
|
+
``zope.interface`` source `can be found here
|
19
|
+
<https://github.com/zopefoundation/zope.interface>`_.
|
20
|
+
|
21
|
+
Disclaimer: this is not used in any real world project yet. The API is still
|
22
|
+
changing (and will, while this gem is not in the 1.0 version). Not ready for
|
23
|
+
prime time.
|
24
|
+
|
25
|
+
|
26
|
+
Basic usage
|
27
|
+
-----------
|
28
|
+
|
29
|
+
Declare some interfaces::
|
30
|
+
|
31
|
+
Labelled = Accord::Interface(:Labelled) do
|
32
|
+
responds_to :label
|
33
|
+
end
|
34
|
+
PersonAPI = Accord::Interface(:PersonAPI)
|
35
|
+
|
36
|
+
Implement them::
|
37
|
+
|
38
|
+
class Person
|
39
|
+
attr_reader :name
|
40
|
+
def initialize(name)
|
41
|
+
@name = name
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class PersonLabel
|
46
|
+
def initialize(person)
|
47
|
+
@person = person
|
48
|
+
end
|
49
|
+
def label
|
50
|
+
@person.name
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Declare that your classes are implementations of the interfaces::
|
55
|
+
|
56
|
+
Accord::Declarations.implements(Person, PersonAPI)
|
57
|
+
Accord::Declarations.implements(PersonLabel, Labelled)
|
58
|
+
|
59
|
+
Tell Accord that people can have labels (that is, they can be adapted to
|
60
|
+
`Labelled`)::
|
61
|
+
|
62
|
+
Accord.default_adapter_registry.register([PersonAPI], Labelled) do |person|
|
63
|
+
PersonLabel.new(person)
|
64
|
+
end
|
65
|
+
|
66
|
+
Now, in a piece of code which needs a label::
|
67
|
+
|
68
|
+
def show(object)
|
69
|
+
Labelled.adapt!(object).label
|
70
|
+
end
|
71
|
+
|
72
|
+
When a person is given to `#show`, its name will be used to provide a label::
|
73
|
+
|
74
|
+
person = Person.new('John')
|
75
|
+
show(person) #=> 'John'
|
76
|
+
|
77
|
+
By registering more adapters, `#show` can be reused to show the labels of
|
78
|
+
other types of objects, without changing its source. `#show` doesn't rely on
|
79
|
+
receiving a person, but depends only on an interface. Also, everything could
|
80
|
+
have been added in the application after the `Person` class (as if that class
|
81
|
+
were legacy code), without modifying its source. Or `Person` could be a class
|
82
|
+
in a third party library.
|
83
|
+
|
84
|
+
|
85
|
+
License
|
86
|
+
-------
|
87
|
+
|
88
|
+
This gem is MIT-licensed. See LICENSE.txt in the root of this repository.
|
data/lib/accord/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: accord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -38,6 +38,8 @@ files:
|
|
38
38
|
- .rvmrc
|
39
39
|
- Gemfile
|
40
40
|
- Gemfile.lock
|
41
|
+
- LICENSE.txt
|
42
|
+
- README.rst
|
41
43
|
- accord.gemspec
|
42
44
|
- lib/accord.rb
|
43
45
|
- lib/accord/adapter_registry.rb
|