bzsnippets 0.0.1 → 0.0.2
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/Gemfile +0 -1
- data/lib/snippets/object/env_attribute.rb +39 -36
- data/lib/snippets/version.rb +1 -1
- data/snippets.gemspec +1 -1
- data/spec/object/env_attribute_spec.rb +7 -1
- metadata +7 -8
- data/unicorn +0 -52
data/Gemfile
CHANGED
@@ -1,57 +1,60 @@
|
|
1
1
|
require 'active_support/concern'
|
2
|
-
require '
|
2
|
+
require 'active_support/core_ext/hash'
|
3
|
+
require 'pathname'
|
4
|
+
require 'delegate'
|
3
5
|
module Snippets
|
4
6
|
module Object
|
5
7
|
module EnvAttribute
|
6
8
|
extend ActiveSupport::Concern
|
7
9
|
|
8
|
-
class
|
10
|
+
class EnvProxy < Delegator
|
11
|
+
def initialize(value, opts={})
|
12
|
+
super(value)
|
13
|
+
@delegate_sd_obj = value
|
14
|
+
opts[:env] = lambda{opts[:env]} if opts[:env].kind_of? String
|
15
|
+
@options = opts
|
16
|
+
end
|
17
|
+
|
18
|
+
def __setobj__(obj)
|
19
|
+
@delegate_sd_obj = obj
|
20
|
+
end
|
9
21
|
|
10
22
|
def __getobj__
|
11
23
|
env = @options[:env].call
|
12
|
-
|
24
|
+
hsh = all_envs
|
25
|
+
(hsh.kind_of?(Hash) && hsh[env]) || nil
|
13
26
|
end
|
14
27
|
|
15
28
|
def all_envs
|
16
|
-
@
|
29
|
+
if @options[:source].kind_of? Proc
|
30
|
+
if @options[:source].arity == 1
|
31
|
+
@options[:source].call(self)
|
32
|
+
else
|
33
|
+
@options[:source].call
|
34
|
+
end
|
35
|
+
else
|
36
|
+
@delegate_sd_obj ||= fetch_source(@options[:source])
|
37
|
+
end
|
17
38
|
end
|
18
|
-
end
|
19
39
|
|
20
|
-
|
21
|
-
|
40
|
+
private
|
41
|
+
|
42
|
+
def fetch_source value
|
43
|
+
YAML.load(Pathname.new(value).open.read).with_indifferent_access
|
44
|
+
end
|
22
45
|
end
|
23
46
|
|
47
|
+
|
24
48
|
module ClassMethods
|
25
|
-
def env_attribute
|
26
|
-
|
27
|
-
|
49
|
+
def env_attribute name, opts={}
|
50
|
+
opts[:env] ||= lambda{(Rails.env if defined? Rails) || RAILS_ENV || "development"}
|
51
|
+
define_method name do
|
52
|
+
v = instance_variable_get("@#{name}")
|
53
|
+
instance_variable_set("@#{name}", EnvProxy.new(nil, opts)) unless v
|
54
|
+
instance_variable_get("@#{name}")
|
28
55
|
end
|
29
|
-
|
30
|
-
|
31
|
-
if defined? Rails
|
32
|
-
opts[:env] ||= lambda{ Rails.env }
|
33
|
-
else
|
34
|
-
opts[:env] ||= lambda do
|
35
|
-
if defined? Rails
|
36
|
-
Rails.env
|
37
|
-
else
|
38
|
-
ENV["RAILS_ENV"] || "development"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
proxy_accessor *args, :proxy => EnvironmentProxy, :source => opts[:source], :env => opts[:env]
|
43
|
-
args.each do |i|
|
44
|
-
old = "_#{i}".to_sym
|
45
|
-
alias_method old, i
|
46
|
-
define_method i do
|
47
|
-
value = self.send(old)
|
48
|
-
unless value
|
49
|
-
self.send("#{i}=", fetch_source(opts[:source]))
|
50
|
-
self.send(old)
|
51
|
-
else
|
52
|
-
value
|
53
|
-
end
|
54
|
-
end
|
56
|
+
define_method "#{name}=" do |value|
|
57
|
+
instance_variable_set("@#{name}", EnvProxy.new(value, opts))
|
55
58
|
end
|
56
59
|
end
|
57
60
|
end
|
data/lib/snippets/version.rb
CHANGED
data/snippets.gemspec
CHANGED
@@ -8,6 +8,7 @@ describe Snippets::Object do
|
|
8
8
|
include Snippets::Object::EnvAttribute
|
9
9
|
env_attribute :a, :source => (Pathname.new(__FILE__) + '../../fixtures/config.yml'), :env => lambda{"test"}
|
10
10
|
env_attribute :a1, :source => (Pathname.new(__FILE__) + '../../fixtures/config.yml'), :env => lambda{"development"}
|
11
|
+
env_attribute :b, :source => lambda{|i| YAML.load_file(Pathname.new(__FILE__) + '../../fixtures/config.yml')}, :env => lambda{"test"}
|
11
12
|
end
|
12
13
|
end
|
13
14
|
it "should work" do
|
@@ -29,7 +30,12 @@ describe Snippets::Object do
|
|
29
30
|
a = Tester.new
|
30
31
|
a.a1 = YAML.load_file(Pathname.new(__FILE__) + '../../fixtures/config.yml')
|
31
32
|
a.a1["adapter"] = "dfsdfdsfdsf"
|
32
|
-
a.a1
|
33
|
+
a.a1["adapter"].should == "dfsdfdsfdsf"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should allow source lambdas" do
|
37
|
+
a = Tester.new
|
38
|
+
a.b.should == a.a
|
33
39
|
end
|
34
40
|
|
35
41
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bzsnippets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -28,21 +28,21 @@ dependencies:
|
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '3'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
|
-
name:
|
31
|
+
name: rspec
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
33
33
|
none: false
|
34
34
|
requirements:
|
35
|
-
- -
|
35
|
+
- - ! '>='
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version: 0
|
38
|
-
type: :
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
39
|
prerelease: false
|
40
40
|
version_requirements: !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
|
-
- -
|
43
|
+
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version: 0
|
45
|
+
version: '0'
|
46
46
|
description: Ruby and Rails code snippets
|
47
47
|
email:
|
48
48
|
- akostrov@spbtv.com
|
@@ -64,7 +64,6 @@ files:
|
|
64
64
|
- spec/fixtures/config.yml
|
65
65
|
- spec/object/env_attribute_spec.rb
|
66
66
|
- spec/spec_helper.rb
|
67
|
-
- unicorn
|
68
67
|
homepage: http://spbtv.com
|
69
68
|
licenses: []
|
70
69
|
post_install_message:
|
data/unicorn
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
#! /bin/sh
|
2
|
-
|
3
|
-
# File: /etc/init.d/unicorn
|
4
|
-
|
5
|
-
### BEGIN INIT INFO
|
6
|
-
# Provides: unicorn
|
7
|
-
# Required-Start: $local_fs $remote_fs $network $syslog
|
8
|
-
# Required-Stop: $local_fs $remote_fs $network $syslog
|
9
|
-
# Default-Start: 2 3 4 5
|
10
|
-
# Default-Stop: 0 1 6
|
11
|
-
# Short-Description: starts the unicorn web server
|
12
|
-
# Description: starts unicorn
|
13
|
-
### END INIT INFO
|
14
|
-
|
15
|
-
DAEMON="/home/deploy/.rvm/bin/bootup_unicorn_rails"
|
16
|
-
DAEMON_OPTS="-c /home/deploy/apps/intranet/production/current/config/unicorn/production.rb -E production -D"
|
17
|
-
NAME=unicorn
|
18
|
-
DESC="Unicorn app for intranet"
|
19
|
-
PID=/home/deploy/apps/intranet/production/shared/pids/unicorn.pid
|
20
|
-
|
21
|
-
|
22
|
-
case "$1" in
|
23
|
-
start)
|
24
|
-
echo -n "Starting $DESC: "
|
25
|
-
su - deploy -c "$DAEMON $DAEMON_OPTS"
|
26
|
-
echo "$NAME."
|
27
|
-
;;
|
28
|
-
stop)
|
29
|
-
echo -n "Stopping $DESC: "
|
30
|
-
kill -QUIT `cat $PID`
|
31
|
-
echo "$NAME."
|
32
|
-
;;
|
33
|
-
restart)
|
34
|
-
echo -n "Restarting $DESC: "
|
35
|
-
kill -QUIT `cat $PID`
|
36
|
-
sleep 1
|
37
|
-
su - deploy -c "$DAEMON $DAEMON_OPTS"
|
38
|
-
echo "$NAME."
|
39
|
-
;;
|
40
|
-
reload)
|
41
|
-
echo -n "Reloading $DESC configuration: "
|
42
|
-
kill -HUP `cat $PID`
|
43
|
-
echo "$NAME."
|
44
|
-
;;
|
45
|
-
*)
|
46
|
-
echo "Usage: $NAME {start|stop|restart|reload}" >&2
|
47
|
-
exit 1
|
48
|
-
;;
|
49
|
-
esac
|
50
|
-
|
51
|
-
exit 0
|
52
|
-
|