Class: UncleKryon::Trainers

Inherits:
Object
  • Object
show all
Defined in:
lib/unclekryon/trainer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath = nil) ⇒ Trainers

Returns a new instance of Trainers.



117
118
119
120
# File 'lib/unclekryon/trainer.rb', line 117

def initialize(filepath=nil)
  @filepath = filepath
  @trainers = {}
end

Instance Attribute Details

#filepathObject

Returns the value of attribute filepath.



114
115
116
# File 'lib/unclekryon/trainer.rb', line 114

def filepath
  @filepath
end

#trainersObject

Returns the value of attribute trainers.



115
116
117
# File 'lib/unclekryon/trainer.rb', line 115

def trainers
  @trainers
end

Instance Method Details

#[](id) ⇒ Object



156
157
158
# File 'lib/unclekryon/trainer.rb', line 156

def [](id)
  @trainers[id]
end

#[]=(id, trainer) ⇒ Object



160
161
162
# File 'lib/unclekryon/trainer.rb', line 160

def []=(id,trainer)
  @trainers[id] = trainer
end

#load_fileObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/unclekryon/trainer.rb', line 122

def load_file
  if @filepath.nil? || (@filepath = @filepath.strip).empty?
    raise ArgumentError,'Training filepath cannot be empty'
  end

  if File.exist?(@filepath)
    y = YAML.load_file(@filepath)

    y.each do |id,trainer|
      if !@trainers.key?(id)
        @trainers[id] = trainer
      else
        @trainers[id].tags = trainer.tags.merge(@trainers[id].tags)
        @trainers[id].trainer = trainer.trainer
      end

      @trainers[id].trainer.reset_after_import
      @trainers[id].init_lengths
    end
  end
end

#save_to_fileObject



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/unclekryon/trainer.rb', line 144

def save_to_file
  if @filepath.nil? || (@filepath = @filepath.strip).empty?
    raise ArgumentError,'Training filepath cannot be empty'
  end

  Util.mk_dirs_from_filepath(@filepath)

  File.open(@filepath,'w') do |f|
    f.write(to_s)
  end
end

#to_sObject



164
165
166
# File 'lib/unclekryon/trainer.rb', line 164

def to_s
  return YAML.dump(@trainers)
end