Class: Plumb::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/plumb/key.rb

Constant Summary collapse

OPTIONAL_EXP =

OPTIONAL_EXP = /(w+)(?)?$/

/(?<word>[A-Za-z0-9_$]+)(?<qmark>\?)?/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_nameObject (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_keyObject (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_symObject (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

Returns:

  • (Boolean)


31
32
33
# File 'lib/plumb/key.rb', line 31

def eql?(other)
  other.hash == hash
end

#hashObject



27
28
29
# File 'lib/plumb/key.rb', line 27

def hash
  @to_key.hash
end

#inspectObject



39
40
41
# File 'lib/plumb/key.rb', line 39

def inspect
  "#{@to_key}#{'?' if @optional}"
end

#optional?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/plumb/key.rb', line 35

def optional?
  @optional
end

#to_sObject



25
# File 'lib/plumb/key.rb', line 25

def to_s = @to_key.to_s