chain-boy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create use ruby-1.9.2@chain-boy
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chain-boy.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/chain-boy.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/chain-boy/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Dan Watson"]
6
+ gem.email = ["dan@dotnetguy.co.uk"]
7
+ gem.description = %q{Dynamic delegator - created from the information demonstrated in RailsCasts episode 212}
8
+ gem.summary = %q{Think of DynamicDelegator as a proxy object that passes any calls to it to its target object}
9
+ gem.homepage = "https://github.com/dotnetguyuk/chain-boy"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "chain-boy"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Chain::Boy::VERSION
17
+
18
+ end
data/lib/chain-boy.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "chain-boy/version"
2
+ require "chain-boy/dynamic_delegator"
3
+
4
+
@@ -0,0 +1,11 @@
1
+ class DynamicDelegator < BasicObject
2
+ def initialize(target)
3
+ @target = target
4
+ end
5
+
6
+ def method_missing(*args, &block)
7
+ result = @target.send(*args, &block)
8
+ @target = result if result.kind_of? @target.class
9
+ result
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Chain
2
+ module Boy
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/readme.rdoc ADDED
@@ -0,0 +1,26 @@
1
+ === Install the gem
2
+ gem install chain-boy
3
+ === Rails Gemfile
4
+ gem 'chain-boy'
5
+
6
+ ===So what does it do?
7
+
8
+ Turn this :
9
+ def self.search(params)
10
+ products = Product.scoped
11
+ products = products.where("name like ?", "%" + params[:name] + "%") if params[:name]
12
+ products = products.where("price >= ?", params[:price_gt]) if params[:price_gt]
13
+ products = products.where("price <= ?", params[:price_lt]) if params[:price_lt]
14
+ products
15
+ end
16
+
17
+ Info this :
18
+
19
+ def self.search(params)
20
+ products = DynamicDelegator.new(Product.scoped)
21
+ products.where("name like ?", "%" + params[:name] + "%") if params[:name]
22
+ products.where("price >= ?", params[:price_gt]) if params[:price_gt]
23
+ products.where("price <= ?", params[:price_lt]) if params[:price_lt]
24
+ products
25
+ end
26
+
@@ -0,0 +1,30 @@
1
+ libdir = File.dirname(File.dirname(__FILE__)) + '/lib'
2
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
3
+
4
+ require 'test/unit'
5
+ require 'chain-boy'
6
+
7
+
8
+ class DynamicDelegatorTests < Test::Unit::TestCase
9
+ def test_delegate_method_call
10
+ adder = DynamicDelegator.new(Adder.new(1))
11
+ adder.add(1)
12
+ adder.add(1)
13
+ assert_equal 3, adder.number
14
+ end
15
+
16
+ private
17
+ class Adder
18
+ def initialize(number)
19
+ @number = number
20
+ end
21
+
22
+ def add(number)
23
+ Adder.new(number + @number)
24
+ end
25
+
26
+ def number
27
+ @number
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chain-boy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dan Watson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-29 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Dynamic delegator - created from the information demonstrated in RailsCasts
15
+ episode 212
16
+ email:
17
+ - dan@dotnetguy.co.uk
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - .rvmrc
24
+ - Gemfile
25
+ - Rakefile
26
+ - chain-boy.gemspec
27
+ - lib/chain-boy.rb
28
+ - lib/chain-boy/dynamic_delegator.rb
29
+ - lib/chain-boy/version.rb
30
+ - readme.rdoc
31
+ - test/dynamic_delegator_tests.rb
32
+ homepage: https://github.com/dotnetguyuk/chain-boy
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.6
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Think of DynamicDelegator as a proxy object that passes any calls to it to
56
+ its target object
57
+ test_files:
58
+ - test/dynamic_delegator_tests.rb