clive 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,12 @@
1
+ unless defined?(KeyError)
2
+ class KeyError < IndexError; end
3
+ end
4
+
1
5
  class Clive
2
6
 
3
- # For general errors with Clive. It stripts most of the backtrace which
7
+ # For general errors with Clive. It stripts most of the backtrace which
4
8
  # you don't really want, and allows you to set nice error messages
5
- # using {.reason}. Arguments can be passed and then used in messages by
9
+ # using {.reason}. Arguments can be passed and then used in messages by
6
10
  # referencing with +#n+ tokens, where +n+ is the index of the argument.
7
11
  #
8
12
  # A lot of this is pulled from OptionParser::ParseError see
@@ -18,13 +22,13 @@ class Clive
18
22
  #
19
23
  class Error < StandardError
20
24
  attr_accessor :args
21
-
25
+
22
26
  # @param args
23
27
  # Arguments that can be accessed with '#n' in {.reason}.
24
28
  def initialize(*args)
25
29
  @args = args
26
30
  end
27
-
31
+
28
32
  # Removes all references to files which are not the file being run
29
33
  # unless in $DEBUG mode.
30
34
  def self.filter_backtrace(array)
@@ -33,22 +37,22 @@ class Clive
33
37
  end
34
38
  array
35
39
  end
36
-
40
+
37
41
  # Set the reason for the error class.
38
42
  # @param str [String]
39
43
  def self.reason(str)
40
44
  @reason = str
41
45
  end
42
-
46
+
43
47
  # Accessor for the reason set with {.reason}.
44
48
  def self._reason
45
49
  @reason
46
50
  end
47
-
51
+
48
52
  def set_backtrace(array)
49
53
  super(self.class.filter_backtrace(array))
50
54
  end
51
-
55
+
52
56
  # Build the message by substituting the arguments into the reason.
53
57
  def message
54
58
  self.class._reason.gsub(/#\d/) do |i|
@@ -61,6 +65,6 @@ class Clive
61
65
  end
62
66
  end
63
67
  alias_method :to_s, :message
64
-
68
+
65
69
  end
66
- end
70
+ end
@@ -1,9 +1,11 @@
1
1
  class Clive
2
2
 
3
3
  # Methods for modifying a state object. Requires that the instance variable
4
- # +@state+ exists and that it responds to +#fetch+, +#store+ and +#key?+.
4
+ # +@state+ exists and that it responds to +#[]+, +#store+ and +#key?+.
5
5
  module StateActions
6
6
 
7
+ # Gets the value for the key, returns nil if the key does not exist.
8
+ #
7
9
  # @param key [Symbol]
8
10
  #
9
11
  # @example
@@ -13,9 +15,23 @@ class Clive
13
15
  # end
14
16
  #
15
17
  def get(key)
16
- @state.fetch key
18
+ @state[key]
19
+ end
20
+
21
+ # Gets the value for the key, raises an error if the key does not exist.
22
+ #
23
+ # @param key [Symbol]
24
+ # @raise [KeyError]
25
+ def get!(key)
26
+ if has?(key)
27
+ get(key)
28
+ else
29
+ raise KeyError, "key not found: #{key}"
30
+ end
17
31
  end
18
32
 
33
+ # Sets the key to the value given.
34
+ #
19
35
  # @param key [Symbol]
20
36
  # @param value [Object]
21
37
  #
@@ -45,13 +45,13 @@ class Clive
45
45
  @data[keys.first] = val
46
46
  end
47
47
 
48
- # Gets the value from the StructHash corresponding to the key given.
48
+ # Gets the value from the StructHash corresponding to the key given. Returns
49
+ # nil if the key given does not exist.
49
50
  #
50
51
  # @param key [Symbol]
51
- def fetch(key)
52
- @data.fetch @aliases[key]
52
+ def [](key)
53
+ @data[@aliases[key]]
53
54
  end
54
- alias_method :[], :fetch
55
55
 
56
56
  # Checks whether the StructHash contains an entry for the key given.
57
57
  def key?(key)
@@ -87,7 +87,7 @@ class Clive
87
87
  # Otherwise calls super.
88
88
  def method_missing(sym, *args, &block)
89
89
  if key?(sym)
90
- fetch sym
90
+ self[sym]
91
91
  elsif sym.to_s[-1..-1] == "?"
92
92
  key? sym.to_s[0..-2].to_sym
93
93
  else
@@ -158,8 +158,9 @@ class Clive
158
158
 
159
159
  def typecast(arg)
160
160
  parts = arg.split('/')
161
- mods = parts.pop
162
- arg = parts.join('')
161
+ parts << '' if parts.size < 3
162
+
163
+ _, arg, mods = parts
163
164
  mods = mods.split('').map {|a| OPTS[a] }.inject{|a,e| a | e }
164
165
 
165
166
  ::Regexp.new arg, mods
@@ -1,3 +1,3 @@
1
1
  class Clive
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -14,9 +14,29 @@ describe Clive::Option::Runner do
14
14
 
15
15
  describe '#get' do
16
16
  it 'gets a value from state' do
17
+ r = nil
18
+ subject._run({}, {:a => 1}, proc { r = get(:a) })
19
+ r.must_equal 1
20
+ end
21
+
22
+ it 'returns nil if key does not exist' do
23
+ r = true
24
+ subject._run({}, {:a => 1}, proc { r = get(:b) })
25
+ r.must_equal nil
26
+ end
27
+ end
28
+
29
+ describe '#get!' do
30
+ it 'gets a value from the state' do
31
+ r = nil
32
+ subject._run({}, {:a => 1}, proc { r = get!(:a) })
33
+ r.must_equal 1
34
+ end
35
+
36
+ it 'raises an error if key does not exist' do
17
37
  this {
18
- subject._run({}, {:a => 1}, proc { puts get(:a) })
19
- }.must_output "1\n"
38
+ subject._run({}, {:a => 1}, proc { r = get!(:b) })
39
+ }.must_raise KeyError
20
40
  end
21
41
  end
22
42
 
@@ -6,9 +6,14 @@ describe Clive::StructHash do
6
6
  subject { Clive::StructHash }
7
7
 
8
8
  describe '#[]' do
9
+ let(:s) { subject.new(:a => 1) }
10
+
9
11
  it 'gets the value' do
10
- sh = subject.new(:a => 1)
11
- sh[:a].must_equal 1
12
+ s[:a].must_equal 1
13
+ end
14
+
15
+ it 'returns nil if key does not exist' do
16
+ s[:b].must_equal nil
12
17
  end
13
18
  end
14
19
 
@@ -305,6 +305,9 @@ describe Clive::Type::Regexp do
305
305
 
306
306
  describe '#typecast' do
307
307
  it 'returns a Regexp' do
308
+ subject.typecast('/a|b/').must_be_kind_of Regexp
309
+ subject.typecast('/a|b/').must_equal /a|b/
310
+
308
311
  subject.typecast('/a/i').must_be_kind_of Regexp
309
312
  subject.typecast('/a/ix').must_equal /a/ix
310
313
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clive
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-15 00:00:00.000000000 Z
12
+ date: 2012-06-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &2152595180 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '2.6'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2152595180
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.6'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: mocha
27
- requirement: &2152592780 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
@@ -32,7 +37,12 @@ dependencies:
32
37
  version: '0.10'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *2152592780
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.10'
36
46
  description: ! " Clive provides a DSL for building command line interfaces. It
37
47
  allows \n you to define commands and options, which can also take arguments,
38
48
  \n and then runs the correct stuff!\n"
@@ -100,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
110
  version: '0'
101
111
  requirements: []
102
112
  rubyforge_project:
103
- rubygems_version: 1.8.11
113
+ rubygems_version: 1.8.23
104
114
  signing_key:
105
115
  specification_version: 3
106
116
  summary: A DSL for building command line interfaces.
@@ -123,4 +133,3 @@ test_files:
123
133
  - spec/extras/expectations.rb
124
134
  - spec/extras/focus.rb
125
135
  - spec/helper.rb
126
- has_rdoc: