fun_with_patterns 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/CHANGELOG.markdown +9 -0
- data/README.rdoc +18 -19
- data/VERSION +1 -1
- data/lib/fun_with/patterns/loader/class_methods.rb +16 -2
- data/lib/fun_with/patterns/loader/object_api.rb +17 -0
- data/lib/fun_with/patterns/loader.rb +2 -0
- data/lib/fun_with_patterns.rb +1 -0
- data/test/test_loader_pattern.rb +11 -6
- data/test/user.rb +8 -6
- data/test/users/eval/mary.rb +1 -1
- data/test/users/eval/more/gary.rb +1 -1
- data/test/users/eval/steve.rb +1 -1
- data/test/users/eval/wanda.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NDYzOWM3MmIyOTRmMWRiZTYxNWExNjc0MGJkNDE2MzcyOTIwYzQ3Zg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MDBhZTdlYTNkMTAwY2MwMDE0ZGQ0MDljYjlkNjdlMTE1MGE2Njk2Yg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NzAyN2ZkNDVkMzBhMTc1NjkzZWE2ZDc2NTczMzAwN2JiMzcwOWZkMzQ0NDY0
|
10
|
+
OTM4ZWVjODc4MzE4OGI5MjNjMzRmMWI4M2IwYzcyN2JiYjY4ZTJkYzQ3Mjg1
|
11
|
+
ZWQxN2MwM2Y5Mjk0OGFkNzZlYjU0YTY5YmEyMmVjYjIxZGM1YTM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NzFiZjIzMzVmMzAxNmZiZGU1Mzk2NTEyMmEwMjEyMDc1MzZlZWYwYjA2YTBi
|
14
|
+
OWRkN2NhMzRkZDgyMDcxZDg1YjVhMTc0Mjk4ZWFiNWE0ODMzNWVkYzFlZjBi
|
15
|
+
MjYyNDg1NTcyOWI0YjVhM2UwYzE0NzUwNDViYzBiNTY4MzRkMTk=
|
data/CHANGELOG.markdown
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
Changelog
|
2
2
|
=========
|
3
3
|
|
4
|
+
0.0.6
|
5
|
+
-----
|
6
|
+
|
7
|
+
Improved Loader.
|
8
|
+
|
9
|
+
* Default behavior infers the lookup key from the filename that the configuration was loaded into.
|
10
|
+
* No longer have to explicitly include FunWith::Patterns::Loader in the class/module.
|
11
|
+
|
12
|
+
|
4
13
|
0.0.5
|
5
14
|
-----
|
6
15
|
|
data/README.rdoc
CHANGED
@@ -79,7 +79,7 @@ More example code!
|
|
79
79
|
include FunWith::Patterns::Loader
|
80
80
|
loader_pattern_configure( :bracketwise_lookup,
|
81
81
|
:warn_on_key_change,
|
82
|
-
{ :verbose => true, :
|
82
|
+
{ :verbose => true, :load_style => } )
|
83
83
|
end
|
84
84
|
|
85
85
|
loader_pattern_configure is a quickie method for setting up a variety of behaviors. Options so far:
|
@@ -87,7 +87,8 @@ loader_pattern_configure is a quickie method for setting up a variety of behavio
|
|
87
87
|
:bracketwise_lookup : instead of calling Klass.loader_pattern_registry_lookup( "string" )
|
88
88
|
you can just call Klass["string"]
|
89
89
|
|
90
|
-
{:key => :<sym>} : loader determines registry key by calling this method
|
90
|
+
{:key => :<sym>} : loader determines registry key by calling this method. If no key is provided, then
|
91
|
+
it infers a key by using "directory:subdirectory:filename_without_extension"
|
91
92
|
|
92
93
|
:warn_on_key_changes : If a newly loaded item has an existing registry key, it prints a warning as it overwrites
|
93
94
|
|
@@ -117,27 +118,25 @@ The styles need some explanation:
|
|
117
118
|
- name: Boris
|
118
119
|
|
119
120
|
would end up calling klass_object.name=( "Boris" )
|
121
|
+
|
122
|
+
<A MODULE> : Extends the target with the provided module. For example,
|
123
|
+
|
124
|
+
module RandomLoaderStyle
|
125
|
+
def loader_pattern_load_item( file )
|
126
|
+
self.loader_pattern_rescue_failing_item_load( file ) do
|
127
|
+
obj = WeirdObject.factory( :random_seed => file.read )
|
128
|
+
|
129
|
+
return obj
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
class RandomObjects
|
120
135
|
|
136
|
+
end
|
121
137
|
|
122
|
-
TODO: I feel like a default label should maybe come out of the filename (zero-config and all that), but haven't built it.
|
123
|
-
|
124
|
-
== Reloadable ==
|
125
|
-
|
126
|
-
You're in the command line, developing a new class, and you find yourself forever restarting the whole project to affect changes, or constantly copy-pasting the class into the command line. Tired? Frustrated? Let .reloadable! help! Just put the method inside your class / module definition, and call Klass.reload! whenever you like.
|
127
138
|
|
128
|
-
require 'fun_with_patterns'
|
129
139
|
|
130
|
-
module SausageFest
|
131
|
-
class BratwurstCounter
|
132
|
-
reloadable!
|
133
|
-
|
134
|
-
def initialize( initial_brat_count )
|
135
|
-
... and so on
|
136
|
-
|
137
|
-
|
138
|
-
(irb) >> SausageFest::BratwurstCounter.reload!
|
139
|
-
|
140
|
-
Easy as peasy. Note, it may be buggy if the file only contains part of the definition, defines other classes as well, or performs other actions. Use at your own risk, and at great peril to your coworkers.
|
141
140
|
|
142
141
|
|
143
142
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.6
|
@@ -45,7 +45,7 @@ module FunWith
|
|
45
45
|
nil
|
46
46
|
end
|
47
47
|
|
48
|
-
# Default, may want to override how the registry behaves.
|
48
|
+
# Default method, user of the class may want to override how the registry behaves.
|
49
49
|
# If you don't provide a key argument, then the object needs to
|
50
50
|
# respond to .loader_pattern_registry_key()
|
51
51
|
def loader_pattern_register_item( obj, key = nil )
|
@@ -56,6 +56,7 @@ module FunWith
|
|
56
56
|
if obj.respond_to?( :loader_pattern_registry_key )
|
57
57
|
key = obj.loader_pattern_registry_key
|
58
58
|
else
|
59
|
+
# inferring a default key from the filepath is handled by the load_from_dir() method
|
59
60
|
raise "#{self.class} not registered. No registry key given, and object does not respond to .loader_pattern_registry_key()."
|
60
61
|
end
|
61
62
|
end
|
@@ -112,7 +113,18 @@ module FunWith
|
|
112
113
|
|
113
114
|
for file in dir.glob( :ext => self.loader_pattern_extension, :recurse => true )
|
114
115
|
obj = self.loader_pattern_load_item( file )
|
115
|
-
|
116
|
+
|
117
|
+
# checks to see if you're only allowed to register objects of certain types
|
118
|
+
if self.loader_pattern_is_item_registerable?( obj )
|
119
|
+
if obj.respond_to?( :loader_pattern_registry_key )
|
120
|
+
self.loader_pattern_register_item( obj )
|
121
|
+
else
|
122
|
+
# Generate a default key from the filename
|
123
|
+
chunks = (file.relative_path_from( dir ).dirname.split << file.basename_no_ext).map(&:to_s).reject{|s| s == "." }
|
124
|
+
key = chunks.join(":")
|
125
|
+
self.loader_pattern_register_item( obj, key )
|
126
|
+
end
|
127
|
+
end
|
116
128
|
end
|
117
129
|
end
|
118
130
|
end
|
@@ -168,6 +180,8 @@ module FunWith
|
|
168
180
|
when :yaml
|
169
181
|
self.extend( LoadingStyles::YAML )
|
170
182
|
self.loader_pattern_extension( [:yml, :yaml] )
|
183
|
+
when Module
|
184
|
+
self.extend( val )
|
171
185
|
else
|
172
186
|
raise "Unknown Loader loading style: #{val.inspect}"
|
173
187
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module FunWith
|
2
|
+
module Patterns
|
3
|
+
module Loader
|
4
|
+
|
5
|
+
# To simplify installing the loader pattern, loader_pattern_configure() is now going to be
|
6
|
+
# callable on all objects by default, and will include FunWith::Patterns::Loader
|
7
|
+
module ObjectAPI
|
8
|
+
def loader_pattern_configure( *args )
|
9
|
+
include FunWith::Patterns::Loader
|
10
|
+
|
11
|
+
# hoping (vainly) that when the include finishes, loader_pattern_configure() is now a different method
|
12
|
+
loader_pattern_configure( *args )
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -3,6 +3,8 @@ module FunWith
|
|
3
3
|
module Loader
|
4
4
|
def self.included( base )
|
5
5
|
base.extend( ClassMethods )
|
6
|
+
|
7
|
+
# set a few defaults
|
6
8
|
base.extend( LoadingStyles::Eval ) # provides a default load_item method
|
7
9
|
base.loader_pattern_extension( :rb ) if base.loader_pattern_extension.nil?
|
8
10
|
end
|
data/lib/fun_with_patterns.rb
CHANGED
data/test/test_loader_pattern.rb
CHANGED
@@ -11,12 +11,13 @@ class TestLoaderPattern < FunWith::Patterns::TestCase
|
|
11
11
|
|
12
12
|
context "testing User" do
|
13
13
|
setup do
|
14
|
-
|
14
|
+
User1.loader_pattern_load_from_dir( FunWith::Patterns.root( "test", "users", "eval" ) )
|
15
15
|
User2.loader_pattern_load_from_dir( FunWith::Patterns.root( "test", "users", "eval" ) )
|
16
16
|
User3.loader_pattern_load_from_dir( FunWith::Patterns.root( "test", "users", "instance_exec" ) )
|
17
17
|
User4.loader_pattern_load_from_dir( FunWith::Patterns.root( "test", "users", "yaml" ) )
|
18
|
+
User5.loader_pattern_load_from_dir( FunWith::Patterns.root( "test", "users", "yaml" ) )
|
18
19
|
|
19
|
-
@user_classes = [
|
20
|
+
@user_classes = [User1, User2, User3, User4, User5]
|
20
21
|
end
|
21
22
|
|
22
23
|
should "have all the right methods" do
|
@@ -33,19 +34,23 @@ class TestLoaderPattern < FunWith::Patterns::TestCase
|
|
33
34
|
end
|
34
35
|
|
35
36
|
should "load users from test/users into various classes" do
|
36
|
-
for klass in @user_classes
|
37
|
+
for klass in @user_classes[0..3]
|
37
38
|
assert klass.loader_pattern_registry_lookup("Gary Milhouse"), "#{klass} did not load Gary. Poor Gary."
|
38
39
|
assert_equal 54, klass.loader_pattern_registry_lookup("Gary Milhouse").age, "#{klass} did not load Gary with proper age data."
|
39
40
|
end
|
41
|
+
|
42
|
+
# User5 uses different keys, so handling separately
|
43
|
+
assert_equal 53, User5["wanda"].age
|
44
|
+
assert_equal "Wanda Wimbledon", User5["wanda"].name
|
40
45
|
end
|
41
46
|
|
42
47
|
should "lookup via brackets" do
|
43
|
-
|
44
|
-
assert_equal 54,
|
48
|
+
User1.loader_pattern_configure( :bracketwise_lookup )
|
49
|
+
assert_equal 54, User1["Gary Milhouse"].age
|
45
50
|
|
46
51
|
# User2 class is already configured for bracketwise lookup
|
47
52
|
m = User2["Gary Milhouse"]
|
48
|
-
assert_kind_of(
|
53
|
+
assert_kind_of( User1, m )
|
49
54
|
assert_equal 54, m.age
|
50
55
|
end
|
51
56
|
end
|
data/test/user.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
class
|
2
|
-
|
3
|
-
|
1
|
+
class User1
|
2
|
+
loader_pattern_configure()
|
4
3
|
attr_accessor :name, :age
|
5
4
|
|
6
5
|
def initialize( name, age )
|
@@ -15,7 +14,6 @@ end
|
|
15
14
|
class User2
|
16
15
|
attr_accessor :name, :age
|
17
16
|
|
18
|
-
include FunWith::Patterns::Loader
|
19
17
|
loader_pattern_configure( :bracketwise_lookup, {:key => :name} )
|
20
18
|
|
21
19
|
def initialize( name, age )
|
@@ -29,7 +27,6 @@ class User3
|
|
29
27
|
|
30
28
|
get_and_set :name, :age
|
31
29
|
|
32
|
-
include FunWith::Patterns::Loader
|
33
30
|
loader_pattern_configure( :bracketwise_lookup,
|
34
31
|
{ :key => :name },
|
35
32
|
{ :style => :instance_exec } # Create object, run code in configuration file inside the object's context.
|
@@ -40,10 +37,15 @@ end
|
|
40
37
|
class User4
|
41
38
|
attr_accessor :name, :age
|
42
39
|
|
43
|
-
include FunWith::Patterns::Loader
|
44
40
|
loader_pattern_configure( :bracketwise_lookup,
|
45
41
|
{ :key => :name },
|
46
42
|
{ :style => :yaml }
|
47
43
|
)
|
48
44
|
end
|
45
|
+
|
46
|
+
class User5
|
47
|
+
attr_accessor :name, :age
|
48
|
+
|
49
|
+
loader_pattern_configure( :bracketwise_lookup, :style => :yaml )
|
50
|
+
end
|
49
51
|
|
data/test/users/eval/mary.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
User1.new( "Mary Masterson", 27 )
|
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
User1.new( "Gary Milhouse", 54 )
|
data/test/users/eval/steve.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
User1.new( "Steve Sturmond", 72 )
|
data/test/users/eval/wanda.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
User1.new( "Wanda Wimbledon", 53 )
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fun_with_patterns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bryce Anderson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fun_with_gems
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- ./lib/fun_with/patterns/loader/loading_styles/eval.rb
|
61
61
|
- ./lib/fun_with/patterns/loader/loading_styles/instance_exec.rb
|
62
62
|
- ./lib/fun_with/patterns/loader/loading_styles/yaml.rb
|
63
|
+
- ./lib/fun_with/patterns/loader/object_api.rb
|
63
64
|
- ./lib/fun_with_patterns.rb
|
64
65
|
- ./test/helper.rb
|
65
66
|
- ./test/test_get_and_set.rb
|