Class: Rubsh::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/rubsh/option.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(k, v, positional:, is_special_kwarg: nil) ⇒ Option

Returns a new instance of Option.

Parameters:

  • k (String, Symbol, #to_s)
  • v (nil, Boolean, Numeric, String, Symbol, Proc)


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

#kString (readonly)

Returns:

  • (String)


5
6
7
# File 'lib/rubsh/option.rb', line 5

def k
  @k
end

#vObject (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

Overloads:

  • .build(option) ⇒ Option

    Parameters:

  • .build(name) ⇒ Option

    Parameters:

    • name (String, Symbol, #to_s)
  • .build(name, value) ⇒ Option

    Parameters:

    • name (String, Symbol, #to_s)
    • value (nil, Boolean, Numeric, String, Symbol, Proc)

Returns:



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

Returns:

  • (Boolean)


61
62
63
# File 'lib/rubsh/option.rb', line 61

def kwarg?
  !positional?
end

#positional?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/rubsh/option.rb', line 57

def positional?
  @is_positional
end

#special_kwarg?(sk = nil) ⇒ Boolean

Returns:

  • (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