Class: Pirka::Library
- Inherits:
-
Object
- Object
- Pirka::Library
- 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
-
.additional_directories ⇒ Object
Returns the value of attribute additional_directories.
-
.data_home ⇒ Object
Returns the value of attribute data_home.
Instance Attribute Summary collapse
-
#codelist ⇒ Object
readonly
Returns the value of attribute codelist.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Class Method Summary collapse
-
.basename_without_ext(release_identifier) ⇒ String
String that
Release Identifier
property in metadata is encoded based on RFC 4648 "Base 64 Encoding with URL and Filename Safe Alphabet". - .data_directory(user = nil) ⇒ Object
- .directories(user = nil) ⇒ Array<Pathname>
- .filename(release_identifier) ⇒ Object
- .find_by_release_identifier(release_identifier) ⇒ Library?
- .load_file(path) ⇒ Library
- .load_hash(h) ⇒ Library
Instance Method Summary collapse
- #data_directory(user = nil) ⇒ Object
-
#each ⇒ Object
Iterate over codelist in order of EPUB CFI.
- #filename ⇒ Object
-
#initialize ⇒ Library
constructor
A new instance of Library.
-
#save(path = nil) ⇒ Pathname
File path that library data was saved.
- #to_h ⇒ Hash
- #to_yaml ⇒ String
Constructor Details
#initialize ⇒ Library
Returns a new instance of Library.
102 103 104 105 |
# File 'lib/pirka/library.rb', line 102 def initialize @metadata = {} @codelist = {} end |
Class Attribute Details
.additional_directories ⇒ Object
Returns the value of attribute additional_directories.
31 32 33 |
# File 'lib/pirka/library.rb', line 31 def additional_directories @additional_directories end |
.data_home ⇒ Object
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
#codelist ⇒ Object (readonly)
Returns the value of attribute codelist.
99 100 101 |
# File 'lib/pirka/library.rb', line 99 def codelist @codelist end |
#metadata ⇒ Object (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
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".
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>
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?
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
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
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 #each ⇒ Enumerator
Iterate over codelist in order of EPUB CFI
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 |
#filename ⇒ Object
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.
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_h ⇒ 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_yaml ⇒ String
154 155 156 |
# File 'lib/pirka/library.rb', line 154 def to_yaml to_h.to_yaml end |