auser-parenting 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2009-03-14
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,13 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/parenting.rb
7
+ lib/parenting/base.rb
8
+ parenting.gemspec
9
+ script/console
10
+ script/destroy
11
+ script/generate
12
+ test/test_helper.rb
13
+ test/test_parenting.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,2 @@
1
+
2
+ For more information on parenting, see http://parenting.rubyforge.org
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = Parenting
2
+
3
+ http://poolpartyrb.com
4
+
5
+ == DESCRIPTION:
6
+
7
+ Easily add parents to classes
8
+
9
+ == SYNOPSIS:
10
+
11
+ Usage:
12
+
13
+ Subclass parenting::base:
14
+
15
+ class Quickie < Parenting::Base
16
+ end
17
+
18
+ Then, use to your heart's content! When you instance_eval in a block, your parent will be set with parent. This is handled with a context stack that is globally accessible. You can get the object's current context by calling current_context on an object (at runtime).
19
+
20
+ == INSTALL:
21
+
22
+ sudo gem install auser-parenting
23
+
24
+ == LICENSE:
25
+
26
+ (The MIT License)
27
+
28
+ Copyright (c) 2009 Ari Lerner, Michael Fairchild, CloudTeam @ AT&T interactive
29
+
30
+ Permission is hereby granted, free of charge, to any person obtaining
31
+ a copy of this software and associated documentation files (the
32
+ 'Software'), to deal in the Software without restriction, including
33
+ without limitation the rights to use, copy, modify, merge, publish,
34
+ distribute, sublicense, and/or sell copies of the Software, and to
35
+ permit persons to whom the Software is furnished to do so, subject to
36
+ the following conditions:
37
+
38
+ The above copyright notice and this permission notice shall be
39
+ included in all copies or substantial portions of the Software.
40
+
41
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
42
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
44
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
45
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
46
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
47
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/parenting'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('parenting', Parenting::VERSION) do |p|
7
+ p.developer('Ari Lerner', 'arilerner@mac.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ # p.extra_deps = [
12
+ # ['activesupport','>= 2.0.2'],
13
+ # ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ # TODO - want other tests/tasks run by default? Add them to the list
28
+ # task :default => [:spec, :features]
data/lib/parenting.rb ADDED
@@ -0,0 +1,10 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module Parenting
5
+ VERSION = '0.0.2' unless const_defined?(:VERSION)
6
+ end
7
+
8
+ Dir["parenting/core/*.rb"].each {|lib| require lib }
9
+
10
+ require "parenting/base"
@@ -0,0 +1,61 @@
1
+ module Parenting
2
+ module ClassMethods
3
+ end
4
+
5
+ module InstanceMethods
6
+ def context_stack
7
+ $context_stack ||= []
8
+ end
9
+ def run_in_context(&block)
10
+ @parent = parent
11
+
12
+ context_stack.push self
13
+ this_context.instance_eval(&block) if block
14
+ context_stack.pop
15
+ head
16
+ end
17
+
18
+ def head
19
+ context_stack.first
20
+ end
21
+ def this_context
22
+ # @this_context ||= context_stack.last
23
+ context_stack.last
24
+ end
25
+ def parent
26
+ @parent ||= current_context[-1] == self ? current_context[-2] : current_context[-1]
27
+ end
28
+
29
+ def current_context
30
+ @current_context ||= context_stack[0..depth]
31
+ end
32
+ def depth
33
+ @depth ||= context_stack.size
34
+ end
35
+ def this
36
+ @this ||= self
37
+ end
38
+ def method_missing(m,*args,&block)
39
+ if block
40
+ if args.empty?
41
+ super
42
+ else
43
+ inst = args[0]
44
+ context_stack.push self
45
+ inst.instance_eval(&block)
46
+ context_stack.pop
47
+ end
48
+ else
49
+ if respond_to?(:parent) && parent != self && parent.respond_to?(m)
50
+ parent.send(m,*args,&block)
51
+ else
52
+ super
53
+ end
54
+ end
55
+ end
56
+ end
57
+ def self.included(receiver)
58
+ receiver.extend ClassMethods
59
+ receiver.send :include, InstanceMethods
60
+ end
61
+ end
data/parenting.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{parenting}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Ari Lerner"]
9
+ s.date = %q{2009-04-30}
10
+ s.description = %q{Easily add parents to classes}
11
+ s.email = ["arilerner@mac.com"]
12
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc"]
13
+ s.files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "lib/parenting.rb", "lib/parenting/base.rb", "parenting.gemspec", "script/console", "script/destroy", "script/generate", "test/test_helper.rb", "test/test_parenting.rb"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://poolpartyrb.com}
16
+ s.post_install_message = %q{PostInstall.txt}
17
+ s.rdoc_options = ["--main", "README.rdoc"]
18
+ s.require_paths = ["lib"]
19
+ s.rubyforge_project = %q{parenting}
20
+ s.rubygems_version = %q{1.3.2}
21
+ s.summary = %q{Easily add parents to classes}
22
+ s.test_files = ["test/test_helper.rb", "test/test_parenting.rb"]
23
+
24
+ if s.respond_to? :specification_version then
25
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
26
+ s.specification_version = 3
27
+
28
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
29
+ s.add_development_dependency(%q<newgem>, [">= 1.2.3"])
30
+ s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
31
+ else
32
+ s.add_dependency(%q<newgem>, [">= 1.2.3"])
33
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
34
+ end
35
+ else
36
+ s.add_dependency(%q<newgem>, [">= 1.2.3"])
37
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
38
+ end
39
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/parenting.rb'}"
9
+ puts "Loading parenting gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,5 @@
1
+ require "rubygems"
2
+ require "dslify"
3
+ require "context"
4
+ require "matchy"
5
+ require File.dirname(__FILE__) + '/../lib/parenting'
@@ -0,0 +1,117 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class Quickie
4
+ include Parenting
5
+ attr_accessor :my_name
6
+ def initialize(nm="Default", &block)
7
+ @my_name ||= nm
8
+ run_in_context(&block) if block
9
+ end
10
+ end
11
+
12
+ class QuickieTest < Test::Unit::TestCase
13
+ context "setting" do
14
+ before do
15
+ @a = Quickie.new do
16
+ $b = Quickie.new("franke") do
17
+ $c = Quickie.new("bob") do
18
+ end
19
+ end
20
+ end
21
+ end
22
+ it "should set the parents properly" do
23
+ assert_equal $c.parent, $b
24
+ assert_equal $b.parent, @a
25
+ assert_equal @a.parent, nil
26
+ end
27
+ it "should have proper depth" do
28
+ assert_equal @a.depth, 0
29
+ assert_equal $b.depth, 1
30
+ assert_equal $c.depth, 2
31
+ end
32
+ it "should have current_context" do
33
+ assert_equal $context_stack, []
34
+ assert_equal @a.current_context, []
35
+ assert_equal $b.current_context,[@a]
36
+ assert_equal $c.current_context,[@a, $b]
37
+ end
38
+ it "should set my_name on $c to frank" do
39
+ assert_equal $c.my_name, "bob"
40
+ end
41
+ it "should set my_name on $b to bob" do
42
+ assert_equal $b.my_name, "franke"
43
+ end
44
+ it "should not set my_name to is_frank on @a" do
45
+ assert_equal @a.my_name, "Default"
46
+ end
47
+ end
48
+ context "from within a module_eval" do
49
+ before(:all) do
50
+ instance_eval <<-EOE
51
+ @a = Quickie.new do
52
+ self.class.send :attr_reader, :b
53
+ @b = Quickie.new do
54
+ self.class.send :attr_reader, :c
55
+ @c = Quickie.new do
56
+ self.class.send :attr_reader, :d
57
+ $d = @d = Quickie.new do
58
+ end
59
+ end
60
+ end
61
+ end
62
+ EOE
63
+ end
64
+ it "should set the parent's properly" do
65
+ assert_equal @a.parent, nil
66
+ assert_equal @a.b.parent, @a
67
+ assert_equal @a.b.c.parent, @a.b
68
+ assert_equal @a.b.c.d.parent, @a.b.c
69
+ end
70
+ it "should set the depth" do
71
+ assert_equal @a.depth, 0
72
+ assert_equal @a.b.depth, 1
73
+ assert_equal @a.b.c.depth, 2
74
+ assert_equal @a.b.c.d.depth, 3
75
+ end
76
+ it "should have a current context" do
77
+ assert_equal @a.context_stack.size, 0
78
+ assert_equal @a.b.current_context, [@a]
79
+ assert_equal @a.b.c.current_context, [@a,@a.b]
80
+ assert_equal @a.b.c.d.current_context, [@a, @a.b, @a.b.c]
81
+ end
82
+ it "should no be weird" do
83
+ assert_equal $d, @a.b.c.d
84
+ assert_equal $d.parent, @a.b.c
85
+ assert_equal $d.parent.parent.parent, @a
86
+ end
87
+ end
88
+ context "calling a method on parent that doesn't exist on self" do
89
+ setup do
90
+ class Tiny
91
+ include Parenting
92
+ attr_reader :message
93
+ def initialize(&block)
94
+ run_in_context(&block) if block
95
+ end
96
+ end
97
+ instance_eval <<-EOE
98
+ @a = Quickie.new :outside do
99
+ self.class.send :attr_reader, :b
100
+ @b = Tiny.new do
101
+ @message = my_name
102
+ end
103
+ end
104
+ EOE
105
+ end
106
+
107
+ should "setup the parents properly" do
108
+ assert_equal @a.parent, nil
109
+ assert_equal @a.b.parent, @a
110
+ end
111
+ should "call my_name on the parent" do
112
+ assert_equal @a.b.message, :outside
113
+ end
114
+ end
115
+
116
+
117
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auser-parenting
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ari Lerner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-30 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ version:
35
+ description: Easily add parents to classes
36
+ email:
37
+ - arilerner@mac.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - History.txt
44
+ - Manifest.txt
45
+ - PostInstall.txt
46
+ - README.rdoc
47
+ files:
48
+ - History.txt
49
+ - Manifest.txt
50
+ - PostInstall.txt
51
+ - README.rdoc
52
+ - Rakefile
53
+ - lib/parenting.rb
54
+ - lib/parenting/base.rb
55
+ - parenting.gemspec
56
+ - script/console
57
+ - script/destroy
58
+ - script/generate
59
+ - test/test_helper.rb
60
+ - test/test_parenting.rb
61
+ has_rdoc: true
62
+ homepage: http://poolpartyrb.com
63
+ post_install_message: PostInstall.txt
64
+ rdoc_options:
65
+ - --main
66
+ - README.rdoc
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project: parenting
84
+ rubygems_version: 1.2.0
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Easily add parents to classes
88
+ test_files:
89
+ - test/test_helper.rb
90
+ - test/test_parenting.rb