Class: RunningTrack::TrackCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/mosTrack/track_collection.rb

Defined Under Namespace

Classes: RunningTrackLoadLocalDataError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = Array.new) ⇒ TrackCollection

Returns a new instance of TrackCollection.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mosTrack/track_collection.rb', line 13

def initialize data = Array.new
	@tracks = data.map do |e|
		Track.new({
			Track::TRACK_DISTRICT_KEY => e["District"],
			Track::TRACK_ADDRESS_KEY => e["Address"],
			Track::TRACK_PHONE_KEY => e["HelpPhone"],
			Track::TRACK_HAS_WIFI_KEY => e["ObjectHasWifi"]
		})
	end
	# p find(Track::TRACK_HAS_WIFI_KEY, "нет")
	@tracks = random(@tracks.count)
end

Instance Attribute Details

#tracksObject (readonly)

Returns the value of attribute tracks.



8
9
10
# File 'lib/mosTrack/track_collection.rb', line 8

def tracks
  @tracks
end

Instance Method Details

#find(key, value) ⇒ Object



30
31
32
33
34
# File 'lib/mosTrack/track_collection.rb', line 30

def find key, value
	@tracks.select do |e|
		e.to_hash[key] == value
	end
end

#load!Object



40
41
42
43
44
45
46
# File 'lib/mosTrack/track_collection.rb', line 40

def load!
	local_data = YAML.load_file('./data/file.yml')
	raise RunningTrackLoadLocalDataError, "Локальных данных нет!" if !local_data
	@tracks = YAML.load_file('./data/file.yml').map do |e| 
		Track.new(e)
	end
end

#random(count = 1) ⇒ Object



26
27
28
# File 'lib/mosTrack/track_collection.rb', line 26

def random count = 1
	@tracks.shuffle.slice(0..count-1)
end

#save!Object



36
37
38
# File 'lib/mosTrack/track_collection.rb', line 36

def save!
	File.open('./data/file.yml', 'w') {|f| f.write self.to_hash.to_yaml }
end

#to_hashObject



48
49
50
51
52
# File 'lib/mosTrack/track_collection.rb', line 48

def to_hash
	hash = @tracks.map do |e|   
		e.to_hash
	end
end