Class: Serienrenamer::InformationStore

Inherits:
Object
  • Object
show all
Defined in:
lib/serienrenamer/information_store.rb

Overview

this class holds a storage for episode information (seriesname) in form of a yaml file which can be used from other tools

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml_path, byte_count = 2048) ⇒ InformationStore

this method will load an exisiting yaml file and tries to rebuild the used hash with episode data



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

def initialize(yaml_path, byte_count=2048)
    @yaml_path = yaml_path
    @byte_count = byte_count.to_i
    @episode_hash = {}

    if File.file?(yaml_path)
        serialized = YAML.load(File.new(yaml_path, "rb").read)
        if serialized.is_a?(Hash)
          @episode_hash = serialized
        end
    end
end

Instance Attribute Details

#byte_countObject (readonly)

Returns the value of attribute byte_count.



9
10
11
# File 'lib/serienrenamer/information_store.rb', line 9

def byte_count
  @byte_count
end

#episode_hashObject (readonly)

Returns the value of attribute episode_hash.



9
10
11
# File 'lib/serienrenamer/information_store.rb', line 9

def episode_hash
  @episode_hash
end

Instance Method Details

#store(episode) ⇒ Object

this method will store the information of the supplied episode instance to the file

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
# File 'lib/serienrenamer/information_store.rb', line 28

def store(episode)
    raise ArgumentError, "Episode instance needed" unless
        episode.is_a? Serienrenamer::Episode

    md5sum = episode.md5sum(@byte_count)

    unless @episode_hash[md5sum]
        @episode_hash[md5sum] = episode.series
    end
end

#writeObject

this method will write the current hash of episodes to the yaml file



41
42
43
44
45
# File 'lib/serienrenamer/information_store.rb', line 41

def write()
    storage_file = File.new(@yaml_path, "w")
    storage_file.write(@episode_hash.to_yaml)
    storage_file.close
end