scripting 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/examples/simple.rb CHANGED
@@ -2,8 +2,6 @@ require File.expand_path('../../lib/scripting', __FILE__)
2
2
 
3
3
  MyApp = Scripting::create_application do
4
4
  plugin Scripting::AppDefaults
5
- uses :default_switches
6
- uses :default_options
7
5
 
8
6
  options do
9
7
  message { "Hello World" }
@@ -1,27 +1,22 @@
1
1
  module Scripting
2
2
  module AppDefaults
3
3
 
4
- def self.included(base)
5
- base.class_eval do
6
- plugins :default_switches, :default_options
7
- end
8
- end
9
-
10
- def default_options(context)
11
- context.describe do
4
+ def self.instance_init(instance, *args)
5
+ instance.instance_eval do
12
6
  options do
13
7
  verbose false
8
+ debug false
14
9
  end
15
- end
16
- end
17
10
 
18
- def default_switches(context)
19
- context.describe do
20
11
  switches do
21
12
  on_tail('-v', '--verbose', 'Enable verbose output') do
22
13
  options.verbose = true
23
14
  end
24
15
 
16
+ on_tail('-D', '--debug', 'Enable debug output') do
17
+ options.debug = true
18
+ end
19
+
25
20
  on_tail('-h', '--help', "Help text") do
26
21
  STDERR.puts self
27
22
  context.clear_work!
@@ -13,7 +13,6 @@ module Scripting
13
13
  clear_work!
14
14
  end
15
15
 
16
-
17
16
  def context
18
17
  self
19
18
  end
@@ -50,21 +49,6 @@ module Scripting
50
49
  @work
51
50
  end
52
51
 
53
- def plugin module_name
54
- self.class.send(:include, module_name)
55
- end
56
-
57
- def uses *names
58
- names.each do |name|
59
- name = name.to_sym
60
- if installed_plugins.include? name
61
- send(name, self)
62
- else
63
- raise "unknown method '#{name.inspect}' provided in uses"
64
- end
65
- end
66
- end
67
-
68
52
  def parse! args
69
53
  switches.parse! args
70
54
  args
@@ -80,7 +64,7 @@ module Scripting
80
64
  end
81
65
 
82
66
  def self.create_application &blk
83
- app = Application.new
67
+ app = Class.new(Application).new
84
68
  app.describe &blk
85
69
  app
86
70
  end
@@ -21,7 +21,7 @@ module Scripting
21
21
 
22
22
  def method_missing(name, value = nil, &blk)
23
23
  name = name.to_s
24
- name = name[0..-2] if name =~ /=$/
24
+ name.chop! if name =~ /=$/
25
25
 
26
26
  if block_given?
27
27
  self[name] = blk
@@ -1,35 +1,40 @@
1
1
  module Scripting
2
2
  module Pluggable
3
3
  module ClassMethods
4
- def meta
5
- (class << self; self; end)
4
+ def plugins
5
+ @__plugins__ ||= []
6
6
  end
7
7
 
8
- def plugin name
9
- plugins = self.meta.instance_variable_get("@__plugins__")
10
- plugins << name
11
- self.meta.instance_variable_set("@__plugins__", plugins)
12
- end
13
-
14
- def plugins *names
15
- names.each { |name| self.plugin(name) }
16
- self.meta.instance_variable_get("@__plugins__")
17
- end
8
+ def plugin mod, *args
9
+ current = self.plugins
10
+ unless current.include? mod
11
+ current << mod
12
+ include(mod::InstanceMethods) if mod.const_defined? "InstanceMethods"
13
+ extend(mod::ClassMethods) if mod.const_defined? "ClassMethods"
18
14
 
19
- def self.extended(base)
20
- base.meta.instance_variable_set("@__plugins__", [])
15
+ if mod.respond_to? :class_init
16
+ mod.class_init(self, *args)
17
+ end
18
+ end
21
19
  end
22
20
  end
23
21
 
24
22
  module InstanceMethods
25
- def installed_plugins
23
+ def plugins
26
24
  self.class.plugins
27
25
  end
26
+
27
+ def plugin mod, *args
28
+ self.class.plugin mod, *args
29
+ if mod.respond_to? :instance_init
30
+ mod.instance_init(self, *args)
31
+ end
32
+ end
28
33
  end
29
34
 
30
35
  def self.extended(base)
31
- base.send(:extend, ClassMethods)
32
36
  base.send(:include, InstanceMethods)
37
+ base.send(:extend, ClassMethods)
33
38
  end
34
39
  end
35
40
  end
data/scripting.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{scripting}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["sdeming"]
12
- s.date = %q{2011-03-07}
12
+ s.date = %q{2011-03-09}
13
13
  s.description = %q{Simplified command line scripting tool}
14
14
  s.email = %q{sdeming@makefile.com}
15
15
  s.extra_rdoc_files = [
@@ -34,22 +34,25 @@ Gem::Specification.new do |s|
34
34
  "spec/app_defaults_spec.rb",
35
35
  "spec/application_spec.rb",
36
36
  "spec/options_spec.rb",
37
+ "spec/pluggable_spec.rb",
37
38
  "spec/spec_helper.rb"
38
39
  ]
39
40
  s.homepage = %q{http://github.com/sdeming/scripting}
40
41
  s.licenses = ["MIT"]
41
42
  s.require_paths = ["lib"]
42
- s.rubygems_version = %q{1.5.2}
43
+ s.rubygems_version = %q{1.3.7}
43
44
  s.summary = %q{Simplified command line scripting tool}
44
45
  s.test_files = [
45
46
  "examples/simple.rb",
46
47
  "spec/app_defaults_spec.rb",
47
48
  "spec/application_spec.rb",
48
49
  "spec/options_spec.rb",
50
+ "spec/pluggable_spec.rb",
49
51
  "spec/spec_helper.rb"
50
52
  ]
51
53
 
52
54
  if s.respond_to? :specification_version then
55
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
53
56
  s.specification_version = 3
54
57
 
55
58
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -10,47 +10,20 @@ describe Scripting::AppDefaults do
10
10
  STDERR.stub(:write) { |*args| @stderr << args.join }
11
11
  end
12
12
 
13
- it "should define default options when using AppDefaults plugin" do
13
+ it "should define default options and switches when using AppDefaults plugin" do
14
14
  app = Scripting::create_application do
15
15
  plugin Scripting::AppDefaults
16
- uses :default_options
17
16
  end
18
17
  app.options.verbose.should be_false
19
- end
20
-
21
- it "should define default switches when using AppDefaults plugin" do
22
- app = Scripting::create_application do
23
- plugin Scripting::AppDefaults
24
- uses :default_switches
25
- end
26
- app.options.verbose.should be_nil
27
- app.parse! ["-v"]
18
+ app.options.debug.should be_false
19
+ app.parse! ["-vD"]
28
20
  app.options.verbose.should be_true
29
- end
30
-
31
- it "should define both default options and switches when using AppDefaults plugin" do
32
- app1 = Scripting::create_application do
33
- plugin Scripting::AppDefaults
34
- uses :default_switches
35
- uses :default_options
36
- end
37
- app1.options.verbose.should be_false
38
- app1.parse! ["-v"]
39
- app1.options.verbose.should be_true
40
-
41
- app2 = Scripting::create_application do
42
- plugin Scripting::AppDefaults
43
- uses :default_switches, :default_options
44
- end
45
- app2.options.verbose.should be_false
46
- app2.parse! ["-v"]
47
- app2.options.verbose.should be_true
21
+ app.options.debug.should be_true
48
22
  end
49
23
 
50
24
  it "should terminate command line processing and clear work when -h is passed with default switches" do
51
25
  app = Scripting::create_application do
52
26
  plugin Scripting::AppDefaults
53
- uses :default_switches, :default_options
54
27
  work { puts 7 * 6 }
55
28
  end
56
29
  @stderr.should be_empty
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ describe Scripting::Pluggable do
4
+ it "should apply both ClassMethods and InstanceMethods" do
5
+ module PluginA
6
+ module ClassMethods
7
+ def my_method; :class_method_result; end
8
+ end
9
+ module InstanceMethods
10
+ def my_method; :instance_method_result; end
11
+ end
12
+ end
13
+
14
+ exemplar = Scripting::Application.new
15
+ exemplar.plugins.should be_empty
16
+ lambda { exemplar.class.my_method }.should raise_error(NoMethodError)
17
+ lambda { exemplar.my_method }.should raise_error(NoMethodError)
18
+
19
+ with_plugin = Scripting::create_application do
20
+ plugin PluginA
21
+ end
22
+ with_plugin.plugins.should == [PluginA]
23
+ with_plugin.class.my_method.should == :class_method_result
24
+ with_plugin.my_method.should == :instance_method_result
25
+
26
+ without_plugin = Scripting::create_application do
27
+ end
28
+ without_plugin.plugins.should be_empty
29
+ lambda { without_plugin.class.my_method }.should raise_error(NoMethodError)
30
+ lambda { without_plugin.my_method }.should raise_error(NoMethodError)
31
+ end
32
+
33
+ it "should apply only ClassMethods if InstanceMethods are undeclared" do
34
+ module PluginB
35
+ module ClassMethods
36
+ def my_method; :class_method_result; end
37
+ end
38
+ end
39
+
40
+ app = Scripting::create_application do
41
+ plugin PluginB
42
+ end
43
+ app.plugins.should == [PluginB]
44
+ app.class.my_method.should == :class_method_result
45
+ lambda { app.my_method }.should raise_error(NoMethodError)
46
+ end
47
+
48
+ it "should apply only InstanceMethods if ClassMethods are undeclared" do
49
+ module PluginC
50
+ module InstanceMethods
51
+ def my_method; :instance_method_result; end
52
+ end
53
+ end
54
+
55
+ app = Scripting::create_application do
56
+ plugin PluginC
57
+ end
58
+ app.plugins.should == [PluginC]
59
+ lambda { app.class.my_method }.should raise_error(NoMethodError)
60
+ app.my_method.should == :instance_method_result
61
+ end
62
+
63
+ it "should run class_init and instance_init of they are declared" do
64
+ module PluginD
65
+ def self.class_init(base, *args)
66
+ base.class_eval do
67
+ def self.foo; 0xdeadbeef; end
68
+ end
69
+ end
70
+
71
+ def self.instance_init(instance, *args)
72
+ instance.instance_eval do
73
+ def foo; 0x8ba11; end
74
+ end
75
+ end
76
+ end
77
+
78
+ app = Scripting::create_application do
79
+ plugin PluginD
80
+ end
81
+
82
+ app.plugins.should == [PluginD]
83
+ app.class.foo.should == 0xdeadbeef
84
+ app.foo.should == 0x8ba11
85
+ end
86
+
87
+ it "should allow multiple plugins" do
88
+ module PluginE
89
+ end
90
+
91
+ module PluginF
92
+ end
93
+
94
+ app = Scripting::create_application do
95
+ plugin PluginE
96
+ plugin PluginF
97
+ end
98
+
99
+ app.plugins.should == [PluginE, PluginF]
100
+ end
101
+ end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scripting
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease:
4
+ prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
11
10
  platform: ruby
12
11
  authors:
13
12
  - sdeming
@@ -15,55 +14,52 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-03-07 00:00:00 -05:00
17
+ date: 2011-03-09 00:00:00 -05:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: rspec
23
- version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ requirement: &id001 !ruby/object:Gem::Requirement
24
23
  none: false
25
24
  requirements:
26
25
  - - ">="
27
26
  - !ruby/object:Gem::Version
28
- hash: 27
29
27
  segments:
30
28
  - 2
31
29
  - 5
32
30
  - 0
33
31
  version: 2.5.0
34
- prerelease: false
35
32
  type: :development
36
- requirement: *id001
33
+ prerelease: false
34
+ version_requirements: *id001
37
35
  - !ruby/object:Gem::Dependency
38
36
  name: jeweler
39
- version_requirements: &id002 !ruby/object:Gem::Requirement
37
+ requirement: &id002 !ruby/object:Gem::Requirement
40
38
  none: false
41
39
  requirements:
42
40
  - - ~>
43
41
  - !ruby/object:Gem::Version
44
- hash: 7
45
42
  segments:
46
43
  - 1
47
44
  - 5
48
45
  - 2
49
46
  version: 1.5.2
50
- prerelease: false
51
47
  type: :development
52
- requirement: *id002
48
+ prerelease: false
49
+ version_requirements: *id002
53
50
  - !ruby/object:Gem::Dependency
54
51
  name: rcov
55
- version_requirements: &id003 !ruby/object:Gem::Requirement
52
+ requirement: &id003 !ruby/object:Gem::Requirement
56
53
  none: false
57
54
  requirements:
58
55
  - - ">="
59
56
  - !ruby/object:Gem::Version
60
- hash: 3
61
57
  segments:
62
58
  - 0
63
59
  version: "0"
64
- prerelease: false
65
60
  type: :development
66
- requirement: *id003
61
+ prerelease: false
62
+ version_requirements: *id003
67
63
  description: Simplified command line scripting tool
68
64
  email: sdeming@makefile.com
69
65
  executables: []
@@ -91,6 +87,7 @@ files:
91
87
  - spec/app_defaults_spec.rb
92
88
  - spec/application_spec.rb
93
89
  - spec/options_spec.rb
90
+ - spec/pluggable_spec.rb
94
91
  - spec/spec_helper.rb
95
92
  has_rdoc: true
96
93
  homepage: http://github.com/sdeming/scripting
@@ -106,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
103
  requirements:
107
104
  - - ">="
108
105
  - !ruby/object:Gem::Version
109
- hash: 3
106
+ hash: -4157022527390188045
110
107
  segments:
111
108
  - 0
112
109
  version: "0"
@@ -115,14 +112,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
112
  requirements:
116
113
  - - ">="
117
114
  - !ruby/object:Gem::Version
118
- hash: 3
119
115
  segments:
120
116
  - 0
121
117
  version: "0"
122
118
  requirements: []
123
119
 
124
120
  rubyforge_project:
125
- rubygems_version: 1.5.2
121
+ rubygems_version: 1.3.7
126
122
  signing_key:
127
123
  specification_version: 3
128
124
  summary: Simplified command line scripting tool
@@ -131,4 +127,5 @@ test_files:
131
127
  - spec/app_defaults_spec.rb
132
128
  - spec/application_spec.rb
133
129
  - spec/options_spec.rb
130
+ - spec/pluggable_spec.rb
134
131
  - spec/spec_helper.rb