ThiagoLelis-backgroundjob 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bj/attributes.rb CHANGED
@@ -1,120 +1,120 @@
1
- module Attributes
2
- Attributes::VERSION = '5.0.0' unless defined? Attributes::VERSION
3
- def self.version() Attributes::VERSION end
4
-
5
- class List < ::Array
6
- def << element
7
- super
8
- self
9
- ensure
10
- uniq!
11
- index!
12
- end
13
- def index!
14
- @index ||= Hash.new
15
- each{|element| @index[element] = true}
16
- end
17
- def include? element
18
- @index ||= Hash.new
19
- @index[element] ? true : false
20
- end
21
- def initializers
22
- @initializers ||= Hash.new
23
- end
24
- end
25
-
26
- def attributes *a, &b
27
- unless a.empty?
28
- returned = Hash.new
29
-
30
- hashes, names = a.partition{|x| Hash === x}
31
- names_and_defaults = {}
32
- hashes.each{|h| names_and_defaults.update h}
33
- names.flatten.compact.each{|name| names_and_defaults.update name => nil}
34
-
35
- initializers = __attributes__.initializers
36
-
37
- names_and_defaults.each do |name, default|
38
- raise NameError, "bad instance variable name '@#{ name }'" if "@#{ name }" =~ %r/[!?=]$/o
39
- name = name.to_s
40
-
41
- initialize = b || lambda { default }
42
- initializer = lambda do |this|
43
- Object.instance_method('instance_eval').bind(this).call &initialize
44
- end
45
- initializer_id = initializer.object_id
46
- __attributes__.initializers[name] = initializer
47
-
48
- module_eval <<-code
49
- def #{ name }=(*value, &block)
50
- value.unshift block if block
51
- @#{ name } = value.first
52
- end
53
- code
54
-
55
- module_eval <<-code
56
- def #{ name }(*value, &block)
57
- value.unshift block if block
58
- return self.send('#{ name }=', value.first) unless value.empty?
59
- #{ name }! unless defined? @#{ name }
60
- @#{ name }
61
- end
62
- code
63
-
64
- module_eval <<-code
65
- def #{ name }!
66
- initializer = ObjectSpace._id2ref #{ initializer_id }
67
- self.#{ name } = initializer.call(self)
68
- @#{ name }
69
- end
70
- code
71
-
72
- module_eval <<-code
73
- def #{ name }?
74
- #{ name }
75
- end
76
- code
77
-
78
- attributes << name
79
- returned[name] = initializer
80
- end
81
-
82
- returned
83
- else
84
- begin
85
- __attribute_list__
86
- rescue NameError
87
- singleton_class =
88
- class << self
89
- self
90
- end
91
- klass = self
92
- singleton_class.module_eval do
93
- attribute_list = List.new
94
- define_method('attribute_list'){ klass == self ? attribute_list : raise(NameError) }
95
- alias_method '__attribute_list__', 'attribute_list'
96
- end
97
- __attribute_list__
98
- end
99
- end
100
- end
101
-
102
- %w( __attributes__ __attribute__ attribute ).each{|dst| alias_method dst, 'attributes'}
103
- end
104
-
105
- =begin
106
- class Object
107
- def attributes *a, &b
108
- sc =
109
- class << self
110
- self
111
- end
112
- sc.attributes *a, &b
113
- end
114
- %w( __attributes__ __attribute__ attribute ).each{|dst| alias_method dst, 'attributes'}
115
- end
116
- =end
117
-
118
- class Module
119
- include Attributes
120
- end
1
+ module Attributes
2
+ Attributes::VERSION = '5.0.0' unless defined? Attributes::VERSION
3
+ def self.version() Attributes::VERSION end
4
+
5
+ class List < ::Array
6
+ def << element
7
+ super
8
+ self
9
+ ensure
10
+ uniq!
11
+ index!
12
+ end
13
+ def index!
14
+ @index ||= Hash.new
15
+ each{|element| @index[element] = true}
16
+ end
17
+ def include? element
18
+ @index ||= Hash.new
19
+ @index[element] ? true : false
20
+ end
21
+ def initializers
22
+ @initializers ||= Hash.new
23
+ end
24
+ end
25
+
26
+ def attributes *a, &b
27
+ unless a.empty?
28
+ returned = Hash.new
29
+
30
+ hashes, names = a.partition{|x| Hash === x}
31
+ names_and_defaults = {}
32
+ hashes.each{|h| names_and_defaults.update h}
33
+ names.flatten.compact.each{|name| names_and_defaults.update name => nil}
34
+
35
+ initializers = __attributes__.initializers
36
+
37
+ names_and_defaults.each do |name, default|
38
+ raise NameError, "bad instance variable name '@#{ name }'" if "@#{ name }" =~ %r/[!?=]$/o
39
+ name = name.to_s
40
+
41
+ initialize = b || lambda { default }
42
+ initializer = lambda do |this|
43
+ Object.instance_method('instance_eval').bind(this).call &initialize
44
+ end
45
+ initializer_id = initializer.object_id
46
+ __attributes__.initializers[name] = initializer
47
+
48
+ module_eval <<-code
49
+ def #{ name }=(*value, &block)
50
+ value.unshift block if block
51
+ @#{ name } = value.first
52
+ end
53
+ code
54
+
55
+ module_eval <<-code
56
+ def #{ name }(*value, &block)
57
+ value.unshift block if block
58
+ return self.send('#{ name }=', value.first) unless value.empty?
59
+ #{ name }! unless defined? @#{ name }
60
+ @#{ name }
61
+ end
62
+ code
63
+
64
+ module_eval <<-code
65
+ def #{ name }!
66
+ initializer = ObjectSpace._id2ref #{ initializer_id }
67
+ self.#{ name } = initializer.call(self)
68
+ @#{ name }
69
+ end
70
+ code
71
+
72
+ module_eval <<-code
73
+ def #{ name }?
74
+ #{ name }
75
+ end
76
+ code
77
+
78
+ attributes << name
79
+ returned[name] = initializer
80
+ end
81
+
82
+ returned
83
+ else
84
+ begin
85
+ __attribute_list__
86
+ rescue NameError
87
+ singleton_class =
88
+ class << self
89
+ self
90
+ end
91
+ klass = self
92
+ singleton_class.module_eval do
93
+ attribute_list = List.new
94
+ define_method('attribute_list'){ klass == self ? attribute_list : raise(NameError) }
95
+ alias_method '__attribute_list__', 'attribute_list'
96
+ end
97
+ __attribute_list__
98
+ end
99
+ end
100
+ end
101
+
102
+ %w( __attributes__ __attribute__ attribute ).each{|dst| alias_method dst, 'attributes'}
103
+ end
104
+
105
+ =begin
106
+ class Object
107
+ def attributes *a, &b
108
+ sc =
109
+ class << self
110
+ self
111
+ end
112
+ sc.attributes *a, &b
113
+ end
114
+ %w( __attributes__ __attribute__ attribute ).each{|dst| alias_method dst, 'attributes'}
115
+ end
116
+ =end
117
+
118
+ class Module
119
+ include Attributes
120
+ end
data/lib/bj/bj.rb CHANGED
@@ -1,72 +1,72 @@
1
- class Bj
2
- module ClassMethods
3
- attribute("rails_root"){ Util.const_or_env("RAILS_ROOT"){ "." } }
4
- attribute("rails_env"){ Util.const_or_env("RAILS_ENV"){ "development" } }
5
- attribute("database_yml"){ File.join rails_root, "config", "database.yml" }
6
- attribute("configurations"){ YAML::load(ERB.new(IO.read(database_yml)).result) }
7
- attribute("tables"){ Table.list }
8
- attribute("hostname"){ Socket.gethostname }
9
- attribute("logger"){ Bj::Logger.off STDERR }
10
- attribute("ruby"){ Util.which_ruby }
11
- attribute("rake"){ Util.which_rake }
12
- attribute("script"){ Util.find_script "bj" }
13
- attribute("ttl"){ Integer(Bj::Table::Config["ttl"] || (twenty_four_hours = 24 * 60 * 60)) }
14
- attribute("table"){ Table }
15
- attribute("config"){ table.config }
16
- attribute("util"){ Util }
17
- attribute("runner"){ Runner }
18
- attribute("joblist"){ Joblist }
19
- attribute("default_path"){ %w'/bin /usr/bin /usr/local/bin /opt/local/bin'.join(File::PATH_SEPARATOR) }
20
-
21
- def transaction options = {}, &block
22
- options.to_options!
23
-
24
- cur_rails_env = Bj.rails_env.to_s
25
- new_rails_env = options[:rails_env].to_s
26
-
27
- cur_spec = configurations[cur_rails_env]
28
- table.establish_connection(cur_spec) unless table.connected?
29
-
30
- if(new_rails_env.empty? or cur_rails_env == new_rails_env)
31
- table.transaction{ block.call(table.connection) }
32
- else
33
- new_spec = configurations[new_rails_env]
34
- table.establish_connection(new_spec)
35
- Bj.rails_env = new_rails_env
36
- begin
37
- table.transaction{ block.call(table.connection) }
38
- ensure
39
- table.establish_connection(cur_spec)
40
- Bj.rails_env = cur_rails_env
41
- end
42
- end
43
- end
44
-
45
- def chroot options = {}, &block
46
- if defined? @chrooted and @chrooted
47
- return(block ? block.call(@chrooted) : @chrooted)
48
- end
49
- if block
50
- begin
51
- chrooted = @chrooted
52
- Dir.chdir(@chrooted = rails_root) do
53
- raise RailsRoot, "<#{ Dir.pwd }> is not a rails root" unless Util.valid_rails_root?(Dir.pwd)
54
- block.call(@chrooted)
55
- end
56
- ensure
57
- @chrooted = chrooted
58
- end
59
- else
60
- Dir.chdir(@chrooted = rails_root)
61
- raise RailsRoot, "<#{ Dir.pwd }> is not a rails root" unless Util.valid_rails_root?(Dir.pwd)
62
- @chrooted
63
- end
64
- end
65
-
66
- def boot
67
- load File.join(rails_root, "config", "boot.rb")
68
- load File.join(rails_root, "config", "environment.rb")
69
- end
70
- end
71
- send :extend, ClassMethods
72
- end
1
+ class Bj
2
+ module ClassMethods
3
+ attribute("rails_root"){ Util.const_or_env("RAILS_ROOT"){ "." } }
4
+ attribute("rails_env"){ Util.const_or_env("RAILS_ENV"){ "development" } }
5
+ attribute("database_yml"){ File.join rails_root, "config", "database.yml" }
6
+ attribute("configurations"){ YAML::load(ERB.new(IO.read(database_yml)).result) }
7
+ attribute("tables"){ Table.list }
8
+ attribute("hostname"){ Socket.gethostname }
9
+ attribute("logger"){ Bj::Logger.off STDERR }
10
+ attribute("ruby"){ Util.which_ruby }
11
+ attribute("rake"){ Util.which_rake }
12
+ attribute("script"){ Util.find_script "bj" }
13
+ attribute("ttl"){ Integer(Bj::Table::Config["ttl"] || (twenty_four_hours = 24 * 60 * 60)) }
14
+ attribute("table"){ Table }
15
+ attribute("config"){ table.config }
16
+ attribute("util"){ Util }
17
+ attribute("runner"){ Runner }
18
+ attribute("joblist"){ Joblist }
19
+ attribute("default_path"){ %w'/bin /usr/bin /usr/local/bin /opt/local/bin'.join(File::PATH_SEPARATOR) }
20
+
21
+ def transaction options = {}, &block
22
+ options.to_options!
23
+
24
+ cur_rails_env = Bj.rails_env.to_s
25
+ new_rails_env = options[:rails_env].to_s
26
+
27
+ cur_spec = configurations[cur_rails_env]
28
+ table.establish_connection(cur_spec) unless table.connected?
29
+
30
+ if(new_rails_env.empty? or cur_rails_env == new_rails_env)
31
+ table.transaction{ block.call(table.connection) }
32
+ else
33
+ new_spec = configurations[new_rails_env]
34
+ table.establish_connection(new_spec)
35
+ Bj.rails_env = new_rails_env
36
+ begin
37
+ table.transaction{ block.call(table.connection) }
38
+ ensure
39
+ table.establish_connection(cur_spec)
40
+ Bj.rails_env = cur_rails_env
41
+ end
42
+ end
43
+ end
44
+
45
+ def chroot options = {}, &block
46
+ if defined? @chrooted and @chrooted
47
+ return(block ? block.call(@chrooted) : @chrooted)
48
+ end
49
+ if block
50
+ begin
51
+ chrooted = @chrooted
52
+ Dir.chdir(@chrooted = rails_root) do
53
+ raise RailsRoot, "<#{ Dir.pwd }> is not a rails root" unless Util.valid_rails_root?(Dir.pwd)
54
+ block.call(@chrooted)
55
+ end
56
+ ensure
57
+ @chrooted = chrooted
58
+ end
59
+ else
60
+ Dir.chdir(@chrooted = rails_root)
61
+ raise RailsRoot, "<#{ Dir.pwd }> is not a rails root" unless Util.valid_rails_root?(Dir.pwd)
62
+ @chrooted
63
+ end
64
+ end
65
+
66
+ def boot
67
+ load File.join(rails_root, "config", "boot.rb")
68
+ load File.join(rails_root, "config", "environment.rb")
69
+ end
70
+ end
71
+ send :extend, ClassMethods
72
+ end
data/lib/bj/errors.rb CHANGED
@@ -1,4 +1,4 @@
1
- class Bj
2
- class Error < ::StandardError; end
3
- class RailsRoot < Error; end
4
- end
1
+ class Bj
2
+ class Error < ::StandardError; end
3
+ class RailsRoot < Error; end
4
+ end
data/lib/bj/joblist.rb CHANGED
@@ -1,112 +1,112 @@
1
- class Bj
2
- class JobList < ::Array
3
- module ClassMethods
4
- def for jobs, options = {}
5
- if Joblist === jobs
6
- jobs.update options
7
- return jobs
8
- end
9
- options.to_options!
10
- jobs = [jobs].flatten.compact
11
- list = []
12
- jobs.each do |arg|
13
- list.push(
14
- case arg
15
- when String
16
- case arg
17
- when %r/^\d+$/
18
- job_from_id arg
19
- else
20
- job_from_string arg
21
- end
22
- when Hash
23
- job_from_hash arg
24
- when Io
25
- jobs_from_io arg
26
- when Fixnum, Bignum
27
- job_from_number arg
28
- else
29
- job_from_string arg
30
- end
31
- )
32
- end
33
- list.flatten!
34
- list.compact!
35
- list.map!{|job| job.reverse_merge! options}
36
- list
37
- end
38
-
39
- def job_from_hash arg
40
- arg.to_hash.to_options!
41
- end
42
-
43
- def job_from_string arg
44
- unless arg.strip.empty?
45
- { :command => arg.to_s }
46
- else
47
- nil
48
- end
49
- end
50
-
51
- def job_from_number arg
52
- id = arg.to_i
53
- Table::Job.find(id).to_hash
54
- end
55
-
56
- def jobs_from_io arg
57
- if arg == "-"
58
- load_from_io STDIN
59
- else
60
- if arg.respond_to? :read
61
- load_from_io arg
62
- else
63
- open(arg, "r"){|fd| load_from_io fd}
64
- end
65
- end
66
- end
67
-
68
- def load_from_io io
69
- list = []
70
- io.each do |line|
71
- line.strip!
72
- next if line.empty?
73
- list << job_from_string(line)
74
- end
75
- list
76
- end
77
-
78
- def jobs_from_yaml arg
79
- object =
80
- if arg == "-"
81
- YAML.load STDIN
82
- else
83
- if arg.respond_to? :read
84
- YAML.load arg
85
- else
86
- open(arg, "r"){|fd| YAML.load fd}
87
- end
88
- end
89
- Joblist.for object
90
- end
91
- end
92
- send :extend, ClassMethods
93
-
94
- module InstanceMethods
95
- def update options = {}
96
- options.to_options!
97
- each{|job| job.update options}
98
- end
99
-
100
- def push other
101
- Joblist.for(other).each do |job|
102
- super job
103
- end
104
- self
105
- end
106
- alias_method "<<", "push"
107
- end
108
- send :include, InstanceMethods
109
- end
110
-
111
- Joblist = JobList
112
- end
1
+ class Bj
2
+ class JobList < ::Array
3
+ module ClassMethods
4
+ def for jobs, options = {}
5
+ if Joblist === jobs
6
+ jobs.update options
7
+ return jobs
8
+ end
9
+ options.to_options!
10
+ jobs = [jobs].flatten.compact
11
+ list = []
12
+ jobs.each do |arg|
13
+ list.push(
14
+ case arg
15
+ when String
16
+ case arg
17
+ when %r/^\d+$/
18
+ job_from_id arg
19
+ else
20
+ job_from_string arg
21
+ end
22
+ when Hash
23
+ job_from_hash arg
24
+ when Io
25
+ jobs_from_io arg
26
+ when Fixnum, Bignum
27
+ job_from_number arg
28
+ else
29
+ job_from_string arg
30
+ end
31
+ )
32
+ end
33
+ list.flatten!
34
+ list.compact!
35
+ list.map!{|job| job.reverse_merge! options}
36
+ list
37
+ end
38
+
39
+ def job_from_hash arg
40
+ arg.to_hash.to_options!
41
+ end
42
+
43
+ def job_from_string arg
44
+ unless arg.strip.empty?
45
+ { :command => arg.to_s }
46
+ else
47
+ nil
48
+ end
49
+ end
50
+
51
+ def job_from_number arg
52
+ id = arg.to_i
53
+ Table::Job.find(id).to_hash
54
+ end
55
+
56
+ def jobs_from_io arg
57
+ if arg == "-"
58
+ load_from_io STDIN
59
+ else
60
+ if arg.respond_to? :read
61
+ load_from_io arg
62
+ else
63
+ open(arg, "r"){|fd| load_from_io fd}
64
+ end
65
+ end
66
+ end
67
+
68
+ def load_from_io io
69
+ list = []
70
+ io.each do |line|
71
+ line.strip!
72
+ next if line.empty?
73
+ list << job_from_string(line)
74
+ end
75
+ list
76
+ end
77
+
78
+ def jobs_from_yaml arg
79
+ object =
80
+ if arg == "-"
81
+ YAML.load STDIN
82
+ else
83
+ if arg.respond_to? :read
84
+ YAML.load arg
85
+ else
86
+ open(arg, "r"){|fd| YAML.load fd}
87
+ end
88
+ end
89
+ Joblist.for object
90
+ end
91
+ end
92
+ send :extend, ClassMethods
93
+
94
+ module InstanceMethods
95
+ def update options = {}
96
+ options.to_options!
97
+ each{|job| job.update options}
98
+ end
99
+
100
+ def push other
101
+ Joblist.for(other).each do |job|
102
+ super job
103
+ end
104
+ self
105
+ end
106
+ alias_method "<<", "push"
107
+ end
108
+ send :include, InstanceMethods
109
+ end
110
+
111
+ Joblist = JobList
112
+ end