Class: TDRB::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/tdrb/token.rb

Constant Summary collapse

DATETIME =
"Date / Time".freeze
FAIL =
"Fails".freeze
LOAD =
"Load".freeze
METAKEY =
"Metadata Key".freeze
METAVALUE =
"Metadata Value".freeze
MOVEMENT =
"Movement".freeze
NOTE =
"Note".freeze
REP =
"Reps".freeze
SET =
"Sets".freeze
SUPERSET =
"Superset Movement".freeze
TOKEN_SYMBOLS =
%i[
  datetime fail load metakey metavalue movement note rep set superset
]
ALLOWED_TYPES =
TOKEN_SYMBOLS.map { |ts| const_get(ts.upcase) }.freeze
EOF =
"\x00".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(literal:, type:) ⇒ Token

Returns a new instance of Token.

Raises:



32
33
34
35
36
37
# File 'lib/tdrb/token.rb', line 32

def initialize(literal:, type:)
  raise TokenTypeError if !ALLOWED_TYPES.include?(type)

  @literal = literal
  @type    = type
end

Instance Attribute Details

#literalObject (readonly)

Returns the value of attribute literal.



3
4
5
# File 'lib/tdrb/token.rb', line 3

def literal
  @literal
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/tdrb/token.rb', line 3

def type
  @type
end

Instance Method Details

#==(t) ⇒ Object

Raises:



39
40
41
42
43
# File 'lib/tdrb/token.rb', line 39

def ==(t)
  raise TokenArgsError.new("Invalid comparison") if !t.is_a? Token

  literal == t.literal && type == t.type
end

#===(t) ⇒ Object

Raises:



45
46
47
48
49
# File 'lib/tdrb/token.rb', line 45

def ===(t)
  raise TokenTypeError if !ALLOWED_TYPES.include?(t)

  type == t
end

#movement?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/tdrb/token.rb', line 51

def movement?
  @movement ||= [MOVEMENT, SUPERSET].include?(type)
end

#performance?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/tdrb/token.rb', line 55

def performance?
  @performance ||= [FAIL, LOAD, REP, SET].include?(type)
end

#to_sObject



59
60
61
# File 'lib/tdrb/token.rb', line 59

def to_s
  "[#{type}] #{literal}"
end