Class: Rubsh::Option
- Inherits:
-
Object
- Object
- Rubsh::Option
- Defined in:
- lib/rubsh/option.rb
Instance Attribute Summary collapse
- #k ⇒ String readonly
-
#v ⇒ Object
readonly
Returns the value of attribute v.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(k, v, positional:, is_special_kwarg: nil) ⇒ Option
constructor
A new instance of Option.
- #kwarg? ⇒ Boolean
- #positional? ⇒ Boolean
- #special_kwarg?(sk = nil) ⇒ Boolean
Constructor Details
#initialize(k, v, positional:, is_special_kwarg: nil) ⇒ Option
Returns a new instance of Option.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rubsh/option.rb', line 35 def initialize(k, v, positional:, is_special_kwarg: nil) @is_positional = positional @is_special_kwarg = is_special_kwarg.nil? ? false : is_special_kwarg if positional @k, @v = k.to_s, nil else case k when ::String @k, @v = k, v when ::Symbol if k.to_s[0] == "_" @k, @v = k.to_s, v @is_special_kwarg = true else @k, @v = k.to_s.tr("_", "-"), v end else raise ::ArgumentError, format("unsupported option type `%s (%s)'", k, k.class) end end end |
Instance Attribute Details
#k ⇒ String (readonly)
5 6 7 |
# File 'lib/rubsh/option.rb', line 5 def k @k end |
#v ⇒ Object (readonly)
Returns the value of attribute v.
8 9 10 |
# File 'lib/rubsh/option.rb', line 8 def v @v end |
Class Method Details
.build(option) ⇒ Option .build(name) ⇒ Option .build(name, value) ⇒ Option
24 25 26 27 28 29 30 31 |
# File 'lib/rubsh/option.rb', line 24 def self.build(*args) if args[0].is_a?(Option) o = args[0] new(o.k, o.v, positional: o.positional?, is_special_kwarg: o.special_kwarg?) else new(args[0], args[1], positional: args.length < 2) end end |
Instance Method Details
#kwarg? ⇒ Boolean
61 62 63 |
# File 'lib/rubsh/option.rb', line 61 def kwarg? !positional? end |
#positional? ⇒ Boolean
57 58 59 |
# File 'lib/rubsh/option.rb', line 57 def positional? @is_positional end |
#special_kwarg?(sk = nil) ⇒ Boolean
65 66 67 68 69 70 71 |
# File 'lib/rubsh/option.rb', line 65 def special_kwarg?(sk = nil) if sk.nil? @is_special_kwarg else @is_special_kwarg && k == sk.to_s end end |