Class: Pirka::Library

Inherits:
Object
  • Object
show all
Includes:
Enumerable, GetText
Defined in:
lib/pirka/library.rb

Overview

Environment variables affect this class: XDG_DATA_HOME: Directory to store library files and read. Defaults to $HOME/.local/share XDG_DATA_DIRS: Directories to search library files. Separated with a colon ':'. Defaults to /usr/local/share:/usr/share

Constant Summary collapse

DIR_NAME =
"pirka/local"
EXT =
".yaml"
SUBDIR_LENGTH =
4
XDG_DATA_HOME =
Pathname.new(".local/share")
DATA_HOME =
XDG_DATA_HOME/DIR_NAME
XDG_DATA_DIRS =
[Pathname.new("/usr/local/share"), Pathname.new("/usr/share")]
DATA_DIRS =
XDG_DATA_DIRS.collect {|dir| dir/DIR_NAME}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLibrary

Returns a new instance of Library.

Parameters:

  • directory (Pathname, String, nil)

    for library files. When nil passed, default directories are used



102
103
104
105
# File 'lib/pirka/library.rb', line 102

def initialize
  @metadata = {}
  @codelist = {}
end

Class Attribute Details

.additional_directoriesObject

Returns the value of attribute additional_directories.



31
32
33
# File 'lib/pirka/library.rb', line 31

def additional_directories
  @additional_directories
end

.data_homeObject

Returns the value of attribute data_home.



31
32
33
# File 'lib/pirka/library.rb', line 31

def data_home
  @data_home
end

Instance Attribute Details

#codelistObject (readonly)

Returns the value of attribute codelist.



99
100
101
# File 'lib/pirka/library.rb', line 99

def codelist
  @codelist
end

#metadataObject (readonly)

Returns the value of attribute metadata.



99
100
101
# File 'lib/pirka/library.rb', line 99

def 
  @metadata
end

Class Method Details

.basename_without_ext(release_identifier) ⇒ String

TODO:

Better name

Returns String that Release Identifier property in metadata is encoded based on RFC 4648 "Base 64 Encoding with URL and Filename Safe Alphabet".

Parameters:

  • release_identifier (String)

    Release Identifier

Returns:

  • (String)

    String that Release Identifier property in metadata is encoded based on RFC 4648 "Base 64 Encoding with URL and Filename Safe Alphabet"

See Also:



64
65
66
# File 'lib/pirka/library.rb', line 64

def basename_without_ext(release_identifier)
  Base64.urlsafe_encode64(release_identifier)
end

.data_directory(user = nil) ⇒ Object



45
46
47
# File 'lib/pirka/library.rb', line 45

def data_directory(user = nil)
  directories.first
end

.directories(user = nil) ⇒ Array<Pathname>

Returns:

  • (Array<Pathname>)


34
35
36
37
38
39
40
41
42
# File 'lib/pirka/library.rb', line 34

def directories(user = nil)
  data_dirs = ENV["XDG_DATA_DIRS"] ?
                ENV["XDG_DATA_DIRS"].split(":").collect {|dir| Pathname.new(dir)/DIR_NAME} :
                DATA_DIRS
  data_home = ENV["XDG_DATA_HOME"] ?
                Pathname.new(ENV["XDG_DATA_HOME"])/DIR_NAME :
                Pathname.new(Dir.home(user))/DATA_HOME
  ([@data_home, data_home] + @additional_directories + data_dirs).compact
end

.filename(release_identifier) ⇒ Object



68
69
70
71
72
# File 'lib/pirka/library.rb', line 68

def filename(release_identifier)
  name = basename_without_ext(release_identifier)
  name.insert(SUBDIR_LENGTH, "/") if name.length > SUBDIR_LENGTH
  name + EXT
end

.find_by_release_identifier(release_identifier) ⇒ Library?

Parameters:

  • release_identifier (String)

Returns:



51
52
53
54
55
56
57
58
# File 'lib/pirka/library.rb', line 51

def find_by_release_identifier(release_identifier)
  lib_path = filename(release_identifier)
  directories.each do |dir|
    path = dir/lib_path
    return load_file(path) if path.file?
  end
  nil
end

.load_file(path) ⇒ Library

Parameters:

  • path (Pathname, String)

Returns:



76
77
78
# File 'lib/pirka/library.rb', line 76

def load_file(path)
  load_hash(YAML.load_file(path.to_s))
end

.load_hash(h) ⇒ Library

Parameters:

  • h (Hash)

Returns:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pirka/library.rb', line 82

def load_hash(h)
  library = new

  h.each_pair do |key, value|
    if key == "codelist"
      value.each_pair do |cfi, data|
        library.codelist[EPUB::CFI.parse(cfi)] = data
      end
    else
      library.[key] = value
    end
  end

  library
end

Instance Method Details

#data_directory(user = nil) ⇒ Object



107
108
109
# File 'lib/pirka/library.rb', line 107

def data_directory(user = nil)
  self.class.data_directory(user)
end

#each {|cfi, language| ... } ⇒ Object #eachEnumerator

Iterate over codelist in order of EPUB CFI

Overloads:

  • #each {|cfi, language| ... } ⇒ Object

    Yield Parameters:

    • cfi (EPUB::CFI)

      EPUB CFI indicating code element

    • language (String)

      Language name

  • #eachEnumerator

    Returns Enumerator which iterates over cfi and lang.

    Returns:

    • (Enumerator)

      Enumerator which iterates over cfi and lang



133
134
135
136
137
138
139
140
141
142
# File 'lib/pirka/library.rb', line 133

def each
  sorted_list = @codelist.each_pair.sort_by {|(cfi, data)| cfi}
  if block_given?
    sorted_list.each do |(cfi, data)|
      yield cfi, data
    end
  else
    sorted_list.each
  end
end

#filenameObject



111
112
113
114
# File 'lib/pirka/library.rb', line 111

def filename
  raise _("Release Identifier is not set") unless @metadata["Release Identifier"]
  self.class.filename(@metadata["Release Identifier"])
end

#save(path = nil) ⇒ Pathname

Returns File path that library data was saved.

Parameters:

  • path (Pathname, String, nil) (defaults to: nil)

    File path to save library data. When nil is passwd, default directory + filename determined by Release Identifier is used

Returns:

  • (Pathname)

    File path that library data was saved



119
120
121
122
123
124
125
# File 'lib/pirka/library.rb', line 119

def save(path = nil)
  path = data_directory/filename unless path
  path = Pathname(path)
  path.dirname.mkpath unless path.dirname.directory?
  path.write to_yaml
  path
end

#to_hHash

Returns:

  • (Hash)


145
146
147
148
149
150
151
# File 'lib/pirka/library.rb', line 145

def to_h
  .merge({
    "codelist" => each.with_object({}) {|(cfi, value), list|
      list[cfi.to_s] = value
    }
  })
end

#to_yamlString

Returns:

  • (String)


154
155
156
# File 'lib/pirka/library.rb', line 154

def to_yaml
  to_h.to_yaml
end