dockly-util 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.cane +3 -0
- data/.gitignore +2 -0
- data/.rspec +3 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +20 -0
- data/Rakefile +18 -0
- data/dockly-util.gemspec +20 -0
- data/lib/dockly.rb +4 -0
- data/lib/dockly/util.rb +8 -0
- data/lib/dockly/util/delegate.rb +6 -0
- data/lib/dockly/util/dsl.rb +118 -0
- data/lib/dockly/util/logger.rb +96 -0
- data/lib/dockly/util/version.rb +5 -0
- data/spec/dockl/util/dsl_spec.rb +171 -0
- data/spec/dockl/util/logger_spec.rb +193 -0
- data/spec/spec_helper.rb +11 -0
- metadata +131 -0
data/.cane
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Swipely, Inc.
|
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/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Copyright Swipely, Inc. All rights reserved.
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift( File.join( File.dirname(__FILE__), 'lib' ) )
|
4
|
+
|
5
|
+
require 'rake'
|
6
|
+
require 'dockly/util'
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
require 'cane/rake_task'
|
9
|
+
|
10
|
+
task :default => [:spec, :quality]
|
11
|
+
|
12
|
+
RSpec::Core::RakeTask.new do |t|
|
13
|
+
t.pattern = 'spec/**/*_spec.rb'
|
14
|
+
end
|
15
|
+
|
16
|
+
Cane::RakeTask.new(:quality) do |cane|
|
17
|
+
cane.canefile = '.cane'
|
18
|
+
end
|
data/dockly-util.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/dockly/util/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Swipely, Inc."]
|
6
|
+
gem.email = %w{tomhulihan@swipely.com bright@swipely.com toddlunter@swipely.com}
|
7
|
+
gem.description = %q{DSL made easy}
|
8
|
+
gem.summary = %q{DSL made easy}
|
9
|
+
gem.homepage = "https://github.com/swipely/dockly-util"
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "dockly-util"
|
14
|
+
gem.require_paths = %w{lib}
|
15
|
+
gem.version = Dockly::Util::VERSION
|
16
|
+
gem.add_development_dependency 'rake'
|
17
|
+
gem.add_development_dependency 'rspec'
|
18
|
+
gem.add_development_dependency 'cane'
|
19
|
+
gem.add_development_dependency 'pry'
|
20
|
+
end
|
data/lib/dockly.rb
ADDED
data/lib/dockly/util.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
module Dockly::Util::DSL
|
2
|
+
def self.included(base)
|
3
|
+
base.instance_eval do
|
4
|
+
extend ClassMethods
|
5
|
+
dsl_attribute :name
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def new!(options = {}, &block)
|
11
|
+
inst = new(options, &block)
|
12
|
+
instances[inst.name] = inst
|
13
|
+
end
|
14
|
+
|
15
|
+
def dsl_attribute(*names)
|
16
|
+
names.each do |name|
|
17
|
+
define_method(name) do |val = nil|
|
18
|
+
val.nil? ? instance_variable_get(:"@#{name}")
|
19
|
+
: instance_variable_set(:"@#{name}", val)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
private :dsl_attribute
|
24
|
+
|
25
|
+
def dsl_class_attribute(name, klass)
|
26
|
+
unless klass.ancestors.include?(Dockly::Util::DSL)
|
27
|
+
raise "#{self.class}.dsl_class_attribute requires a class that includes DSL"
|
28
|
+
end
|
29
|
+
define_method(name) do |sym = nil, &block|
|
30
|
+
new_value = case
|
31
|
+
when !block.nil? && !sym.nil? then klass.new!(:name => sym, &block)
|
32
|
+
when block.nil? then klass.instances[sym]
|
33
|
+
when sym.nil? then klass.new!(:name => :"#{self.name}_#{klass}", &block)
|
34
|
+
end
|
35
|
+
instance_variable_set(:"@#{name}", new_value) unless new_value.nil?
|
36
|
+
instance_variable_get(:"@#{name}")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
private :dsl_class_attribute
|
40
|
+
|
41
|
+
def default_value(name, val = nil)
|
42
|
+
default_values[name] = block_given? ? yield : val
|
43
|
+
end
|
44
|
+
private :default_value
|
45
|
+
|
46
|
+
def default_values
|
47
|
+
@default_values ||= {}
|
48
|
+
end
|
49
|
+
|
50
|
+
def instances
|
51
|
+
@instances ||= {}
|
52
|
+
end
|
53
|
+
|
54
|
+
def demodulize(path)
|
55
|
+
if i = path.rindex('::')
|
56
|
+
path[(i+2)..-1]
|
57
|
+
else
|
58
|
+
path
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def generate_unique_name
|
63
|
+
name = nil
|
64
|
+
(0..(1.0 / 0.0)).each do |n|
|
65
|
+
name = :"#{demodulize(self.name)}_#{n}"
|
66
|
+
break unless instances.has_key?(name)
|
67
|
+
end
|
68
|
+
name
|
69
|
+
end
|
70
|
+
|
71
|
+
def [](sym)
|
72
|
+
instances[sym]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_s
|
77
|
+
"#{self.class.name} (#{
|
78
|
+
instance_variables.map { |var|
|
79
|
+
"#{var} = #{instance_variable_get(var)}"
|
80
|
+
}.join(', ')
|
81
|
+
})"
|
82
|
+
end
|
83
|
+
|
84
|
+
def [](var)
|
85
|
+
instance_variable_get(:"@#{var}")
|
86
|
+
end
|
87
|
+
|
88
|
+
def []=(var, val)
|
89
|
+
instance_variable_set(:"@#{var}", val)
|
90
|
+
end
|
91
|
+
|
92
|
+
def ==(other_dsl)
|
93
|
+
(other_dsl.class == self.class) && (other_dsl.name == self.name)
|
94
|
+
end
|
95
|
+
def initialize(options = {}, &block)
|
96
|
+
self.class.default_values.merge(options).each do |k, v|
|
97
|
+
v = v.dup rescue v unless v.class.ancestors.include?(Dockly::Util::DSL)
|
98
|
+
public_send(k, v)
|
99
|
+
end
|
100
|
+
instance_eval(&block) unless block.nil?
|
101
|
+
name(self.class.generate_unique_name) if name.nil?
|
102
|
+
self.instance_variable_get(:@name).freeze
|
103
|
+
end
|
104
|
+
|
105
|
+
def to_hash
|
106
|
+
Hash[instance_variables.map { |ivar| [ivar.to_s[1..-1].to_sym, instance_variable_get(ivar)] }]
|
107
|
+
end
|
108
|
+
|
109
|
+
def ensure_present!(*syms)
|
110
|
+
syms.each do |sym|
|
111
|
+
if instance_variable_get(:"@#{sym}").nil?
|
112
|
+
method_name = /.*`(?<method_name>.+)'$/.match(caller[2])[:method_name]
|
113
|
+
raise "The following must be present to use #{self.class}##{method_name}: #{sym}"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
private :ensure_present!
|
118
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
class Dockly::Util::Logger
|
2
|
+
attr_accessor :prefix, :print_method, :output
|
3
|
+
alias_method :print_method?, :print_method
|
4
|
+
|
5
|
+
LEVELS = [:debug, :info, :warn, :error, :fatal, :unknown].freeze
|
6
|
+
|
7
|
+
def initialize(prefix = "", output = Dockly::Util::Logger.output, print_method = Dockly::Util::Logger.print_method)
|
8
|
+
@prefix = prefix
|
9
|
+
@print_method = print_method
|
10
|
+
@output = output
|
11
|
+
end
|
12
|
+
|
13
|
+
def log(level, message)
|
14
|
+
output.puts(format_message(level, message)) if self.class.enabled?
|
15
|
+
end
|
16
|
+
|
17
|
+
LEVELS.each do |level|
|
18
|
+
define_method(level) { |message| log(level, message) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def format_message(level, message)
|
22
|
+
[
|
23
|
+
format_level(level),
|
24
|
+
Thread.current[:rake_task].to_s,
|
25
|
+
prefix,
|
26
|
+
get_last_method,
|
27
|
+
message
|
28
|
+
].compact.reject(&:empty?).join(' ')
|
29
|
+
end
|
30
|
+
|
31
|
+
def format_level(level)
|
32
|
+
(char = level.to_s[0]) ? char.upcase : nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_last_method
|
36
|
+
if print_method?
|
37
|
+
file_and_method = caller.reject { |trace| trace =~ /deployz\/logger\.rb|block \(\d+ levels\)/ }.first
|
38
|
+
file_and_method.match(/:in `(.+)'$/)[1]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def with_prefix(new_prefix = "", output = nil, print_method = nil)
|
43
|
+
output ||= self.output
|
44
|
+
print_method ||= self.print_method
|
45
|
+
yield(self.class.new([prefix, new_prefix].compact.reject(&:empty?).join(' '), output, print_method))
|
46
|
+
end
|
47
|
+
|
48
|
+
module Mixin
|
49
|
+
extend Dockly::Util::Delegate
|
50
|
+
|
51
|
+
def self.included(base)
|
52
|
+
base.extend(ClassMethods)
|
53
|
+
end
|
54
|
+
|
55
|
+
def logger
|
56
|
+
@logger ||= Dockly::Util::Logger.new(self.class.logger_prefix)
|
57
|
+
end
|
58
|
+
|
59
|
+
delegate(*(LEVELS + [:log, :with_prefix]), :to => :logger)
|
60
|
+
|
61
|
+
module ClassMethods
|
62
|
+
def logger_prefix(val = nil)
|
63
|
+
val.nil? ? (@logger_prefix ||= self.name) : (@logger_prefix = val)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
class << self
|
70
|
+
include Mixin
|
71
|
+
|
72
|
+
alias_method :default, :logger
|
73
|
+
attr_writer :print_method, :output
|
74
|
+
|
75
|
+
def disable!
|
76
|
+
@logger_enabled = false
|
77
|
+
end
|
78
|
+
|
79
|
+
def enable!
|
80
|
+
@logger_enabled = true
|
81
|
+
end
|
82
|
+
|
83
|
+
def enabled?
|
84
|
+
enable! if @logger_enabled.nil?
|
85
|
+
@logger_enabled
|
86
|
+
end
|
87
|
+
|
88
|
+
def print_method
|
89
|
+
(@print_method == false) ? false : true
|
90
|
+
end
|
91
|
+
|
92
|
+
def output
|
93
|
+
@output ||= STDOUT
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Dockly::Util::DSL do
|
4
|
+
let(:included_class) do
|
5
|
+
Class.new do
|
6
|
+
include Dockly::Util::DSL
|
7
|
+
|
8
|
+
dsl_attribute :ocean
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:test_class) do
|
13
|
+
klass = included_class
|
14
|
+
Class.new do
|
15
|
+
include Dockly::Util::DSL
|
16
|
+
|
17
|
+
dsl_attribute :chips
|
18
|
+
dsl_class_attribute :meta, klass
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
subject { test_class.new!(:name => 'Tony') }
|
23
|
+
|
24
|
+
describe '.dsl_attribute' do
|
25
|
+
it 'defines a method for each name given' do
|
26
|
+
subject.should respond_to :chips
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'the defined method(s)' do
|
30
|
+
context 'with 0 arguments' do
|
31
|
+
let(:test_val) { 'lays' }
|
32
|
+
before { subject.instance_variable_set(:@chips, test_val) }
|
33
|
+
|
34
|
+
it 'returns the value of the instance variable with a matching name' do
|
35
|
+
subject.chips.should == test_val
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with one argument' do
|
40
|
+
it 'sets the corresponding instance variable to the passed-in value' do
|
41
|
+
expect { subject.chips('bbq') }
|
42
|
+
.to change { subject.instance_variable_get(:@chips) }
|
43
|
+
.to 'bbq'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '.dsl_class_attribute' do
|
50
|
+
context 'when the sceond argument is not a DSL' do
|
51
|
+
it 'raises an error' do
|
52
|
+
expect do
|
53
|
+
Class.new do
|
54
|
+
include Dockly::Util::DSL
|
55
|
+
|
56
|
+
dsl_class_attribute :test, Array
|
57
|
+
end
|
58
|
+
end.to raise_error
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'when the second argument is a DSL' do
|
63
|
+
it 'defines a new method with the name of the first argument' do
|
64
|
+
subject.should respond_to :meta
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'the method' do
|
68
|
+
let(:meta) { subject.instance_variable_get(:@meta) }
|
69
|
+
|
70
|
+
context 'when a symbol is given' do
|
71
|
+
context 'and a block is given' do
|
72
|
+
before { subject.meta(:yolo) { ocean 'Indian' } }
|
73
|
+
|
74
|
+
it 'creates a new instance of the specified Class with the name set' do
|
75
|
+
meta.name.should == :yolo
|
76
|
+
meta.ocean.should == 'Indian'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'and a block is not given' do
|
81
|
+
before { included_class.new!(:name => :test_name) { ocean 'Pacific' } }
|
82
|
+
|
83
|
+
it 'looks up the instance in its Class\'s .instances hash' do
|
84
|
+
subject.meta(:test_name).ocean.should == 'Pacific'
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'when a symbol is not given' do
|
90
|
+
context 'and a block is given' do
|
91
|
+
before { subject.meta { ocean 'Atlantic' } }
|
92
|
+
|
93
|
+
it 'creates a new instance of the specified Class' do
|
94
|
+
meta.ocean.should == 'Atlantic'
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'generates a name' do
|
98
|
+
meta.name.should == :"#{subject.name}_#{meta.class}"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'and a block is not given' do
|
103
|
+
before { subject.meta(:test) { ocean 'Artic' } }
|
104
|
+
|
105
|
+
it 'returns the corresponding instance variable' do
|
106
|
+
subject.meta.should == meta
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe '#ensure_present!' do
|
115
|
+
context 'when at least one of the symbols is nil' do
|
116
|
+
it 'raises an error' do
|
117
|
+
expect { subject.send(:ensure_present!, :meta) }.to raise_error
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'when each symbol is present' do
|
122
|
+
it 'does nothing' do
|
123
|
+
expect { subject.send(:ensure_present!, :name) }.to_not raise_error
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe '.default_value' do
|
129
|
+
context 'when a block is given' do
|
130
|
+
it 'calls the block and adds it to he default_values hash' do
|
131
|
+
test_class.send(:default_value, :meta) { included_class.new!(:name => :joey) }
|
132
|
+
test_class.default_values[:meta].name.should == :joey
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'when a value is given' do
|
137
|
+
it 'adds the pair to the default_values hash' do
|
138
|
+
test_class.send(:default_value, :chips, 'Cool Ranch')
|
139
|
+
test_class.default_values[:chips].should == 'Cool Ranch'
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe '#initialize' do
|
145
|
+
context 'when a block is given' do
|
146
|
+
it 'is instance evaluated' do
|
147
|
+
test_class.new! { name :timmy }.name.should == :timmy
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
context 'when a hash is given' do
|
152
|
+
it 'sends each key value pair to the object as a message' do
|
153
|
+
test_class.new!(:name => :tommy).name.should == :tommy
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
context 'when there is no name' do
|
158
|
+
it 'raises an error' do
|
159
|
+
expect { test_class.new! { chips 'Tortilla' } }.to raise_error
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
context 'when there is a name' do
|
164
|
+
it 'adds the current instance to the class\'s instances hash' do
|
165
|
+
test_class.instances[:jillian].should be_nil
|
166
|
+
test_class.new! { name :jillian }
|
167
|
+
test_class.instances[:jillian].name.should == :jillian
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
@@ -0,0 +1,193 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Dockly::Util::Logger do
|
4
|
+
before(:all) { Dockly::Util::Logger.enable! }
|
5
|
+
after(:all) { Dockly::Util::Logger.disable! unless ENV['ENABLE_LOGGER'] == 'true' }
|
6
|
+
|
7
|
+
describe '#initialize' do
|
8
|
+
its(:prefix) { should be_empty }
|
9
|
+
its(:print_method?) { should be_true }
|
10
|
+
its(:output) { should == STDOUT }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#log' do
|
14
|
+
context 'when the logger is enabled' do
|
15
|
+
let(:level) { :debug }
|
16
|
+
let(:message) { 'hey mom' }
|
17
|
+
let(:formatted) { 'T hey mom' }
|
18
|
+
|
19
|
+
before { subject.stub(:format_message).with(level, message).and_return(formatted) }
|
20
|
+
|
21
|
+
it 'sends a formatted level and message to the output' do
|
22
|
+
subject.output.should_receive(:puts).with(formatted)
|
23
|
+
subject.log(level, message)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'when the logger is disabled' do
|
28
|
+
before(:all) { Dockly::Util::Logger.disable! }
|
29
|
+
after(:all) { Dockly::Util::Logger.enable! }
|
30
|
+
|
31
|
+
it 'does nothing' do
|
32
|
+
subject.output.should_not_receive(:puts)
|
33
|
+
subject.log(:debug, 'this wont be printed')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
Dockly::Util::Logger::LEVELS.each do |level|
|
39
|
+
describe "##{level}" do
|
40
|
+
let(:message) { "message for #{level}" }
|
41
|
+
|
42
|
+
it "sends the message to #log with #{level} as the level" do
|
43
|
+
subject.should_receive(:log).with(level, message)
|
44
|
+
subject.public_send(level, message)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#format message' do
|
50
|
+
before do
|
51
|
+
subject.stub(:prefix).and_return(prefix)
|
52
|
+
subject.stub(:get_last_method).and_return(method)
|
53
|
+
subject.stub(:format_level).with(level).and_return(formatted_level)
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'when all of the fields are present' do
|
57
|
+
let(:level) { :debug }
|
58
|
+
let(:formatted_level) { 'D' }
|
59
|
+
let(:prefix) { '[deployz logger]' }
|
60
|
+
let(:method) { 'test' }
|
61
|
+
let(:message) { 'here i am' }
|
62
|
+
|
63
|
+
it 'returns each of them with a space in the middle' do
|
64
|
+
subject.format_message(level, message).should == "#{formatted_level} #{prefix} #{method} #{message}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when one of the fields returns nil' do
|
69
|
+
let(:level) { :info }
|
70
|
+
let(:formatted_level) { 'I' }
|
71
|
+
let(:prefix) { '[deployz logger]' }
|
72
|
+
let(:method) { nil }
|
73
|
+
let(:message) { 'there you are' }
|
74
|
+
|
75
|
+
it 'does not insert an extra space' do
|
76
|
+
subject.format_message(level, message).should == "#{formatted_level} #{prefix} #{message}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#format_level' do
|
82
|
+
context 'when the level is nil or empty' do
|
83
|
+
it 'returns nil' do
|
84
|
+
[nil, '', :''].should be_all { |level| subject.format_level(level).nil? }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'when the level is present' do
|
89
|
+
it 'returns the first character upper-cased' do
|
90
|
+
{ :info => 'I',
|
91
|
+
:debug => 'D',
|
92
|
+
'>>=' => '>'
|
93
|
+
}.should be_all { |level, formatted|
|
94
|
+
subject.format_level(level) == formatted
|
95
|
+
}
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe '#get_last_method' do
|
101
|
+
def test_get_last_method
|
102
|
+
subject.get_last_method
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'when calls are not nested' do
|
106
|
+
it 'returns the calling method' do
|
107
|
+
test_get_last_method.should == 'test_get_last_method'
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context 'when calls are nested' do
|
112
|
+
def outer_method
|
113
|
+
test_get_last_method
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'still returns the calling method' do
|
117
|
+
outer_method.should == 'test_get_last_method'
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe '#with_prefix' do
|
123
|
+
context 'when the new prefix is empty' do
|
124
|
+
context 'and the old prefix is empty' do
|
125
|
+
it 'yields a new logger with an empty prefix' do
|
126
|
+
subject.with_prefix(nil) { |logger| logger.prefix.should be_empty }
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context 'and the old prefix is present' do
|
131
|
+
let(:prefix) { 'hello' }
|
132
|
+
subject { described_class.new(prefix) }
|
133
|
+
|
134
|
+
it 'yields a new logger with the old prefix' do
|
135
|
+
subject.with_prefix(nil) { |logger| logger.prefix.should == prefix }
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context 'when the new prefix is present' do
|
141
|
+
context 'and the old prefix is empty' do
|
142
|
+
let(:prefix) { 'hola' }
|
143
|
+
|
144
|
+
it 'yields a new logger with the new prefix' do
|
145
|
+
subject.with_prefix(prefix) { |logger| logger.prefix.should == prefix }
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context 'and the old prefix is present' do
|
150
|
+
let(:old_prefix) { 'hello' }
|
151
|
+
let(:new_prefix) { 'hola' }
|
152
|
+
let(:prefix) { "#{old_prefix} #{new_prefix}" }
|
153
|
+
|
154
|
+
subject { described_class.new(old_prefix) }
|
155
|
+
|
156
|
+
it 'yields a new logger with the old prefix' do
|
157
|
+
subject.with_prefix(new_prefix) { |logger| logger.prefix.should == prefix }
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
describe 'the meta class' do
|
164
|
+
subject { described_class }
|
165
|
+
|
166
|
+
(Dockly::Util::Logger::LEVELS + [:default, :logger, :log, :with_prefix]).each do |method|
|
167
|
+
it { should respond_to method }
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe Dockly::Util::Logger::Mixin do
|
172
|
+
let(:test_class) { Class.new { include Dockly::Util::Logger::Mixin } }
|
173
|
+
|
174
|
+
subject { test_class.new }
|
175
|
+
|
176
|
+
describe '#logger' do
|
177
|
+
its(:logger) { should be_a Dockly::Util::Logger }
|
178
|
+
|
179
|
+
it 'has the class name as the prefix' do
|
180
|
+
subject.logger.prefix.should == test_class.name
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
(Dockly::Util::Logger::LEVELS + [:log, :with_prefix]).each do |method|
|
185
|
+
describe "##{method}" do
|
186
|
+
it "sends ##{method} to #logger" do
|
187
|
+
subject.logger.should_receive(method)
|
188
|
+
subject.public_send(method)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
require 'dockly/util'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.mock_with :rspec
|
9
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
10
|
+
config.tty = true
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dockly-util
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Swipely, Inc.
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-11-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: cane
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: pry
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: DSL made easy
|
79
|
+
email:
|
80
|
+
- tomhulihan@swipely.com
|
81
|
+
- bright@swipely.com
|
82
|
+
- toddlunter@swipely.com
|
83
|
+
executables: []
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files: []
|
86
|
+
files:
|
87
|
+
- .cane
|
88
|
+
- .gitignore
|
89
|
+
- .rspec
|
90
|
+
- Gemfile
|
91
|
+
- LICENSE.txt
|
92
|
+
- Rakefile
|
93
|
+
- dockly-util.gemspec
|
94
|
+
- lib/dockly.rb
|
95
|
+
- lib/dockly/util.rb
|
96
|
+
- lib/dockly/util/delegate.rb
|
97
|
+
- lib/dockly/util/dsl.rb
|
98
|
+
- lib/dockly/util/logger.rb
|
99
|
+
- lib/dockly/util/version.rb
|
100
|
+
- spec/dockl/util/dsl_spec.rb
|
101
|
+
- spec/dockl/util/logger_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
homepage: https://github.com/swipely/dockly-util
|
104
|
+
licenses: []
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.23
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: DSL made easy
|
127
|
+
test_files:
|
128
|
+
- spec/dockl/util/dsl_spec.rb
|
129
|
+
- spec/dockl/util/logger_spec.rb
|
130
|
+
- spec/spec_helper.rb
|
131
|
+
has_rdoc:
|