Class: Nametrainer::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/nametrainer/collection.rb

Overview

A class for a collection of persons, each with a name, a corresponding (image) file, and a score.

Create a Collection instance with

collection = CollectionLoader.load(
               :directory  => directory,
               :extensions => extensions
             )

You can get a random person from a collection with

person = collection.sample

Persons with a lower score are chosen more often than persons with a higher score.

Constant Summary collapse

SCORE_FILE =
'nametrainer.dat'

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Collection

Creates a Collection instance. Expects an argument hash with:

:persons - array of Person instances :directory - collection directory :rng - random number generator, defaults to RNG



37
38
39
40
41
# File 'lib/nametrainer/collection.rb', line 37

def initialize(args)
  @collection = args[:persons]
  @directory  = args[:directory]
  @rng        = args[:rng] || RNG.new(:size => @collection.size, :weighting_factor => 6)
end

Instance Method Details

#delete_scoresObject

Delete score file.



76
77
78
79
# File 'lib/nametrainer/collection.rb', line 76

def delete_scores
  filename = File.expand_path("#{@directory}/#{SCORE_FILE}")
  File.delete(filename)
end

#export_scoresObject

Export all scores to file (YAML).



70
71
72
73
# File 'lib/nametrainer/collection.rb', line 70

def export_scores
  filename = File.expand_path("#{@directory}/#{SCORE_FILE}")
  File.open(filename, 'w') {|f| f.write(scores.to_yaml) }
end

#import_scoresObject

Loads the scores from file (YAML).



63
64
65
66
67
# File 'lib/nametrainer/collection.rb', line 63

def import_scores
  filename = File.expand_path("#{@directory}/#{SCORE_FILE}")
  new_scores = YAML::load(File.read filename)
  set_scores(new_scores)
end

#namesObject

Returns an array of all names.



44
45
46
# File 'lib/nametrainer/collection.rb', line 44

def names
  map {|person| person.name }
end

#sampleObject

Returns a random element, preferring persons with a smaller score.



82
83
84
# File 'lib/nametrainer/collection.rb', line 82

def sample
  shuffle.sort[@rng.rand]  # shuffle first to mix up elements with equal scores
end

#scoresObject

Returns a hash with the score of all persons (name => score).



49
50
51
# File 'lib/nametrainer/collection.rb', line 49

def scores
  Hash[map {|person| [person.name, person.score] }]
end

#set_scores(new_scores) ⇒ Object

Sets the score of some or all persons.

new_scores - hash with (name => score) values



56
57
58
59
60
# File 'lib/nametrainer/collection.rb', line 56

def set_scores(new_scores)
  each do |person|
    person.score = new_scores[person.name]  if new_scores[person.name]
  end
end

#successor(element) ⇒ Object

Returns the successor of the specified element, if possible, or the first element.

element - element whose successor should be returned



90
91
92
93
94
95
# File 'lib/nametrainer/collection.rb', line 90

def successor(element)
  element_index = index(element)
  return first  unless element_index

  self[element_index + 1] || first
end