yfactorial-roxy 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ 0.1.0
2
+
3
+ * Initial release.
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2008 Ryan Daigle
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 of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ 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, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,51 @@
1
+ h1. Roxy (A ruby proxy library)
2
+
3
+ h2. Summary
4
+
5
+ Roxy is a basic proxy library that lets you quickly create proxies between your ruby objects. Its syntax
6
+ is loosely based on "Association Extensions in ActiveRecord":http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
7
+ as that is a well-known use of proxies.
8
+
9
+ Please see the "CHANGELOG":http://github.com/yfactorial/utility_scopes/tree/master/CHANGELOG for contribution details.
10
+
11
+ Roxy has the following dependencies:
12
+
13
+ * rspec >= 1.1.4 (for specs only, not runtime)
14
+
15
+ h2. Installation
16
+
17
+ To install the roxy gem run the following:
18
+
19
+ sudo gem install yfactorial-roxy --source http://gems.github.com
20
+
21
+ And to enable the scopes in your project just require the @roxy@ library and give your object some moxie:
22
+
23
+ require 'roxy'
24
+ class Person
25
+ include Roxy::Moxie
26
+ ...
27
+ end
28
+
29
+ h2. Usage
30
+
31
+ See the announcement post for detailed usage examples: "http://ryandaigle.com/articles/2008/11/10/implement-ruby-proxy-objects-with-roxy":http://ryandaigle.com/articles/2008/11/10/implement-ruby-proxy-objects-with-roxy
32
+
33
+ Here's a basic example:
34
+
35
+ <pre><code>
36
+ require 'roxy'
37
+ class Person
38
+ include Roxy::Moxie
39
+
40
+ attr_accessor :first, :last, :parents
41
+
42
+ proxy :parents do
43
+ def divorced?
44
+ proxy_target.size > 1 and proxy_target.collect { |parent| parent.last }.uniq.size > 1
45
+ end
46
+ end
47
+ end
48
+
49
+ # Can then invoke your proxy methods directly on parents
50
+ person.parents.divorced?
51
+ </code></pre>
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/rdoctask'
4
+ require 'spec/rake/spectask'
5
+
6
+ desc 'Run the specs'
7
+ Spec::Rake::SpecTask.new(:spec) do |t|
8
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
9
+ t.spec_files = FileList['spec/**/*_spec.rb']
10
+ end
11
+
12
+ desc 'Generate RDoc documentation.'
13
+ Rake::RDocTask.new(:rdoc) do |rdoc|
14
+ rdoc.rdoc_files.include('README.textile', 'LICENSE', 'Rakefile').
15
+ include('lib/**/*.rb')
16
+
17
+ rdoc.main = "README.textile"
18
+ rdoc.title = "roxy documentation"
19
+
20
+ rdoc.rdoc_dir = 'doc' # rdoc output folder
21
+ rdoc.options << '--inline-source' << '--charset=UTF-8'
22
+ rdoc.options << '--webcvs=http://github.com/yfactorial/roxy/tree/master/'
23
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'roxy'
data/lib/roxy/moxie.rb ADDED
@@ -0,0 +1,27 @@
1
+ module Roxy
2
+ module Moxie
3
+
4
+ def self.included(within)
5
+ within.class_eval { extend ClassMethods }
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ # Set up this class to proxy on the given name
11
+ def proxy(name, options = {}, &block)
12
+
13
+ # If we don't have the :to option then we are proxying an existing
14
+ # method
15
+ if(!options[:to])
16
+ alias_method "proxied_#{name}", "#{name}"
17
+ options[:to] = lambda { |owner| owner.send("proxied_#{name}") }
18
+ end
19
+
20
+ define_method(name) do
21
+ instance_variable_get("@#{name}_proxy") ||
22
+ instance_variable_set("@#{name}_proxy", Proxy.new(self, options, &block))
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
data/lib/roxy/proxy.rb ADDED
@@ -0,0 +1,34 @@
1
+ module Roxy
2
+
3
+ # The very simple proxy class that provides a basic pass-through
4
+ # mechanism between the proxy owner and the proxy target.
5
+ class Proxy
6
+
7
+ alias :proxy_instance_eval :instance_eval
8
+ alias :proxy_extend :extend
9
+
10
+ # Make sure the proxy is as dumb as it can be.
11
+ # Blatanly taken from Jim Wierich's BlankSlate post:
12
+ # http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc
13
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^proxy_)/ }
14
+
15
+ def initialize(owner, options, &block)
16
+ @owner = owner
17
+ @target = options[:to]
18
+
19
+ # Adorn with user-provided proxy methods
20
+ [options[:extend]].flatten.each { |ext| proxy_extend(ext) } if options[:extend]
21
+ proxy_instance_eval &block if block_given?
22
+ end
23
+
24
+ def proxy_owner; @owner; end
25
+ def proxy_target
26
+ @proxy_target_object ||= @target.is_a?(Proc) ? @target.call(@owner) : @target
27
+ end
28
+
29
+ # Delegate all method calls we don't know about to target object
30
+ def method_missing(sym, *args, &block)
31
+ proxy_target.__send__(sym, *args, &block)
32
+ end
33
+ end
34
+ end
data/lib/roxy.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'roxy/moxie'
2
+ require 'roxy/proxy'
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), *%w[spec_helper])
2
+
3
+ $:.unshift "#{File.dirname(__FILE__)}/../lib"
4
+ require 'roxy'
5
+
6
+ # Load a test class
7
+ def uses_fixture(fixture_name)
8
+ require File.join(File.dirname(__FILE__), 'fixtures', fixture_name.to_s)
9
+ end
@@ -0,0 +1,53 @@
1
+ require File.join(File.dirname(__FILE__), *%w[abstract_spec])
2
+
3
+ describe "Whole family example" do
4
+
5
+ before do
6
+ uses_fixture(:family)
7
+ @ryan = Person.new('Ryan', 'Daigle')
8
+ @ryan_parents = [Person.new('Dad', 'Daigle'), Person.new('Mom', 'Daigle')]
9
+ @ryan.parents = @ryan_parents
10
+ @ryan_children = [Person.new('Child1', 'Daigle'), Person.new('Child2', 'Daigle')]
11
+ @ryan.children = @ryan_children
12
+ end
13
+
14
+ it "should know a person's parents (proxy should not overwrite target method)" do
15
+ @ryan.parents.should == @ryan_parents
16
+ end
17
+
18
+ it "should know if a person's parents are divorced" do
19
+ @ryan.parents.divorced?.should be_false
20
+ end
21
+
22
+ it "should know how to print out parents names' to a string when not divorced" do
23
+ @ryan.parents.to_s.should == "Mr. and Mrs. Daigle"
24
+ end
25
+
26
+ it "should know how to get a person's step-children" do
27
+ @ryan.children.step.should be_empty
28
+ end
29
+ end
30
+
31
+ describe "Divorced family example" do
32
+
33
+ before do
34
+ uses_fixture(:family)
35
+ @ryan = Person.new('Ryan', 'Daigle')
36
+ @ryan_parents = [Person.new('Dad', 'Daigle'), Person.new('Mom', 'NotDaigle')]
37
+ @ryan.parents = @ryan_parents
38
+ @ryan_children = [Person.new('Child1', 'Daigle'), Person.new('Child2', 'NotDaigle')]
39
+ @ryan.children = @ryan_children
40
+ end
41
+
42
+ it "should know if a person's parents are divorced" do
43
+ @ryan.parents.divorced?.should be_true
44
+ end
45
+
46
+ it "should know how to print out parents names' to a string when divorced" do
47
+ @ryan.parents.to_s.should == "Dad Daigle and Mom NotDaigle"
48
+ end
49
+
50
+ it "should know how to get a person's step-children" do
51
+ @ryan.children.step.should == [@ryan_children.last]
52
+ end
53
+ end
@@ -0,0 +1,38 @@
1
+ class Person
2
+
3
+ include Roxy::Moxie
4
+
5
+ attr_accessor :first, :last, :parents, :children
6
+
7
+ # Add ability to ask the parents collection if they are divorced
8
+ # (As defined by not having the same last name). Also print
9
+ # them out as a string taking this into account
10
+ proxy :parents do
11
+
12
+ def divorced?
13
+ proxy_target.size > 1 and proxy_target.collect { |parent| parent.last }.uniq.size > 1
14
+ end
15
+
16
+ def to_s
17
+ if divorced?
18
+ proxy_target.collect { |parent| parent.to_s }.join(' and ')
19
+ else
20
+ "Mr. and Mrs. #{proxy_target[0].last}"
21
+ end
22
+ end
23
+ end
24
+
25
+ # Add ability to ask the children collection for the list of
26
+ # step-children
27
+ proxy :children do
28
+ def step
29
+ proxy_target.select { |child| proxy_owner.last != child.last }
30
+ end
31
+ end
32
+
33
+ def initialize(first, last)
34
+ @first, @last = first, last
35
+ end
36
+
37
+ def to_s; "#{first} #{last}"; end
38
+ end
@@ -0,0 +1,30 @@
1
+ class Person
2
+
3
+ include Roxy::Moxie
4
+
5
+ proxy :children, :to => ['child1', 'child2'] do
6
+ def cost
7
+ 100 * proxy_target.size
8
+ end
9
+ end
10
+
11
+ def parents
12
+ ['parent1', 'parent2']
13
+ end
14
+
15
+ proxy :parents do
16
+ def divorced?
17
+ proxy_target.size < 2
18
+ end
19
+ end
20
+
21
+ module NeighborGeographics
22
+ def nearby?; true; end
23
+ end
24
+
25
+ module NeighborDemographics
26
+ def caucasian?; false; end
27
+ end
28
+
29
+ proxy :neighbors, :to => ['neighbor1', 'neighbor2'], :extend => [NeighborGeographics, NeighborDemographics]
30
+ end
@@ -0,0 +1,28 @@
1
+ require File.join(File.dirname(__FILE__), *%w[abstract_spec])
2
+
3
+ describe "Proxy" do
4
+
5
+ before(:each) do
6
+ @owner = "String owner"
7
+ @target = "String target"
8
+ @lambda_target = lambda { @target }
9
+ end
10
+
11
+ it "should properly evaluate a block-based target" do
12
+ proxy = Roxy::Proxy.new(@owner, :to => @lambda_target)
13
+ proxy.should == @target
14
+ end
15
+
16
+ it "should properly adorn the proxy with proxy methods" do
17
+ proxy = Roxy::Proxy.new(@owner, :to => @target) do
18
+ def poop; 'poop'; end
19
+ end
20
+ proxy.poop.should == 'poop'
21
+ end
22
+
23
+ it "should make the proxy owner accessible to the target block" do
24
+ proxy = Roxy::Proxy.new(@owner, :to => proc { |owner| owner })
25
+ proxy.should == @owner
26
+ end
27
+
28
+ end
data/spec/roxy_spec.rb ADDED
@@ -0,0 +1,35 @@
1
+ require File.join(File.dirname(__FILE__), *%w[abstract_spec])
2
+
3
+ describe "Roxy" do
4
+
5
+ before do
6
+ uses_fixture(:person)
7
+ @person = Person.new
8
+ end
9
+
10
+ it "should return the target when the proxy name is called" do
11
+ @person.children.should == ['child1', 'child2']
12
+ end
13
+
14
+ it "should return the evaluated target if the target is an existing method" do
15
+ @person.parents.should == ['parent1', 'parent2']
16
+ end
17
+
18
+ it "should return the evaluated target if the target is a proc" do
19
+ @person.neighbors.should == ['neighbor1', 'neighbor2']
20
+ end
21
+
22
+ it "should pass method invocations through to the target" do
23
+ @person.children.size.should == 2
24
+ end
25
+
26
+ it "should intercept block-based proxy methods" do
27
+ @person.children.cost.should == 200
28
+ @person.parents.divorced?.should be_false
29
+ end
30
+
31
+ it "should intercept :extend based proxy methods" do
32
+ @person.neighbors.nearby?.should be_true
33
+ @person.neighbors.caucasian?.should be_false
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yfactorial-roxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Daigle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-11 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A Ruby library for quickly creating proxy objects.
17
+ email: ryan@yfactorial.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.textile
24
+ - Rakefile
25
+ - LICENSE
26
+ - CHANGELOG
27
+ files:
28
+ - README.textile
29
+ - Rakefile
30
+ - LICENSE
31
+ - CHANGELOG
32
+ - init.rb
33
+ - lib
34
+ - lib/roxy
35
+ - lib/roxy.rb
36
+ - lib/roxy/moxie.rb
37
+ - lib/roxy/proxy.rb
38
+ - spec
39
+ - spec/abstract_spec.rb
40
+ - spec/proxy_spec.rb
41
+ - spec/roxy_spec.rb
42
+ - spec/family_spec.rb
43
+ - spec/fixtures
44
+ - spec/fixtures/person.rb
45
+ - spec/fixtures/family.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/yfactorial/roxy
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --main
51
+ - README.textile
52
+ - --inline-source
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.2.0
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: A Ruby library for quickly creating proxy objects.
75
+ test_files: []
76
+