visitors 0.0.8 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/config.yml +5 -7
- data/lib/visitors.rb +8 -0
- data/lib/visitors/config.rb +9 -7
- data/lib/visitors/core_ext/hash.rb +21 -0
- data/lib/visitors/version.rb +1 -1
- data/spec/visitors/config_spec.rb +4 -2
- data/spec/visitors/helpers_spec.rb +0 -0
- data/spec/visitors_spec.rb +19 -0
- metadata +5 -2
data/config.yml
CHANGED
@@ -6,11 +6,9 @@ fields:
|
|
6
6
|
- similar
|
7
7
|
|
8
8
|
development:
|
9
|
-
redis_namespace: visitors_development
|
10
|
-
redis_config:
|
11
|
-
host: localhost
|
9
|
+
:redis_namespace: visitors_development
|
10
|
+
:redis_config:
|
11
|
+
:host: localhost
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
redis_config:
|
16
|
-
host: qa.touchlocal.com
|
13
|
+
production:
|
14
|
+
:disabled: true
|
data/lib/visitors.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
$:.push File.expand_path('..', __FILE__)
|
2
|
+
require 'bundler/setup'
|
2
3
|
require 'visitors/helpers'
|
4
|
+
require 'visitors/core_ext/hash'
|
3
5
|
|
4
6
|
module Visitors
|
5
7
|
extend self
|
@@ -7,15 +9,21 @@ module Visitors
|
|
7
9
|
autoload :Config, 'visitors/config'
|
8
10
|
autoload :Resource, 'visitors/resource'
|
9
11
|
|
12
|
+
def env
|
13
|
+
ENV['RAILS_ENV'] || ENV['VISITORS_ENV'] || 'development'
|
14
|
+
end
|
15
|
+
|
10
16
|
def config
|
11
17
|
@config ||= Config.load
|
12
18
|
end
|
13
19
|
|
14
20
|
def find(id)
|
21
|
+
return if Visitors.config.disabled
|
15
22
|
Resource.find(id)
|
16
23
|
end
|
17
24
|
|
18
25
|
def increment(id, field)
|
26
|
+
return if Visitors.config.disabled
|
19
27
|
Resource.new(id).increment(field)
|
20
28
|
end
|
21
29
|
end
|
data/lib/visitors/config.rb
CHANGED
@@ -3,6 +3,12 @@ require 'yaml'
|
|
3
3
|
class Visitors::Config
|
4
4
|
class MissingEnvironmentError < StandardError; end
|
5
5
|
|
6
|
+
DEFAULTS = {
|
7
|
+
:redis_namespace => "visitors_#{Visitors.env}",
|
8
|
+
:redis_connection => {:host => '127.0.0.1'},
|
9
|
+
:disabled => false
|
10
|
+
}
|
11
|
+
|
6
12
|
def self.load
|
7
13
|
new.tap { |instance| instance.send(:define_methods_from_yaml) }
|
8
14
|
end
|
@@ -12,8 +18,8 @@ class Visitors::Config
|
|
12
18
|
end
|
13
19
|
|
14
20
|
def all
|
15
|
-
return yaml[env] if yaml[env]
|
16
|
-
raise MissingEnvironmentError, "#{env.inspect} environment not configured"
|
21
|
+
return yaml[Visitors.env] if yaml[Visitors.env]
|
22
|
+
raise MissingEnvironmentError, "#{Visitors.env.inspect} environment not configured"
|
17
23
|
end
|
18
24
|
|
19
25
|
def inspect
|
@@ -32,12 +38,8 @@ class Visitors::Config
|
|
32
38
|
end
|
33
39
|
end
|
34
40
|
|
35
|
-
def env
|
36
|
-
ENV['RAILS_ENV'] || ENV['VISITORS_ENV'] || 'development'
|
37
|
-
end
|
38
|
-
|
39
41
|
def define_methods_from_yaml
|
40
|
-
all.each do |key, value|
|
42
|
+
DEFAULTS.deep_merge(all).each do |key, value|
|
41
43
|
self.class.send(:define_method, key) { value }
|
42
44
|
end
|
43
45
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Hash
|
2
|
+
if instance_methods.include?(:deep_merge)
|
3
|
+
warn 'Hash#deep_merge already defined!'
|
4
|
+
else
|
5
|
+
def deep_merge(other_hash)
|
6
|
+
dup.deep_merge!(other_hash)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
if instance_methods.include?(:deep_merge!)
|
11
|
+
warn 'Hash#deep_merge! already defined!'
|
12
|
+
else
|
13
|
+
def deep_merge!(other_hash)
|
14
|
+
other_hash.each_pair do |k,v|
|
15
|
+
tv = self[k]
|
16
|
+
self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
|
17
|
+
end
|
18
|
+
self
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/visitors/version.rb
CHANGED
@@ -3,15 +3,17 @@ require 'spec_helper'
|
|
3
3
|
describe Visitors::Config do
|
4
4
|
describe '#all' do
|
5
5
|
it 'uses the config.yml file to return a hash of configuration information' do
|
6
|
+
Visitors.stub!(:env => 'development')
|
6
7
|
File.stub!(:expand_path => '/path/to/config.yml')
|
8
|
+
|
7
9
|
YAML.should_receive(:load_file).with('/path/to/config.yml').and_return('development' => {})
|
8
10
|
Visitors::Config.load.all
|
9
11
|
end
|
10
12
|
|
11
13
|
it 'raises when an environment is not defined' do
|
12
14
|
expect {
|
13
|
-
|
14
|
-
|
15
|
+
YAML.stub!(:load_file => {})
|
16
|
+
Visitors::Config.load.all
|
15
17
|
}.to raise_error(Visitors::Config::MissingEnvironmentError)
|
16
18
|
end
|
17
19
|
end
|
File without changes
|
data/spec/visitors_spec.rb
CHANGED
@@ -1,4 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Visitors do
|
4
|
+
describe '#find' do
|
5
|
+
context 'with a disabled environment' do
|
6
|
+
it 'returns immediately' do
|
7
|
+
Visitors.config.stub!(:disabled => true)
|
8
|
+
Visitors::Resource.should_not_receive(:find)
|
9
|
+
Visitors.find(1).should be_nil
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#increment' do
|
15
|
+
context 'with a disabled environment' do
|
16
|
+
it 'returns immediately' do
|
17
|
+
Visitors.config.stub!(:disabled => true)
|
18
|
+
Visitors::Resource.should_not_receive(:new)
|
19
|
+
Visitors.increment(1, :show).should be_nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
4
23
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: visitors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- James Conroy-Finn
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-15 00:00:00 +00:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -144,6 +144,7 @@ files:
|
|
144
144
|
- config.yml
|
145
145
|
- lib/visitors.rb
|
146
146
|
- lib/visitors/config.rb
|
147
|
+
- lib/visitors/core_ext/hash.rb
|
147
148
|
- lib/visitors/helpers.rb
|
148
149
|
- lib/visitors/resource.rb
|
149
150
|
- lib/visitors/version.rb
|
@@ -155,6 +156,7 @@ files:
|
|
155
156
|
- server.rb
|
156
157
|
- spec/spec_helper.rb
|
157
158
|
- spec/visitors/config_spec.rb
|
159
|
+
- spec/visitors/helpers_spec.rb
|
158
160
|
- spec/visitors/resource_spec.rb
|
159
161
|
- spec/visitors_spec.rb
|
160
162
|
- views/_form.haml
|
@@ -194,5 +196,6 @@ summary: All-in-one fast logging system for your Rails application
|
|
194
196
|
test_files:
|
195
197
|
- spec/spec_helper.rb
|
196
198
|
- spec/visitors/config_spec.rb
|
199
|
+
- spec/visitors/helpers_spec.rb
|
197
200
|
- spec/visitors/resource_spec.rb
|
198
201
|
- spec/visitors_spec.rb
|