options-arg 0.0.1

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.
Files changed (2) hide show
  1. data/lib/options_arg.rb +74 -0
  2. metadata +66 -0
@@ -0,0 +1,74 @@
1
+ #
2
+ # 16 Jul 2012
3
+ #
4
+
5
+ # Provides a few methods to work with hashes.
6
+ module Kernel
7
+
8
+ # Raised if a required option has not been set.
9
+ class OptionNotSetError < Exception
10
+ end
11
+
12
+ private
13
+
14
+ # Get a value from the specified hash.
15
+ #
16
+ # @param [Hash] options the hash.
17
+ # @param [Symbol] key the key whose value is required.
18
+ # @param [TrueClass, FalseClass] required flag to indicate if it is an error if the key does not exist.
19
+ # @param [Object] default the default value if the key does not exist. Only used if 'required' is false.
20
+ # If a block is provided, it is called with the options hash and requested key and the results of the block is
21
+ # returned.
22
+ #
23
+ # @return [Object] the value from the hash, or the default value.
24
+ def get_option(options, key, required = false, default = nil)
25
+ if options.has_key?(key)
26
+ options[key]
27
+ elsif required
28
+ raise OptionNotSetError.new("Option '#{key}' has not been set!")
29
+ elsif block_given?
30
+ yield(options, key)
31
+ else
32
+ default
33
+ end
34
+ end
35
+
36
+ # Pops (delete) a value from the specified hash.
37
+ #
38
+ # @param [Hash] options the hash.
39
+ # @param [Symbol] key the key whose value is required.
40
+ # @param [TrueClass, FalseClass] required flag to indicate if it is an error if the key does not exist.
41
+ # @param [Object] default the default value if the key does not exist. Only used if 'required' is false.
42
+ # If a block is provided, it is called with the options hash and requested key and the results of the block is
43
+ # returned.
44
+ #
45
+ # @return [Object] the value from the hash, or the default value.
46
+ def pop_option(options, key, required = false, default = nil)
47
+ if options.has_key?(key)
48
+ options.delete(key)
49
+ elsif required
50
+ raise OptionNotSetError.new("Option '#{key}' has not been set!")
51
+ elsif block_given?
52
+ yield(options, key)
53
+ else
54
+ default
55
+ end
56
+ end
57
+
58
+ # Sets a value in the specified hash.
59
+ #
60
+ # @param [Hash] options the hash.
61
+ # @param [Symbol] key the key whose value is to be set.
62
+ # @param [Object] value the value to set.
63
+ # @param [TrueClass, FalseClass] overwrite flag to indicate if existing values should be overwritten.
64
+ #
65
+ # @return [Object] the value in the hash at the end of the operation.
66
+ def set_option(options, key, value, overwrite)
67
+ if not options.has_key?(key) or overwrite
68
+ options[key] = value
69
+ end
70
+ # Return whatever is in the hash now.
71
+ options[key]
72
+ end
73
+
74
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: options-arg
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Suraj Vijayakumar
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-07-24 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Gem to add a few methods to the Kernel class to make it easier to work with hashes as arguments in methods.
22
+ email:
23
+ - vijayakumar.suraj@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/options_arg.rb
32
+ homepage:
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.24
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Gem to make it easier to work with hashes as arguments in methods.
65
+ test_files: []
66
+