Class: Plumb::Key
- Inherits:
-
Object
- Object
- Plumb::Key
- Defined in:
- lib/plumb/key.rb
Constant Summary collapse
- OPTIONAL_EXP =
OPTIONAL_EXP = /(w+)(?)?$/
/(?<word>[A-Za-z0-9_$]+)(?<qmark>\?)?/
Instance Attribute Summary collapse
-
#node_name ⇒ Object
readonly
Returns the value of attribute node_name.
-
#to_key ⇒ Object
readonly
Returns the value of attribute to_key.
-
#to_sym ⇒ Object
readonly
Returns the value of attribute to_sym.
Class Method Summary collapse
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(key, optional: false, symbolize: false) ⇒ Key
constructor
A new instance of Key.
- #inspect ⇒ Object
- #optional? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(key, optional: false, symbolize: false) ⇒ Key
Returns a new instance of Key.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/plumb/key.rb', line 14 def initialize(key, optional: false, symbolize: false) key_type = symbolize ? Symbol : key.class match = OPTIONAL_EXP.match(key.to_s) key = match[:word] @to_key = key_type == Symbol ? key.to_sym : key @to_sym = @to_key.to_sym @optional = !match[:qmark].nil? ? true : optional @node_name = :key freeze end |
Instance Attribute Details
#node_name ⇒ Object (readonly)
Returns the value of attribute node_name.
12 13 14 |
# File 'lib/plumb/key.rb', line 12 def node_name @node_name end |
#to_key ⇒ Object (readonly)
Returns the value of attribute to_key.
12 13 14 |
# File 'lib/plumb/key.rb', line 12 def to_key @to_key end |
#to_sym ⇒ Object (readonly)
Returns the value of attribute to_sym.
12 13 14 |
# File 'lib/plumb/key.rb', line 12 def to_sym @to_sym end |
Class Method Details
.wrap(key, symbolize: false) ⇒ Object
8 9 10 |
# File 'lib/plumb/key.rb', line 8 def self.wrap(key, symbolize: false) key.is_a?(Key) ? key : new(key, symbolize:) end |
Instance Method Details
#eql?(other) ⇒ Boolean
31 32 33 |
# File 'lib/plumb/key.rb', line 31 def eql?(other) other.hash == hash end |
#hash ⇒ Object
27 28 29 |
# File 'lib/plumb/key.rb', line 27 def hash @to_key.hash end |
#inspect ⇒ Object
39 40 41 |
# File 'lib/plumb/key.rb', line 39 def inspect "#{@to_key}#{'?' if @optional}" end |
#optional? ⇒ Boolean
35 36 37 |
# File 'lib/plumb/key.rb', line 35 def optional? @optional end |
#to_s ⇒ Object
25 |
# File 'lib/plumb/key.rb', line 25 def to_s = @to_key.to_s |