fun_with_patterns 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZmVjYzY0ODYzZjIxYTQxNjQ5YWYzMzYwNGMzMzBjY2ZlNjA5ZTk0ZQ==
4
+ OWZlYzlkNjQ2MzBkZjY3ZGUxOTUwZTI2MmY0MzYzNDE4MjI1MGJjNA==
5
5
  data.tar.gz: !binary |-
6
- YzRkMzZkOWMxMTI3NDdkMjYxNWY3ZjkxMGFiZTIzMTAzNTZlMzkwOQ==
6
+ ZDM0ZDUzMmY2ZTM2YmJmNTNhNDRhZjg1ODZlMDc3ZjBkOTc0NWRkOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NDA0OTllMTE0OWZmYTI2NDEyYzhhNmMyYzllM2RmMDBkNWVkYThiYjkwMjY5
10
- MmUxMDE0MmU2OGVkY2VmYjQ1Mjc3ZmNkMjIyY2I0Njg3ZDA5NGRjNzU0YzJm
11
- NjMxOGEzZTBmZGQ4ODk3NjRhYTg5OWYxNDkzNDdiNzcxOWYyZmQ=
9
+ YzRlM2IwOGMwNWNjN2RhOWJkYjNmYjgyMGU0YjgwMjE3NTU4NWViNDI2MDU2
10
+ NjY3Yzg3OTRiMzNhZTA3ZGJiY2UwNzg1YzNmMDcwMTk5NDQ1NmFmZTEwMDAx
11
+ NjU5Nzk5MDc0NDk0YTY1YTk2MzJmNDI1Y2MxMjA1YjE4M2EyMWE=
12
12
  data.tar.gz: !binary |-
13
- MWFmODI1ZmIwMzZkM2M5ZDlmNDVhY2FkNjNlOWE5ZGJlODE1ZTljODhmOTI2
14
- MTM0Yzk4NDQ3NjExODJmMTBjNjVkZDFlNGE2YTczNDNkYzJmODE3ODVhOThi
15
- YzQwNzA4MTRiNTI3NzRmMzkyZTVhMzhjNWJjZjE2N2RmM2JjMDM=
13
+ NDZiNWMyOWQxMjUyZDk5ZWUwMWZlNjJjNDVhNmQ0OTk5NjdhY2FjMGIwMzBl
14
+ ZGEyNDg2MzBlOGZkYTkxNjM1MjBiNjk5Y2M0MDBkNDVmZDA3YzQyM2Q0NTk1
15
+ NjFlOThjNTNkNGM2YjgzOTE3MjQ1NjhjM2RjMzQzM2QxYzFjNTY=
@@ -0,0 +1,12 @@
1
+ Changelog
2
+ =========
3
+
4
+ 0.0.4
5
+ -----
6
+
7
+ Added GetAndSet pattern (similar to `attr_accessor`).
8
+
9
+ 0.0.3
10
+ -----
11
+
12
+ Added Reloadable pattern.
data/Gemfile CHANGED
@@ -6,13 +6,12 @@ source "http://rubygems.org"
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
9
- gem "shoulda", "~> 3.5"
10
- gem "rdoc", "~> 3.12"
11
- gem "bundler", "~> 1.5"
12
- gem "jeweler", "~> 2.0"
13
- gem "fun_with_testing"
14
- gem "debugger"
9
+ # gem "shoulda", "~> 3.5"
10
+ # gem "rdoc", "~> 3.12"
11
+ # gem "bundler", "~> 1.5"
12
+ # gem "jeweler", "~> 2.0"
13
+ # gem "debugger", "~> 1.6"
14
+ gem 'fun_with_testing', '~> 0.0'
15
15
  end
16
16
 
17
- gem "fun_with_files", "~> 0"
18
- gem "fun_with_version_strings", "~> 0"
17
+ gem "fun_with_gems", "~> 0.0"
data/README.rdoc CHANGED
@@ -46,6 +46,10 @@ To load your tasks, you'd run:
46
46
  Task.loader_pattern_load_from_dir( "mylib/builtin/tasks" )
47
47
  Task.loader_pattern_load_from_dir( "~/.config/mylib/tasks" )
48
48
  Task.loader_pattern_load_from_dir( "~/project/tasks" )
49
+
50
+ or
51
+
52
+ Task.loader_pattern_load_from_dir( "mylib/builtin/tasks", "~/.config/mylib/tasks", "~/project/tasks" )
49
53
 
50
54
  And to snag the backup.db task, you'd just say:
51
55
 
@@ -59,6 +63,8 @@ For each directory you give it, it will:
59
63
 
60
64
  You can get some interesting behavior by overwriting individual methods. For example, loading individual configurations from YAML or JSON or XML. There's an example in the test/ folder.
61
65
 
66
+ Note: If you want to manage your own registry by redefining loader_pattern_load_from_dir( dir ), loader_pattern_register_item( item ), etc., make sure you handle and report exceptions.
67
+
62
68
 
63
69
 
64
70
  == Reloadable ==
@@ -81,6 +87,58 @@ Easy as peasy. Note, it may be buggy if the file only contains part of the defi
81
87
 
82
88
 
83
89
 
90
+ == GetAndSet ==
91
+
92
+ When active, this module behaves a lot like #attr_accessor.
93
+ The difference is, only one method is defined. Whether you're
94
+ looking up the instance_variable's value or altering it depends
95
+ on whether you pass it an argument.
96
+
97
+
98
+ FunWith::Patterns::GetAndSet.activate
99
+
100
+ class A
101
+ get_and_set( :radio, :radius, :radium )
102
+ get_and_set( :radiate )
103
+ get_and_set_boolean( :stringy, :flurmish ) # unless changed (! called or @var set some other way), defaults to false (stringy! returns false, not_stringy! returns true)
104
+ end
105
+
106
+ a = A.new
107
+
108
+ a.radius( 3.14159 ) # ==> 3.14159
109
+ a.radius() # ==> 3.14159
110
+
111
+ a.radium() # ==> nil
112
+ a.radium( "radioactive" ) # ==> "radioactive"
113
+ a.radium( "Madame Curie") # ==> "Madame Curie"
114
+ a.radium() # ==> "Madame Curie"
115
+
116
+ a.stringy! # ==> true
117
+ a.stringy? # ==> true
118
+ a.not_stringy! # ==> false
119
+ a.stringy? # ==> false
120
+
121
+ a.flurmish? # ==> false # doesn't have a default, and hasn't had one of the !s run
122
+ a.not_flurmish? # ==> true # unfortunate implementation side-effect. Do not rely on it.
123
+
124
+ That's all you need to start using #get_and_set( :varname1, :varname2 )
125
+
126
+ If you want to make all objects more extensible, you can make
127
+ it available for all objects, not just classes and modules:
128
+
129
+ FunWith::Patterns::GetAndSet.activate( Object )
130
+
131
+ class A
132
+ end
133
+
134
+ a = A.new
135
+
136
+ a.get_and_set( :radiate )
137
+ a.get_and_set_boolean( :stringy? )
138
+
139
+ They work the same, but only apply to the object, not all objects of that class.
140
+
141
+
84
142
  == Contributing to fun_with_patterns
85
143
 
86
144
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
data/Rakefile CHANGED
@@ -22,7 +22,13 @@ Jeweler::Tasks.new do |gem|
22
22
  gem.email = "keeputahweird@gmail.com"
23
23
  gem.authors = ["Bryce Anderson"]
24
24
  # dependencies defined in Gemfile
25
+
26
+ gem.files = Dir.glob( File.join( ".", "lib", "**", "*.rb" ) ) +
27
+ Dir.glob( File.join( ".", "test", "**", "*" ) ) +
28
+ %w( Gemfile Rakefile LICENSE.txt README.rdoc VERSION CHANGELOG.markdown )
29
+
25
30
  end
31
+
26
32
  Jeweler::RubygemsDotOrgTasks.new
27
33
 
28
34
  require 'rake/testtask'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -0,0 +1,50 @@
1
+ module FunWith
2
+ module Patterns
3
+ module GetAndSet
4
+ def get_and_set( *method_names )
5
+ for name in method_names
6
+ if self.is_a?(Class) || self.is_a?(Module)
7
+ eval "define_method( :#{name} ) do |*args|
8
+ self.instance_variable_set( :@#{name}, args.first ) if args.length == 1
9
+ self.instance_variable_get( :@#{name} )
10
+ end"
11
+ else
12
+ m = Module.new
13
+ m.get_and_set( *method_names )
14
+ self.extend( m )
15
+ end
16
+ end
17
+ end
18
+
19
+ # Would also like to do a boolean version which creates
20
+ # .bool? .bool! and .not_bool!
21
+
22
+ def get_and_set_boolean( *method_names )
23
+ for name in method_names
24
+ if self.is_a?(Class) || self.is_a?(Module)
25
+ eval "define_method( :#{name}? ) do
26
+ self.instance_variable_get( :@#{name} ) || false
27
+ end
28
+
29
+ define_method( :not_#{name}? ) do
30
+ ! self.#{name}?
31
+ end
32
+
33
+ define_method( :#{name}! ) do
34
+ self.instance_variable_set( :@#{name}, true )
35
+ end
36
+
37
+ define_method( :not_#{name}! ) do
38
+ self.instance_variable_set( :@#{name}, false )
39
+ end
40
+ "
41
+ else
42
+ m = Module.new
43
+ m.get_and_set_bool( *method_names )
44
+ self.extend( m )
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ module FunWith
2
+ module Patterns
3
+ module GetAndSetAPI
4
+ def activate( classes_to_activate = [Class, Module] )
5
+ classes_to_activate = [classes_to_activate] unless classes_to_activate.is_a?(Array)
6
+
7
+ for klass in classes_to_activate
8
+ klass.send( :include, FunWith::Patterns::GetAndSet )
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,47 @@
1
+ module FunWith
2
+ module Patterns
3
+ module Hooks
4
+ def add_hook_before( method, &block )
5
+ hook_pattern_install_hooks_for_method( method )
6
+ end
7
+
8
+ def add_hook_after( method, &block )
9
+ hook_pattern_install_hooks_for_method( method )
10
+
11
+ end
12
+
13
+ def hook_pattern_hooks_installed_for_method?( method )
14
+ self.respond_to?( :"#{method}_method_without_hooks" )
15
+ end
16
+
17
+ def hook_pattern_install_hooks_for_method( method )
18
+ return false if hook_pattern_hooks_installed_for_method?( method )
19
+
20
+ alias :"#{method}_method_without_hooks" :"#{method}"
21
+
22
+ @hook_pattern_hooks ||= {}
23
+ @hook_pattern_hooks[method] ||= {}
24
+ @hook_pattern_hooks[method][:before] ||= []
25
+ @hook_pattern_hooks[method][:after] ||= []
26
+
27
+
28
+ # http://stackoverflow.com/questions/4470108/when-monkey-patching-a-method-can-you-call-the-overridden-method-from-the-new-i
29
+ old_method = instance_method(method)
30
+
31
+ # define_method(:bar) do
32
+ # old_bar.bind(self).() + ' World'
33
+ # end
34
+
35
+ define_method( method ) do |*args, &block|
36
+ self.hook_pattern_run( @hook_pattern_hooks[method][:before], *args, &block )
37
+ old_method.bind( self ).call( *args, &block )
38
+ self.hook_pattern_run( @hook_pattern_hooks[method][:after], *args, &block )
39
+ end
40
+ end
41
+
42
+ def hook_pattern_run( hookset, *args, &block )
43
+ puts "running hookset"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -51,41 +51,113 @@ module FunWith
51
51
 
52
52
  if self.loader_pattern_verbose
53
53
  STDERR.puts( puts e.backtrace.map{|line| "\t\t#{line}"}.join("\n") )
54
- STDERR.puts( "" )
54
+ STDERR.puts( "\n" )
55
55
  end
56
56
 
57
57
  nil
58
58
  end
59
59
 
60
60
  # Default, may want to override how the registry behaves.
61
- def loader_pattern_register_item( obj )
61
+ # If you don't provide a key argument, then the object needs to
62
+ # respond to .loader_pattern_registry_key()
63
+ def loader_pattern_register_item( obj, key = nil )
62
64
  return nil if obj.nil?
63
- return (@loader_pattern_registry ||= {})[ obj.loader_pattern_registry_key ] = obj
65
+ @loader_pattern_registry ||= {}
66
+
67
+ key = obj.loader_pattern_registry_key if key.nil?
68
+
69
+ if loader_pattern_is_item_registerable?( obj )
70
+ if @loader_pattern_warn_on_key_changes && loader_pattern_registry_lookup( key )
71
+ warn( "class #{self} is replacing lookup key #{key.inspect}" )
72
+ end
73
+
74
+ return @loader_pattern_registry[ key ] = obj
75
+ else
76
+ warn( "#{obj} is not an instance of a registerable class. Registerable classes: #{self.loader_pattern_only_register_classes.inspect}" )
77
+ return nil
78
+ end
64
79
  end
65
80
 
66
81
  def loader_pattern_registry_lookup( key )
82
+ @loader_pattern_registry ||= {}
67
83
  @loader_pattern_registry[key]
68
84
  end
69
85
 
86
+ def loader_pattern_registry
87
+ @loader_pattern_registry
88
+ end
89
+
90
+ def loader_pattern_only_register_classes( *args )
91
+ if args.length > 0
92
+ @loader_pattern_only_register_classes = args
93
+ end
94
+
95
+ @loader_pattern_only_register_classes || []
96
+ end
97
+
98
+ def loader_pattern_is_item_registerable?( item )
99
+ return true if loader_pattern_only_register_classes.fwf_blank?
100
+
101
+ for klass in @loader_pattern_only_register_classes
102
+ return true if item.is_a?( klass )
103
+ end
104
+
105
+ return false
106
+ end
107
+
70
108
 
71
109
  # Assumes that every file in the directory and subdirectories contain ruby code that
72
110
  # will yield an object that the loader is looking for. It also automatically
73
111
  # adds the resulting object to a registry.
74
112
  # You may want to override this if you're looking for different behavior.
75
- def loader_pattern_load_from_dir( dir )
76
- dir = dir.fwf_filepath
77
- @loader_pattern_directories ||= []
78
- @loader_pattern_directories << dir
113
+ def loader_pattern_load_from_dir( *dirs )
114
+ for dir in dirs
115
+ dir = dir.fwf_filepath
116
+ @loader_pattern_directories ||= []
117
+ @loader_pattern_directories << dir
79
118
 
80
- for file in dir.glob( "**", self.loader_pattern_extension )
81
- obj = self.loader_pattern_load_item( file )
82
- self.loader_pattern_register_item( obj )
119
+ for file in dir.glob( "**", self.loader_pattern_extension )
120
+ obj = self.loader_pattern_load_item( file )
121
+ self.loader_pattern_register_item( obj ) if self.loader_pattern_is_item_registerable?( obj )
122
+ end
83
123
  end
84
124
  end
85
125
 
86
126
  def loader_pattern_loaded_directories
87
127
  @loader_pattern_directories ||= []
88
128
  end
129
+
130
+ def loader_pattern_configure( *args )
131
+ for arg in args
132
+ case arg
133
+ when :bracketwise_lookup
134
+ self.class_eval do
135
+ def self.[]( key )
136
+ loader_pattern_registry_lookup( key )
137
+ end
138
+
139
+ def self.[]=( key, val )
140
+ loader_pattern_register_item( val, key )
141
+ end
142
+ end
143
+ when :warn_on_key_changes
144
+ @loader_pattern_warn_on_key_changes = true
145
+ when :dont_warn_on_key_changes
146
+ @loader_pattern_warn_on_key_changes = false
147
+ when Hash
148
+ for key, val in arg
149
+ case key
150
+ when :key
151
+ self.class_eval do
152
+ eval( "alias :loader_pattern_registry_key #{val.inspect}" )
153
+ end
154
+ when :verbose
155
+ self.loader_pattern_verbose( val )
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
89
161
  end
90
162
  end
91
163
  end
@@ -0,0 +1,33 @@
1
+ warn( "Something wrong with the Reloadable class. Flaky. Not recommended for use." )
2
+ module FunWith
3
+ module Patterns
4
+ # A bare-bones reloading system. Useful when the entire file defines exactly one
5
+ # class or module, with no dependencies or side-effects.
6
+ module Reloadable
7
+ def reload!
8
+ FunWith::Patterns::Reloadable.reload_class( self )
9
+ end
10
+
11
+ def reloader_filepath
12
+ @reloader_filepath
13
+ end
14
+ end
15
+
16
+ module MakeInstancesReloadable
17
+ def reloadable!
18
+ self.extend( FunWith::Patterns::Reloadable )
19
+ kaller = caller.first.gsub(/:\d+:in.*/, '')
20
+ @reloader_filepath = kaller.fwf_filepath.expand
21
+ end
22
+ end
23
+
24
+ module ClassReloaderMethod
25
+ def reload_class( klass )
26
+ if file = klass.reloader_filepath
27
+ Object.send( :remove_const, klass.name.to_sym )
28
+ file.load
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,16 +1,10 @@
1
- require 'fun_with_files'
2
- require 'fun_with_version_strings'
1
+ require 'fun_with_gems'
3
2
 
4
- module FunWith
5
- module Patterns
6
- end
7
- end
8
-
9
- FunWith::Files::RootPath.rootify( FunWith::Patterns, __FILE__.fwf_filepath.dirname.up )
10
- FunWith::VersionStrings.version( FunWith::Patterns )
11
-
12
- FunWith::Patterns.root("lib", "fun_with").requir
3
+ FunWith::Gems.make_gem_fun( "FunWith::Patterns" )
13
4
 
14
5
  Class.send( :include, FunWith::Patterns::MakeInstancesReloadable )
15
6
  Module.send( :include, FunWith::Patterns::MakeInstancesReloadable )
16
- FunWith::Patterns::Reloadable.extend( FunWith::Patterns::ClassReloaderMethod )
7
+ FunWith::Patterns::Reloadable.extend( FunWith::Patterns::ClassReloaderMethod )
8
+
9
+ # Activate Object#get_and_set / Module#get_and_set by calling GetAndSet.activate
10
+ FunWith::Patterns::GetAndSet.extend( FunWith::Patterns::GetAndSetAPI )
data/test/helper.rb CHANGED
@@ -1,31 +1,32 @@
1
- require 'rubygems'
2
- require 'bundler'
3
- require 'fun_with_testing'
4
- require 'debugger'
5
-
6
- begin
7
- Bundler.setup(:default, :development)
8
- rescue Bundler::BundlerError => e
9
- $stderr.puts e.message
10
- $stderr.puts "Run `bundle install` to install missing gems"
11
- exit e.status_code
12
- end
13
-
14
- require 'test/unit'
15
- require 'shoulda'
1
+ # require 'rubygems'
2
+ # require 'bundler'
3
+ #
4
+ # begin
5
+ # Bundler.setup(:default, :development)
6
+ # rescue Bundler::BundlerError => e
7
+ # $stderr.puts e.message
8
+ # $stderr.puts "Run `bundle install` to install missing gems"
9
+ # exit e.status_code
10
+ # end
11
+
12
+ # require 'test/unit'
13
+ # require 'shoulda'
16
14
 
17
15
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
18
16
  $LOAD_PATH.unshift(File.dirname(__FILE__))
19
17
 
18
+ require 'fun_with_testing'
20
19
  require 'fun_with_patterns'
21
20
 
22
21
  require_relative 'user'
23
22
  require_relative 'yaml_obj'
24
23
 
25
24
 
26
- class Test::Unit::TestCase
27
- end
25
+ # class Test::Unit::TestCase
26
+ # end
28
27
 
29
- class FunWith::Patterns::TestCase < Test::Unit::TestCase
28
+ class FunWith::Patterns::TestCase < FunWith::Testing::TestCase
29
+ self.gem_to_test = FunWith::Patterns
30
+
30
31
  include FunWith::Testing::Assertions::Basics
31
32
  end
@@ -0,0 +1,7 @@
1
+ class MyReloadable
2
+ reloadable!
3
+
4
+ def square( x )
5
+ x * x
6
+ end
7
+ end
@@ -0,0 +1,77 @@
1
+ require 'helper'
2
+
3
+ FunWith::Patterns::GetAndSet.activate
4
+
5
+ class TestGetAndSet < FunWith::Patterns::TestCase
6
+ context "basics" do
7
+ should "be plumbed correctly" do
8
+ assert FunWith::Patterns::GetAndSet.respond_to?(:activate), "API not installed properly"
9
+ end
10
+ end
11
+
12
+ context "trial run" do
13
+ should "get and set" do
14
+ c = Class.new
15
+ c.get_and_set( :radio, :radius, :radium )
16
+ c.get_and_set( :radiate )
17
+ c.get_and_set_boolean( :stringy, :flurmish )
18
+
19
+ o = c.new
20
+
21
+ pi = 3.14159
22
+
23
+ assert_nil o.radius
24
+ assert_equal pi, o.radius( pi ) # ==> 3.14159
25
+ assert_equal pi, o.radius() # ==> 3.14159
26
+ assert_nil o.radium() # ==> nil
27
+
28
+ v1 = "radioactive"
29
+ v2 = "Madame Curie"
30
+
31
+ assert_equal v1, o.radium( v1 ) # ==> "radioactive"
32
+ assert_equal v2, o.radium( v2 ) # ==> "Madame Curie"
33
+ assert_equal v2, o.radium() # ==> "Madame Curie"
34
+
35
+ assert_false o.stringy?
36
+ o.stringy!
37
+ assert_true o.stringy?
38
+ assert_false o.not_stringy!
39
+ assert_true !o.stringy?
40
+
41
+ assert_false o.flurmish?
42
+ assert_true o.not_flurmish?
43
+ end
44
+
45
+ should "get and set 2 : module boogaloo" do
46
+ m = Module.new
47
+ c = Class.new
48
+
49
+ c.send( :include, m )
50
+
51
+ m.get_and_set( :radio, :radius, :radium )
52
+ m.get_and_set( :radiate )
53
+
54
+ o = c.new
55
+
56
+ assert_respond_to( o, :radio )
57
+ assert_respond_to( o, :radius )
58
+ assert_respond_to( o, :radium )
59
+ assert_respond_to( o, :radiate )
60
+
61
+
62
+ pi = 3.14159
63
+
64
+ assert_nil o.radius
65
+ assert_equal pi, o.radius( pi ) # ==> 3.14159
66
+ assert_equal pi, o.radius() # ==> 3.14159
67
+ assert_nil o.radium() # ==> nil
68
+
69
+ v1 = "radioactive"
70
+ v2 = "Madame Curie"
71
+
72
+ assert_equal v1, o.radium( v1 ) # ==> "radioactive"
73
+ assert_equal v2, o.radium( v2 ) # ==> "Madame Curie"
74
+ assert_equal v2, o.radium() # ==> "Madame Curie"
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ class TestHooksPattern < FunWith::Patterns::TestCase
4
+ _context "first try" do
5
+ should "set up a class with hooks" do
6
+ class A
7
+ include FunWith::Patterns::Hooks
8
+
9
+ def called_x_times()
10
+ @called_x_times ||= 0
11
+ @called_x_times
12
+ end
13
+
14
+ add_hook_before( :called_x_times ) do
15
+ @called_x_times ||= 0
16
+ @called_x_times += 1
17
+ end
18
+ end
19
+
20
+ a = A.new
21
+
22
+ assert_one( a.called_x_times() )
23
+ end
24
+ end
25
+ end
@@ -12,6 +12,7 @@ class TestLoaderPattern < FunWith::Patterns::TestCase
12
12
  context "testing User" do
13
13
  setup do
14
14
  User.loader_pattern_load_from_dir( FunWith::Patterns.root( "test", "users" ) )
15
+ User2.loader_pattern_load_from_dir( FunWith::Patterns.root( "test", "users" ) )
15
16
  end
16
17
 
17
18
  should "have all the right methods" do
@@ -19,7 +20,8 @@ class TestLoaderPattern < FunWith::Patterns::TestCase
19
20
  :loader_pattern_verbose,
20
21
  :loader_pattern_load_item,
21
22
  :loader_pattern_register_item,
22
- :loader_pattern_load_from_dir ]
23
+ :loader_pattern_load_from_dir,
24
+ :loader_pattern_configure ]
23
25
  assert_respond_to( User, method )
24
26
  end
25
27
  end
@@ -28,6 +30,16 @@ class TestLoaderPattern < FunWith::Patterns::TestCase
28
30
  assert User.loader_pattern_registry_lookup("Gary Milhouse")
29
31
  assert_equal 54, User.loader_pattern_registry_lookup("Gary Milhouse").age
30
32
  end
33
+
34
+ should "lookup via brackets" do
35
+ User.loader_pattern_configure( :bracketwise_lookup )
36
+ assert_equal 54, User["Gary Milhouse"].age
37
+
38
+ # User2 class is already configured for bracketwise lookup
39
+ m = User2["Gary Milhouse"]
40
+ assert_kind_of( User, m )
41
+ assert_equal 54, m.age
42
+ end
31
43
  end
32
44
 
33
45
  context "testing YamlObj (non-standard loader)" do
@@ -0,0 +1,33 @@
1
+ require 'helper'
2
+
3
+ class TestReloadablePattern < FunWith::Patterns::TestCase
4
+ context "testing basics" do
5
+ setup do
6
+ Class.send( :include, FunWith::Patterns::Reloadable )
7
+ assert_has_instance_method( Class, :reload! )
8
+ assert_has_instance_method( Class, :reloadable! )
9
+
10
+ assert_respond_to( Object, :reload!)
11
+ assert_respond_to( Object, :reloadable!)
12
+ end
13
+
14
+ should "reload MyReloadable" do
15
+ refute defined?(MyReloadable)
16
+ FunWith::Patterns.root( "test", "reloadable", "my_reloadable.rb" ).requir
17
+ assert defined?(MyReloadable)
18
+ assert_respond_to( MyReloadable.new, :square )
19
+
20
+ MyReloadable.class_eval do
21
+ remove_method :square
22
+ end
23
+
24
+ refute_respond_to( MyReloadable.new, :square )
25
+
26
+ MyReloadable.reload!
27
+
28
+ assert defined?(MyReloadable)
29
+
30
+ assert_respond_to( MyReloadable.new, :square )
31
+ end
32
+ end
33
+ end
data/test/user.rb CHANGED
@@ -11,3 +11,15 @@ class User
11
11
  # For User, name will be used to look up the item in the registry.
12
12
  alias :loader_pattern_registry_key :name
13
13
  end
14
+
15
+ class User2
16
+ attr_accessor :name, :age
17
+
18
+ include FunWith::Patterns::Loader
19
+ loader_pattern_configure( :bracketwise_lookup, {:key => :name} )
20
+
21
+ def initialize( name, age )
22
+ @name = name
23
+ @age = age
24
+ end
25
+ end
metadata CHANGED
@@ -1,127 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fun_with_patterns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryce Anderson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-28 00:00:00.000000000 Z
11
+ date: 2014-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: fun_with_files
14
+ name: fun_with_gems
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '0.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '0.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: fun_with_version_strings
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ~>
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
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: shoulda
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ~>
46
- - !ruby/object:Gem::Version
47
- version: '3.5'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ~>
53
- - !ruby/object:Gem::Version
54
- version: '3.5'
55
- - !ruby/object:Gem::Dependency
56
- name: rdoc
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: '3.12'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ~>
67
- - !ruby/object:Gem::Version
68
- version: '3.12'
69
- - !ruby/object:Gem::Dependency
70
- name: bundler
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ~>
74
- - !ruby/object:Gem::Version
75
- version: '1.5'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ~>
81
- - !ruby/object:Gem::Version
82
- version: '1.5'
83
- - !ruby/object:Gem::Dependency
84
- name: jeweler
28
+ name: fun_with_testing
85
29
  requirement: !ruby/object:Gem::Requirement
86
30
  requirements:
87
31
  - - ~>
88
32
  - !ruby/object:Gem::Version
89
- version: '2.0'
33
+ version: '0.0'
90
34
  type: :development
91
35
  prerelease: false
92
36
  version_requirements: !ruby/object:Gem::Requirement
93
37
  requirements:
94
38
  - - ~>
95
39
  - !ruby/object:Gem::Version
96
- version: '2.0'
97
- - !ruby/object:Gem::Dependency
98
- name: fun_with_testing
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ! '>='
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ! '>='
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: debugger
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ! '>='
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ! '>='
123
- - !ruby/object:Gem::Version
124
- version: '0'
40
+ version: '0.0'
125
41
  description: A collection of useful patterns
126
42
  email: keeputahweird@gmail.com
127
43
  executables: []
@@ -130,25 +46,33 @@ extra_rdoc_files:
130
46
  - LICENSE.txt
131
47
  - README.rdoc
132
48
  files:
133
- - .document
49
+ - ./lib/fun_with/patterns/get_and_set.rb
50
+ - ./lib/fun_with/patterns/get_and_set_api.rb
51
+ - ./lib/fun_with/patterns/hooks.rb
52
+ - ./lib/fun_with/patterns/loader.rb
53
+ - ./lib/fun_with/patterns/reloadable.rb
54
+ - ./lib/fun_with_patterns.rb
55
+ - ./test/helper.rb
56
+ - ./test/reloadable/my_reloadable.rb
57
+ - ./test/test_get_and_set.rb
58
+ - ./test/test_hooks.rb
59
+ - ./test/test_loader_pattern.rb
60
+ - ./test/test_reloadable_pattern.rb
61
+ - ./test/user.rb
62
+ - ./test/users/mary.rb
63
+ - ./test/users/more/gary.rb
64
+ - ./test/users/steve.rb
65
+ - ./test/users/wanda.rb
66
+ - ./test/yaml_obj.rb
67
+ - ./test/yamls/mike/mike_amazon.yaml
68
+ - ./test/yamls/mike/mike_gmail.yaml
69
+ - ./test/yamls/steve/steve_instagram.yaml
70
+ - CHANGELOG.markdown
134
71
  - Gemfile
135
72
  - LICENSE.txt
136
73
  - README.rdoc
137
74
  - Rakefile
138
75
  - VERSION
139
- - lib/fun_with/patterns/loader.rb
140
- - lib/fun_with_patterns.rb
141
- - test/helper.rb
142
- - test/test_loader_pattern.rb
143
- - test/user.rb
144
- - test/users/mary.rb
145
- - test/users/more/gary.rb
146
- - test/users/steve.rb
147
- - test/users/wanda.rb
148
- - test/yaml_obj.rb
149
- - test/yamls/mike/mike_amazon.yaml
150
- - test/yamls/mike/mike_gmail.yaml
151
- - test/yamls/steve/steve_instagram.yaml
152
76
  homepage: http://github.com/darthschmoo/fun_with_patterns
153
77
  licenses:
154
78
  - MIT
data/.document DELETED
@@ -1,5 +0,0 @@
1
- lib/**/*.rb
2
- bin/*
3
- -
4
- features/**/*.feature
5
- LICENSE.txt