perennial 0.2.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,95 @@
1
+ require 'yaml'
2
+
3
+ module Perennial
4
+ class Settings
5
+
6
+ cattr_accessor :configuration, :log_level, :verbose, :daemon
7
+
8
+ @@verbose = false
9
+ @@log_level = :info
10
+ @@daemon = false
11
+
12
+ class << self
13
+
14
+ def daemon?
15
+ !!@@daemon
16
+ end
17
+
18
+ def verbose?
19
+ !!@@verbose
20
+ end
21
+
22
+ def root=(path)
23
+ @@root = File.expand_path(path.to_str)
24
+ end
25
+
26
+ def root
27
+ @@root ||= File.expand_path(File.dirname(__FILE__) / ".." / "..")
28
+ end
29
+
30
+ def library_root=(path)
31
+ @@library_root = File.expand_path(path.to_str)
32
+ end
33
+
34
+ def library_root
35
+ @@library_root ||= File.expand_path(File.dirname(__FILE__) / ".." / "..")
36
+ end
37
+
38
+ def setup?
39
+ @@setup ||= false
40
+ end
41
+
42
+ def setup(options = {})
43
+ self.setup!(options) unless setup?
44
+ end
45
+
46
+ def setup!(options = {})
47
+ @@configuration = {}
48
+ settings_file = root / "config" / "settings.yml"
49
+ if File.exist?(settings_file)
50
+ loaded_yaml = YAML.load(File.read(settings_file))
51
+ @@configuration.merge! loaded_yaml["default"]
52
+ end
53
+ @@configuration.merge! options
54
+ @@configuration.symbolize_keys!
55
+ # Generate a module
56
+ mod = generate_settings_accessor_mixin
57
+ extend mod
58
+ include mod
59
+ @@setup = true
60
+ end
61
+
62
+ def [](key)
63
+ self.setup
64
+ return self.configuration[key.to_sym]
65
+ end
66
+
67
+ def []=(key, value)
68
+ self.setup
69
+ self.configuration[key.to_sym] = value
70
+ return value
71
+ end
72
+
73
+ def to_hash
74
+ self.configuration.dup
75
+ end
76
+
77
+ protected
78
+
79
+ def generate_settings_accessor_mixin
80
+ Module.new do
81
+ Settings.configuration.keys.each do |k|
82
+ define_method(k) do
83
+ return Settings.configuration[k]
84
+ end
85
+ define_method("#{k}=") do |val|
86
+ Settings.configuration[k] = val
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+ end
data/lib/perennial.rb ADDED
@@ -0,0 +1,21 @@
1
+ # Append the perennial lib folder onto the load path to make it
2
+ # nicer to require perennial-related libraries.
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+
5
+ require 'pathname'
6
+ require 'perennial/core_ext'
7
+ require 'perennial/exceptions'
8
+
9
+ module Perennial
10
+
11
+ VERSION = "0.2.2.2"
12
+
13
+ has_libary :dispatchable, :hookable, :loader, :logger,
14
+ :loggable, :manifest, :settings, :argument_parser,
15
+ :option_parser, :application, :generator, :daemon
16
+
17
+ def self.included(parent)
18
+ parent.extend(Manifest::Mixin)
19
+ end
20
+
21
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'perennial'
3
+
4
+ module <%= @application_module %>
5
+ include Perennial
6
+
7
+ VERSION = "0.0.1"
8
+
9
+ manifest do |m, l|
10
+ Settings.root = File.dirname(__FILE__)
11
+ # Initialize your controllers, e.g:
12
+ # l.register_controller :client, <%= @application_module %>::Client
13
+ end
14
+
15
+ end
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', <%= @application_path.to_s.inspect %>)
2
+ <%= @application_module %>::Settings.root = Pathname.new(__FILE__).dirname.join("..").expand_path
@@ -0,0 +1,31 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ task :default => "test:units"
5
+
6
+ namespace :test do
7
+
8
+ desc "Runs the unit tests for perennial"
9
+ Rake::TestTask.new("units") do |t|
10
+ t.pattern = 'test/*_test.rb'
11
+ t.libs << 'test'
12
+ t.verbose = true
13
+ end
14
+
15
+ end
16
+
17
+ task :gemspec do
18
+ require 'rubygems'
19
+ require File.join(File.dirname(__FILE__), "lib", "<%= @application_path %>")
20
+ spec = Gem::Specification.new do |s|
21
+ s.name = '<%= @application_path %>'
22
+ s.email = ''
23
+ s.homepage = ''
24
+ s.authors = ["YOUR NAME"]
25
+ s.version = <%= @application_module %>::VERSION
26
+ s.summary = ""
27
+ s.files = FileList["{bin,vendor,lib,test}/**/*"].to_a
28
+ s.platform = Gem::Platform::RUBY
29
+ end
30
+ File.open("<%= @application_path %>.gemspec", "w+") { |f| f.puts spec.to_ruby }
31
+ end
@@ -0,0 +1,4 @@
1
+ # Use this on a per-application setup / initialization basis.
2
+ <%= @application_module %>::Loader.before_run do
3
+ # Do stuff here...
4
+ end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class <%= @application_module %>Test < Test::Unit::TestCase
4
+
5
+ should 'have tests' do
6
+ fail "You need to write some tests"
7
+ end
8
+
9
+ end
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+
3
+ # Testing dependencies
4
+ require 'test/unit'
5
+ require 'shoulda'
6
+ # RedGreen doesn't seem to be needed under 1.9
7
+ require 'redgreen' if RUBY_VERSION < "1.9"
8
+
9
+ require 'pathname'
10
+ root_directory = Pathname.new(__FILE__).dirname.join("..").expand_path
11
+ require root_directory.join("lib", "<%= @application_path %>")
12
+
13
+ class Test::Unit::TestCase
14
+
15
+ protected
16
+
17
+ # Short hand for creating a class with
18
+ # a given class_eval block.
19
+ def class_via(*args, &blk)
20
+ klass = Class.new(*args)
21
+ klass.class_eval(&blk) unless blk.blank?
22
+ return klass
23
+ end
24
+
25
+ # Short hand for creating a test class
26
+ # for a set of mixins - give it the modules
27
+ # and it will include them all.
28
+ def test_class_for(*mods, &blk)
29
+ klass = Class.new
30
+ klass.class_eval { include(*mods) }
31
+ klass.class_eval(&blk) unless blk.blank?
32
+ return klass
33
+ end
34
+
35
+ end
@@ -0,0 +1,129 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class DispatchableTest < Test::Unit::TestCase
4
+
5
+ class ExampleDispatcher
6
+ include Perennial::Dispatchable
7
+ end
8
+
9
+ class ExampleHandlerA
10
+
11
+ attr_accessor :messages
12
+
13
+ def initialize
14
+ @messages = []
15
+ end
16
+
17
+ def handle(name, opts)
18
+ @messages << [name, opts]
19
+ end
20
+
21
+ end
22
+
23
+ class ExampleHandlerB < ExampleHandlerA; end
24
+
25
+ context 'marking a class as dispatchable' do
26
+
27
+ setup do
28
+ @dispatcher = ExampleDispatcher.new
29
+ end
30
+
31
+ should 'define a dispatch method' do
32
+ assert @dispatcher.respond_to?(:dispatch)
33
+ end
34
+
35
+ should 'require atleast a name for dispatch' do
36
+ assert_equal -2, @dispatcher.method(:dispatch).arity
37
+ end
38
+
39
+ end
40
+
41
+ context 'when registering handlers' do
42
+
43
+ setup do
44
+ @dispatcher = test_class_for(Perennial::Dispatchable)
45
+ end
46
+
47
+ should 'append a handler using register_handler' do
48
+ assert_equal [], @dispatcher.handlers
49
+ @dispatcher.register_handler(handler = ExampleHandlerA.new)
50
+ assert_equal [handler], @dispatcher.handlers
51
+ end
52
+
53
+ should 'batch assign handlers on handlers= using register_handler' do
54
+ handlers = [ExampleHandlerA.new, ExampleHandlerB.new]
55
+ assert_equal [], @dispatcher.handlers
56
+ @dispatcher.handlers = handlers
57
+ assert_equal handlers, @dispatcher.handlers
58
+ end
59
+
60
+ should 'return all handlers via the handlers class method' do
61
+ handlers = [ExampleHandlerA.new, ExampleHandlerB.new]
62
+ @dispatcher.handlers = handlers
63
+ assert_equal handlers, @dispatcher.handlers
64
+ end
65
+
66
+ should 'make handlers available to myself and all subclasses' do
67
+ # Set A
68
+ dispatcher_a = class_via(@dispatcher)
69
+ dispatcher_a.register_handler(handler_a = ExampleHandlerA.new)
70
+ # Set B
71
+ dispatcher_b = class_via(dispatcher_a)
72
+ dispatcher_b.register_handler(handler_b = ExampleHandlerA.new)
73
+ # Set C
74
+ dispatcher_c = class_via(dispatcher_b)
75
+ dispatcher_c.register_handler(handler_c = ExampleHandlerA.new)
76
+ # Set D
77
+ dispatcher_d = class_via(dispatcher_a)
78
+ dispatcher_d.register_handler(handler_d = ExampleHandlerB.new)
79
+ # Actual Assertions
80
+ assert_equal [], @dispatcher.handlers
81
+ assert_equal [handler_a], dispatcher_a.handlers
82
+ assert_equal [handler_a, handler_b], dispatcher_b.handlers
83
+ assert_equal [handler_a, handler_b, handler_c], dispatcher_c.handlers
84
+ assert_equal [handler_a, handler_d], dispatcher_d.handlers
85
+ end
86
+
87
+ end
88
+
89
+ context 'dispatching events' do
90
+
91
+ setup do
92
+ @dispatcher = class_via(ExampleDispatcher).new
93
+ @handler = ExampleHandlerA.new
94
+ @dispatcher.class.register_handler @handler
95
+ end
96
+
97
+ should 'attempt to call handle_[event_name] on itself' do
98
+ mock(@dispatcher).respond_to?(:handle_sample_event) { true }
99
+ mock(@dispatcher).handle_sample_event(:awesome => true, :sauce => 2)
100
+ @dispatcher.dispatch :sample_event, :awesome => true, :sauce => 2
101
+ end
102
+
103
+ should 'attempt to call handle_[event_name] on each handler' do
104
+ mock(@handler).respond_to?(:handle_sample_event) { true }
105
+ mock(@handler).handle_sample_event(:awesome => true, :sauce => 2)
106
+ @dispatcher.dispatch :sample_event, :awesome => true, :sauce => 2
107
+ end
108
+
109
+ should 'call handle on each handler if handle_[event_name] isn\'t defined' do
110
+ mock(@handler).respond_to?(:handle_sample_event) { false }
111
+ mock(@handler).handle(:sample_event, :awesome => true, :sauce => 2)
112
+ @dispatcher.dispatch :sample_event, :awesome => true, :sauce => 2
113
+ end
114
+
115
+ should 'let you halt handler processing if you raise HaltHandlerProcessing' do
116
+ handler_two = ExampleHandlerB.new
117
+ @dispatcher.class.register_handler handler_two
118
+ mock(@handler).handle(:sample_event, :awesome => true, :sauce => 2) do
119
+ raise Perennial::HaltHandlerProcessing
120
+ end
121
+ dont_allow(handler_two).handle(:sample_event, :awesome => true, :sauce => 2)
122
+ @dispatcher.dispatch :sample_event, :awesome => true, :sauce => 2
123
+ end
124
+
125
+ should 'log exceptions when encountered and not crash'
126
+
127
+ end
128
+
129
+ end
@@ -0,0 +1,61 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class HookableTest < Test::Unit::TestCase
4
+
5
+ context 'Hookable Classes' do
6
+
7
+ setup do
8
+ @hookable_class = test_class_for(Perennial::Hookable)
9
+ end
10
+
11
+ should 'let you append hooks via append_hook' do
12
+ assert_equal [], @hookable_class.hooks_for(:awesome)
13
+ @hookable_class.append_hook(:awesome) { puts "Hello!" }
14
+ assert_equal 1, @hookable_class.hooks_for(:awesome).size
15
+ end
16
+
17
+ should 'only append hooks if they aren\'t blank' do
18
+ @hookable_class.append_hook(:awesome)
19
+ assert_equal [], @hookable_class.hooks_for(:awesome)
20
+ end
21
+
22
+ should 'let you get an array of hooks' do
23
+ @hookable_class.append_hook(:awesome) { puts "A" }
24
+ @hookable_class.append_hook(:awesome) { puts "B" }
25
+ assert_equal 2, @hookable_class.hooks_for(:awesome).size
26
+ end
27
+
28
+ should 'let you invoke hooks' do
29
+ items = []
30
+ @hookable_class.append_hook(:awesome) { items << :a }
31
+ @hookable_class.append_hook(:awesome) { items << :b }
32
+ @hookable_class.append_hook(:awesome) { items << :c }
33
+ @hookable_class.invoke_hooks!(:awesome)
34
+ assert_equal [:a, :b, :c], items
35
+ end
36
+
37
+ should 'call them in the order they are appended' do
38
+ items = []
39
+ @hookable_class.append_hook(:awesome) { items << :a }
40
+ @hookable_class.append_hook(:awesome) { items << :b }
41
+ @hookable_class.append_hook(:awesome) { items << :c }
42
+ @hookable_class.invoke_hooks!(:awesome)
43
+ [:a, :b, :c].each_with_index do |value, index|
44
+ assert_equal value, items[index]
45
+ end
46
+ end
47
+
48
+ should 'let you define hook accessors' do
49
+ assert_equal [], @hookable_class.hooks_for(:awesome)
50
+ assert !@hookable_class.respond_to?(:awesome)
51
+ assert !@hookable_class.respond_to?(:sauce)
52
+ @hookable_class.define_hook :awesome, :sauce
53
+ assert @hookable_class.respond_to?(:awesome)
54
+ assert @hookable_class.respond_to?(:sauce)
55
+ @hookable_class.awesome { puts "A" }
56
+ assert_equal 1, @hookable_class.hooks_for(:awesome).size
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class LoggableTest < Test::Unit::TestCase
4
+
5
+ class ExampleLoggable
6
+ include Perennial::Loggable
7
+ end
8
+
9
+ context "Defining a class as loggable" do
10
+
11
+ setup do
12
+ @example = ExampleLoggable.new
13
+ end
14
+
15
+ should 'define a logger instance method' do
16
+ assert @example.respond_to?(:logger)
17
+ end
18
+
19
+ should 'define a logger class method' do
20
+ assert ExampleLoggable.respond_to?(:logger)
21
+ end
22
+
23
+ should 'not define a logger= instance method' do
24
+ assert !@example.respond_to?(:logger=)
25
+ end
26
+
27
+ should 'not define a logger= class method' do
28
+ assert !ExampleLoggable.respond_to?(:logger=)
29
+ end
30
+
31
+ should 'define logger to be an instance of Perennial::Logger' do
32
+ assert_equal Perennial::Logger, ExampleLoggable.logger
33
+ assert_equal Perennial::Logger, @example.logger
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,57 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class LoggerTest < Test::Unit::TestCase
4
+ context 'logger tests' do
5
+
6
+ setup do
7
+ @root_path = Perennial::Settings.root / "log"
8
+ Perennial::Logger.log_name = "example.log"
9
+ FileUtils.mkdir_p @root_path
10
+ end
11
+
12
+ context 'setting up a logger' do
13
+
14
+ setup { Perennial::Logger.setup! }
15
+
16
+ should 'create the log file file after writing' do
17
+ Perennial::Logger.fatal "Blergh."
18
+ assert File.exist?(@root_path / "example.log")
19
+ end
20
+
21
+ Perennial::Logger::LEVELS.each_key do |level_name|
22
+ should "define a method for the #{level_name} log level" do
23
+ assert Perennial::Logger.respond_to?(level_name)
24
+ assert Perennial::Logger.logger.respond_to?(level_name)
25
+ assert_equal 1, Perennial::Logger.logger.method(level_name).arity
26
+ end
27
+ end
28
+
29
+ should 'have a log exception method' do
30
+ assert Perennial::Logger.respond_to?(:log_exception)
31
+ assert Perennial::Logger.logger.respond_to?(:log_exception)
32
+ end
33
+
34
+ end
35
+
36
+ context 'writing to the log' do
37
+
38
+ Perennial::Logger::LEVELS.each_key do |level_name|
39
+ should "let you write to the #{level_name} log level" do
40
+ Perennial::Logger.verbose = false
41
+ Perennial::Logger.level = level_name
42
+ assert_nothing_raised do
43
+ Perennial::Logger.logger.send(level_name, "An Example Message No. 1")
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+ teardown do
51
+ log_path = @root_path / "example.log"
52
+ FileUtils.rm_rf(log_path) if File.exist?(log_path)
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,99 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class SettingsTest < Test::Unit::TestCase
4
+
5
+ context 'default settings' do
6
+
7
+ setup do
8
+ Perennial::Settings.setup!
9
+ end
10
+
11
+ should "default the application root to the parent folder of perennial" do
12
+ assert_equal __FILE__.to_pathname.dirname.join("..").expand_path,
13
+ Perennial::Settings.root.to_pathname
14
+ Perennial::Settings.root = "/awesome/sauce"
15
+ assert_equal "/awesome/sauce", Perennial::Settings.root
16
+ end
17
+
18
+ should "default daemonized to false" do
19
+ assert !Perennial::Settings.daemon?
20
+ Perennial::Settings.daemon = true
21
+ assert Perennial::Settings.daemon?
22
+ Perennial::Settings.daemon = false
23
+ assert !Perennial::Settings.daemon?
24
+ end
25
+
26
+ should "default the log level to :info" do
27
+ assert_equal :info, Perennial::Settings.log_level
28
+ Perennial::Settings.log_level = :debug
29
+ assert_equal :debug, Perennial::Settings.log_level
30
+ end
31
+
32
+ should "default verbose to false" do
33
+ assert !Perennial::Settings.verbose?
34
+ Perennial::Settings.verbose = true
35
+ assert Perennial::Settings.verbose?
36
+ Perennial::Settings.verbose = false
37
+ assert !Perennial::Settings.verbose?
38
+ end
39
+
40
+ end
41
+
42
+ context 'loading settings' do
43
+
44
+ setup do
45
+ config_folder = Perennial::Settings.root / "config"
46
+ @default_settings = {
47
+ "default" => {
48
+ "introduction" => true,
49
+ "description" => "Ninjas are Totally Awesome",
50
+ "channel" => "#offrails",
51
+ "users" => ["Sutto", "njero", "zapnap"]
52
+ }
53
+ }
54
+ FileUtils.mkdir_p(config_folder)
55
+ File.open(config_folder / "settings.yml", "w+") do |file|
56
+ file.write(@default_settings.to_yaml)
57
+ end
58
+ Perennial::Settings.setup!
59
+ end
60
+
61
+ should 'load settings from the file' do
62
+ assert Perennial::Settings.setup?
63
+ assert_equal @default_settings["default"].symbolize_keys, Perennial::Settings.to_hash
64
+ end
65
+
66
+ should 'define readers for the settings' do
67
+ instance = Perennial::Settings.new
68
+ @default_settings["default"].each_pair do |key, value|
69
+ assert Perennial::Settings.respond_to?(key.to_sym)
70
+ assert_equal value, Perennial::Settings.send(key)
71
+ assert instance.respond_to?(key.to_sym)
72
+ assert_equal value, instance.send(key)
73
+ end
74
+ end
75
+
76
+ should 'let you access settings via hash-style accessors' do
77
+ @default_settings["default"].each_pair do |key, value|
78
+ assert_equal value, Perennial::Settings[key]
79
+ Perennial::Settings[key] = "a-new-value from #{value.inspect}"
80
+ assert_equal "a-new-value from #{value.inspect}", Perennial::Settings[key]
81
+ end
82
+ end
83
+
84
+ should 'define writers for the settings' do
85
+ instance = Perennial::Settings.new
86
+ @default_settings["default"].each_pair do |key, value|
87
+ setter = :"#{key}="
88
+ assert Perennial::Settings.respond_to?(setter)
89
+ Perennial::Settings.send(setter, "value #{value.inspect} on class")
90
+ assert_equal "value #{value.inspect} on class", Perennial::Settings.send(key)
91
+ assert instance.respond_to?(setter)
92
+ instance.send(setter, "value #{value.inspect} on instance")
93
+ assert_equal "value #{value.inspect} on instance", instance.send(key)
94
+ end
95
+ end
96
+
97
+ end
98
+
99
+ end
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+
3
+ # Testing dependencies
4
+ require 'test/unit'
5
+ require 'shoulda'
6
+ require 'rr'
7
+ # RedGreen doesn't seem to be needed under 1.9
8
+ require 'redgreen' if RUBY_VERSION < "1.9"
9
+
10
+ require 'pathname'
11
+ root_directory = Pathname.new(__FILE__).dirname.join("..").expand_path
12
+ require root_directory.join("lib", "perennial")
13
+ require root_directory.join("vendor", "fakefs", "lib", "fakefs")
14
+
15
+ class Test::Unit::TestCase
16
+ include RR::Adapters::TestUnit
17
+
18
+ protected
19
+
20
+ # Short hand for creating a class with
21
+ # a given class_eval block.
22
+ def class_via(*args, &blk)
23
+ klass = Class.new(*args)
24
+ klass.class_eval(&blk) unless blk.blank?
25
+ return klass
26
+ end
27
+
28
+ # Short hand for creating a test class
29
+ # for a set of mixins - give it the modules
30
+ # and it will include them all.
31
+ def test_class_for(*mods, &blk)
32
+ klass = Class.new
33
+ klass.class_eval { include(*mods) }
34
+ klass.class_eval(&blk) unless blk.blank?
35
+ return klass
36
+ end
37
+
38
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Chris Wanstrath
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.