Class: Plumb::Key

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, optional: false) ⇒ Key

Returns a new instance of Key.



13
14
15
16
17
18
19
20
21
# File 'lib/plumb/key.rb', line 13

def initialize(key, optional: false)
  key_s = key.to_s
  match = OPTIONAL_EXP.match(key_s)
  @node_name = :key
  @key = match[1]
  @to_sym = @key.to_sym
  @optional = !match[2].nil? ? true : optional
  freeze
end

Instance Attribute Details

#node_nameObject (readonly)

Returns the value of attribute node_name.



11
12
13
# File 'lib/plumb/key.rb', line 11

def node_name
  @node_name
end

#to_symObject (readonly)

Returns the value of attribute to_sym.



11
12
13
# File 'lib/plumb/key.rb', line 11

def to_sym
  @to_sym
end

Class Method Details

.wrap(key) ⇒ Object



7
8
9
# File 'lib/plumb/key.rb', line 7

def self.wrap(key)
  key.is_a?(Key) ? key : new(key)
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/plumb/key.rb', line 29

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

#hashObject



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

def hash
  @key.hash
end

#inspectObject



37
38
39
# File 'lib/plumb/key.rb', line 37

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

#optional?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/plumb/key.rb', line 33

def optional?
  @optional
end

#to_sObject



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

def to_s = @key