class SciolyFF::Interpreter

Interprets the YAML representation of a SciolyFF file through objects that respond to idiomatic Ruby method calls

Attributes

events[R]
penalties[R]
placings[R]
teams[R]
tournament[R]

Public Class Methods

new(rep) click to toggle source
# File lib/sciolyff/interpreter.rb, line 19
def initialize(rep)
  if rep.instance_of? String
    rep = Psych.safe_load(rep,
                          permitted_classes: [Date],
                          symbolize_names: true)
  end
  create_models(@rep = rep)
  link_models(self)

  sort_events_naturally
  sort_teams_by_rank
end

Public Instance Methods

subdivisions() click to toggle source
# File lib/sciolyff/interpreter.rb, line 32
def subdivisions
  @subdivisions ||=
    teams.map(&:subdivision)
         .uniq
         .compact
         .map { |sub| [sub, Interpreter.new(subdivision_rep(sub))] }
         .to_h
end

Private Instance Methods

create_models(rep) click to toggle source
# File lib/sciolyff/interpreter.rb, line 43
def create_models(rep)
  @tournament = Tournament.new(rep)
  @events     = map_array_to_models rep[:Events],    Event,   rep
  @teams      = map_array_to_models rep[:Teams],     Team,    rep
  @placings   = map_array_to_models rep[:Placings],  Placing, rep
  @penalties  = map_array_to_models rep[:Penalties], Penalty, rep
end
map_array_to_models(arr, object_class, rep) click to toggle source
# File lib/sciolyff/interpreter.rb, line 51
def map_array_to_models(arr, object_class, rep)
  return [] if arr.nil?

  arr.map.with_index { |_, index| object_class.new(rep, index) }
end
sort_events_naturally() click to toggle source
# File lib/sciolyff/interpreter.rb, line 66
def sort_events_naturally
  @events.sort_by! { |e| [e.trial?.to_s, e.name] }
end
sort_teams_by_rank() click to toggle source
# File lib/sciolyff/interpreter.rb, line 70
def sort_teams_by_rank
  sorted =
    @teams
    .group_by { |t| [t.disqualified?.to_s, t.exhibition?.to_s] }
    .map { |key, teams| [key, sort_teams_by_points(teams)] }
    .sort_by(&:first)
    .map(&:last)
    .flatten
  @teams.map!.with_index { |_, i| sorted[i] }
end