fun_with_patterns 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWI2MTM0MDJiN2ExNWJlOTQwM2M2OTJiNzYyZGJhZDQ3YjdhMmMwYQ==
5
+ data.tar.gz: !binary |-
6
+ YjAwZDFhZDQ2NjEwODUzZTUxYWZiNmU3MDk2OWMyMDI1MmNhMjYzMw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MmUxNDc0YjMwMThjMmMxOTA5ZjFmNjkyODM1MmQ4MTMyNzVhZWNkYjg4OTVk
10
+ ZmRlZWQ1NjJjNTIzODk2ZGQ2YjRjMjZiNDAwOTdlOGQ3NGM4NWUyYmY4OTcy
11
+ OWU2N2Q5M2IzNTAyNmZlZDg2YzYwOGRmMmM2NjAwNDA3NTA0MmM=
12
+ data.tar.gz: !binary |-
13
+ YzU1ODdlNDQ0MjJjY2I1M2Y0NDQ3OGUyYjNiMGU3ZDMwMGNiOTc3M2MwN2U5
14
+ NjU0OGEzYzNkNmMzYjliZTY4YjgyMjA5YWI2N2E1NmU1OTg1OTQ1MzA1YWYz
15
+ YzRjYjU0YmYwZjU2NWE2NWQ5MWFiNTA3MzViODU4YTQ1NTExNDg=
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
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
+
14
+ end
15
+
16
+ gem "fun_with_files", "~> 0"
17
+ gem "fun_with_version_strings", "~> 0"
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Bryce Anderson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,78 @@
1
+ = fun_with_patterns
2
+
3
+ A collection of patterns that I seem to keep re-implementing. I'll be adding them here as I find time to revise and refactor. Until then, the only one I've gotten around to is...
4
+
5
+
6
+ == Loader Pattern
7
+
8
+ The Loader pattern is a good way to customize/modularize part of your app's functionality.
9
+
10
+ It assumes that you have a directory or set of directories, which include a bunch of definitions of various 'thingies' in different files. For each directory you request loaded, it will look for these 'thingies' and try to load each file. You need only define a load_item() and an <optional> loader_pattern_register_item() method. load_item() can simply call load( the_ruby_file.rb ). If you don't define a load_item() method, it will assume the default case: there is ruby code in that thar file, and I'm gonna execute it.
11
+
12
+ Say you have a bunch of tasks, scattered over three directories, written in a task-describing DSL, one task per file:
13
+
14
+ my_lib/builtin/tasks/mysql/backup.db.rb
15
+ my_lib/builtin/tasks/website/maintain.users.rb
16
+ ~/.config/mylib/tasks/email_wendy.rb
17
+ ~/.config/mylib/tasks/backup_pr0n.rb
18
+ ~/project/tasks/submit_invoice.rb
19
+
20
+ Inside each file would be some code describing the task:
21
+
22
+ Task.new( "backup:db" ) do |t|
23
+ t.run_schedule :daily
24
+ t.action "mysqldump -u{{user}} -h{{host}} {{db}}"
25
+ t.user "dbuser"
26
+ t.host "mysql.hostapalooza.com"
27
+ ... you get the idea
28
+ end
29
+
30
+ The tasks could number in the hundreds. You want your Task class to have a way of loading them, registering them so you can find them, and so on, so you can run them.
31
+
32
+ Task might look like this:
33
+
34
+ class Task
35
+ include FunWith::Patterns::Loader
36
+
37
+ alias :loader_pattern_registry_key :name
38
+
39
+ def initialize( name )
40
+ @name = name # in the example, it was "backup:db"
41
+ end
42
+ end
43
+
44
+ To load your tasks, you'd run:
45
+
46
+ Task.loader_pattern_load_from_dir( "mylib/builtin/tasks" )
47
+ Task.loader_pattern_load_from_dir( "~/.config/mylib/tasks" )
48
+ Task.loader_pattern_load_from_dir( "~/project/tasks" )
49
+
50
+ And to snag the backup.db task, you'd just say:
51
+
52
+ task = Task.loader_pattern_registry_lookup( "backup:db" )
53
+ task.run
54
+
55
+ For each directory you give it, it will:
56
+
57
+ 1. load all the .rb files in the directory/subdirectories
58
+ 2. register them into the registry.
59
+
60
+ 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
+
62
+
63
+
64
+ == Contributing to fun_with_patterns
65
+
66
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
67
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
68
+ * Fork the project.
69
+ * Start a feature/bugfix branch.
70
+ * Commit and push until you are happy with your contribution.
71
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
72
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
73
+
74
+ == Copyright
75
+
76
+ Copyright (c) 2013 Bryce Anderson. See LICENSE.txt for
77
+ further details.
78
+
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "fun_with_patterns"
18
+ gem.homepage = "http://github.com/darthschmoo/fun_with_patterns"
19
+ gem.license = "MIT"
20
+ gem.summary = "A collection of useful patterns"
21
+ gem.description = "A collection of useful patterns"
22
+ gem.email = "keeputahweird@gmail.com"
23
+ gem.authors = ["Bryce Anderson"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+
36
+ task :default => :test
37
+
38
+ require 'rdoc/task'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "fun_with_patterns #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,92 @@
1
+ module FunWith
2
+ module Patterns
3
+ module Loader
4
+ def self.included( base )
5
+ base.extend( ClassMethods )
6
+ base.loader_pattern_extension("rb") if base.loader_pattern_extension.nil?
7
+ end
8
+
9
+ module ClassMethods
10
+ # By default, looks for .rb files to evaluate
11
+ # ext => :all (or "*") to load every file from directory.
12
+ # ext => :rb (or "rb") to load all .rb files (this is default)
13
+ # call without arguments to inquire about current extension.
14
+ def loader_pattern_extension( ext = nil )
15
+ case ext
16
+ when nil
17
+ # do nothing
18
+ when "*", :all
19
+ @loader_pattern_extension = "*"
20
+ else
21
+ @loader_pattern_extension = "*.#{ext}"
22
+ end
23
+
24
+ @loader_pattern_extension
25
+ end
26
+
27
+ def loader_pattern_verbose( verbosity = nil )
28
+ @loader_pattern_verbose = verbosity unless verbosity.nil?
29
+ @loader_pattern_verbose
30
+ end
31
+
32
+ # Default behavior: read the file, evaluate it, expect a ruby object
33
+ # of the class that the loader pattern is installed on. If anything goes
34
+ # wrong (file no exist, syntax error), returns a nil.
35
+ #
36
+ # Override in your class if you need your files translated
37
+ # into objects differently.
38
+ def loader_pattern_load_item( file )
39
+ file = file.fwf_filepath
40
+ if file.file?
41
+ obj = eval( file.read )
42
+
43
+ STDOUT.puts( "Loaded file #{file}" ) if self.loader_pattern_verbose
44
+ return obj
45
+ else
46
+ STDERR.puts( "(#{self.class}) Load failed, no such file: #{file}" )
47
+ return nil
48
+ end
49
+ rescue Exception => e
50
+ STDERR.puts( "Could not load file #{file}. Reason: #{e.class.name} #{e.message}" )
51
+
52
+ if self.loader_pattern_verbose
53
+ STDERR.puts( puts e.backtrace.map{|line| "\t\t#{line}"}.join("\n") )
54
+ STDERR.puts( "" )
55
+ end
56
+
57
+ nil
58
+ end
59
+
60
+ # Default, may want to override how the registry behaves.
61
+ def loader_pattern_register_item( obj )
62
+ return nil if obj.nil?
63
+ return (@loader_pattern_registry ||= {})[ obj.loader_pattern_registry_key ] = obj
64
+ end
65
+
66
+ def loader_pattern_registry_lookup( key )
67
+ @loader_pattern_registry[key]
68
+ end
69
+
70
+
71
+ # Assumes that every file in the directory and subdirectories contain ruby code that
72
+ # will yield an object that the loader is looking for. It also automatically
73
+ # adds the resulting object to a registry.
74
+ # 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
79
+
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 )
83
+ end
84
+ end
85
+
86
+ def loader_pattern_loaded_directories
87
+ @loader_pattern_directories ||= []
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,12 @@
1
+ require 'fun_with_files'
2
+ require 'fun_with_version_strings'
3
+
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
data/test/helper.rb ADDED
@@ -0,0 +1,28 @@
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'
14
+
15
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
17
+
18
+ require 'fun_with_patterns'
19
+
20
+ require_relative 'user'
21
+ require_relative 'yaml_obj'
22
+
23
+
24
+ class Test::Unit::TestCase
25
+ end
26
+
27
+ class FunWith::Patterns::TestCase < Test::Unit::TestCase
28
+ end
@@ -0,0 +1,47 @@
1
+ require 'helper'
2
+
3
+ class TestLoaderPattern < FunWith::Patterns::TestCase
4
+ context "testing basics" do
5
+ should "have all the pieces in place" do
6
+ assert defined?( FunWith::Patterns )
7
+ assert defined?( FunWith::Patterns::Loader )
8
+ assert defined?( FunWith::Patterns::Loader::ClassMethods )
9
+ end
10
+ end
11
+
12
+ context "testing User" do
13
+ setup do
14
+ User.loader_pattern_load_from_dir( FunWith::Patterns.root( "test", "users" ) )
15
+ end
16
+
17
+ should "have all the right methods" do
18
+ for method in [ :loader_pattern_extension,
19
+ :loader_pattern_verbose,
20
+ :loader_pattern_load_item,
21
+ :loader_pattern_register_item,
22
+ :loader_pattern_load_from_dir ]
23
+ assert_respond_to( User, method )
24
+ end
25
+ end
26
+
27
+ should "load users from test/users and test/users/more" do
28
+ assert User.loader_pattern_registry_lookup("Gary Milhouse")
29
+ assert_equal 54, User.loader_pattern_registry_lookup("Gary Milhouse").age
30
+ end
31
+ end
32
+
33
+ context "testing YamlObj (non-standard loader)" do
34
+ setup do
35
+ YamlObj.loader_pattern_extension( "yaml" )
36
+ YamlObj.loader_pattern_load_from_dir( FunWith::Patterns.root( "test", "yamls" ) )
37
+ end
38
+
39
+ should "look up items in registry" do
40
+ obj = YamlObj.loader_pattern_registry_lookup("Steve's Instagram Login")
41
+ assert_not_nil obj
42
+ assert_equal "password", obj.password
43
+ end
44
+ end
45
+ end
46
+
47
+
data/test/user.rb ADDED
@@ -0,0 +1,13 @@
1
+ class User
2
+ include FunWith::Patterns::Loader
3
+
4
+ attr_accessor :name, :age
5
+
6
+ def initialize( name, age )
7
+ @name = name
8
+ @age = age
9
+ end
10
+
11
+ # For User, name will be used to look up the item in the registry.
12
+ alias :loader_pattern_registry_key :name
13
+ end
@@ -0,0 +1 @@
1
+ User.new( "Mary Masterson", 27 )
@@ -0,0 +1 @@
1
+ User.new( "Gary Milhouse", 54 )
@@ -0,0 +1 @@
1
+ User.new( "Steve Sturmond", 72 )
@@ -0,0 +1 @@
1
+ User.new( "Wanda Wimbledon", 53 )
data/test/yaml_obj.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'yaml'
2
+
3
+
4
+ # Change the behavior by overriding
5
+ class YamlObj
6
+ include FunWith::Patterns::Loader
7
+
8
+ attr_accessor :label, :password, :host, :login
9
+ alias :loader_pattern_registry_key :label
10
+
11
+ # The class initializes via yaml strings.
12
+ def self.loader_pattern_load_item( file )
13
+ obj = self.new( file.read )
14
+ self.loader_pattern_register_item( obj )
15
+ end
16
+
17
+ def initialize( yaml )
18
+ yaml = Psych.load( yaml )
19
+
20
+ @login = yaml["login"]
21
+ @password = self.decrypt( yaml["password"] )
22
+ @host = yaml["host"]
23
+ @label = yaml["label"]
24
+ end
25
+
26
+ # Warning: not production cryptography code.
27
+ def decrypt( str )
28
+ num = 13
29
+
30
+ bytes = str.split("").map(&:bytes).map(&:first)
31
+
32
+ lower_a = 'a'.bytes.first
33
+ upper_a = 'A'.bytes.first
34
+ is_lowercase = (lower_a .. (lower_a + 26))
35
+ is_uppercase = (upper_a .. (upper_a + 26))
36
+ rot_bytes = bytes.map{ |ch|
37
+ case ch
38
+ when is_lowercase
39
+ ((ch + num - lower_a) % 26 + lower_a)
40
+ when is_uppercase
41
+ ((ch + num - upper_a) % 26 + upper_a)
42
+ else
43
+ ch
44
+ end
45
+ }
46
+
47
+ rot_bytes.map(&:chr).join("")
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ ---
2
+ host: login.amazon.com
3
+ login: MikeIsNotSteve1964
4
+ password: cnffjbeq
5
+ label: Mike's Amazon Login
@@ -0,0 +1,5 @@
1
+ ---
2
+ host: services.google.com
3
+ login: MikeWillBuryYou
4
+ password: cnffjbeq
5
+ label: Mike's Google Login
@@ -0,0 +1,5 @@
1
+ ---
2
+ host: signin.instagram.com
3
+ login: StevePix1958
4
+ password: cnffjbeq
5
+ label: Steve's Instagram Login
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fun_with_patterns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Bryce Anderson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fun_with_files
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '2.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ description: A collection of useful patterns
98
+ email: keeputahweird@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files:
102
+ - LICENSE.txt
103
+ - README.rdoc
104
+ files:
105
+ - .document
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ - Rakefile
110
+ - VERSION
111
+ - lib/fun_with/patterns/loader.rb
112
+ - lib/fun_with_patterns.rb
113
+ - test/helper.rb
114
+ - test/test_loader_pattern.rb
115
+ - test/user.rb
116
+ - test/users/mary.rb
117
+ - test/users/more/gary.rb
118
+ - test/users/steve.rb
119
+ - test/users/wanda.rb
120
+ - test/yaml_obj.rb
121
+ - test/yamls/mike/mike_amazon.yaml
122
+ - test/yamls/mike/mike_gmail.yaml
123
+ - test/yamls/steve/steve_instagram.yaml
124
+ homepage: http://github.com/darthschmoo/fun_with_patterns
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.2.2
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: A collection of useful patterns
148
+ test_files: []