Module: TDRB
- Defined in:
- lib/tdrb.rb,
lib/tdrb/error.rb,
lib/tdrb/lexer.rb,
lib/tdrb/parse.rb,
lib/tdrb/token.rb,
lib/tdrb/parser.rb,
lib/tdrb/result.rb,
lib/tdrb/version.rb,
lib/tdrb/performance.rb
Defined Under Namespace
Classes: Error, Lexer, ParseError, Parser, Performance, Result, Token, TokenArgsError, TokenError, TokenTypeError
Constant Summary collapse
- VERSION =
"1.0.0".freeze
Class Method Summary collapse
-
.parse(traindown:) ⇒ Object
Returns Array<Result> to allow for multisession documents.
- .safe_float(maybe_float_str) ⇒ Object
- .safe_time(maybe_time_str) ⇒ Object
Class Method Details
.parse(traindown:) ⇒ Object
Returns Array<Result> to allow for multisession documents.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/tdrb/parse.rb', line 5 def self.parse(traindown:) = nil = {} movement_notes = [] result = nil performance = nil Parser .new(traindown: traindown) .tokens .each_with_object([]) do |token, arr| case token.type when Token::DATETIME arr << result if !result.nil? occurred_at = safe_time(token.literal) result = Result.new(occurred_at: occurred_at) when Token::FAIL fails = safe_float(token.literal) (performance || Performance.new).fails = fails when Token::LOAD if performance.nil? || performance.load result.add_performance(performance) if performance&.load performance = Performance.new( movement: performance&.movement, load: safe_float(token.literal), ) else performance.load = safe_float(token.literal) end when Token::METAKEY = token.literal target = performance.nil? ? result. : target[] = "" when Token::METAVALUE target = performance.nil? ? result. : target[] = token.literal = nil when Token::MOVEMENT if !performance.nil? result .add_performance( performance, metadata: , notes: movement_notes, ) end performance = Performance.new(movement: token.literal) when Token::NOTE target = performance.nil? ? result.notes : movement_notes target << token.literal when Token::REP reps = safe_float(token.literal) (performance || Performance.new).reps = reps when Token::SET sets = safe_float(token.literal) (performance || Performance.new).sets = sets when Token::SUPERSET end end.tap do |results| return results if result.nil? result.add_performance(performance) if !performance.nil? results << result end end |
.safe_float(maybe_float_str) ⇒ Object
78 79 80 81 82 |
# File 'lib/tdrb/parse.rb', line 78 def self.safe_float(maybe_float_str) Float(maybe_float_str) rescue 0.0 end |
.safe_time(maybe_time_str) ⇒ Object
84 85 86 87 88 |
# File 'lib/tdrb/parse.rb', line 84 def self.safe_time(maybe_time_str) Time.parse(maybe_time_str) rescue Time.now end |