Class: Aniview::Interface::Pref
- Includes:
- Observable
- Defined in:
- lib/aniview/interface/pref/pref.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
-
#get(key) ⇒ Object
The preference at key or nil if there is no such preference.
-
#initialize(logger: nil) ⇒ Object
constructor
A new pref object.
-
#items ⇒ Object
An array of PrefItems.
-
#load ⇒ Object
Nil.
-
#parseDir(path) ⇒ Object
The parsed directory string.
-
#read(file) ⇒ Object
Parsed json.
-
#save ⇒ Object
Nil.
-
#set(trail, destination) ⇒ Object
Nil.
-
#valid_preference?(trail, destination) ⇒ Boolean
True if preference is valid, false otherwise.
Methods inherited from Bridge
Constructor Details
#initialize(logger: nil) ⇒ Object
Returns A new pref object.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/aniview/interface/pref/pref.rb', line 28 def initialize(logger: nil) @logger = logger conf = Dir.home + "/.config/aniview" @default_file = File.join(File.dirname(__FILE__), "/defaults.json") @pref_file = conf + "/aniview.json" validation_file = File.join(File.dirname(__FILE__), "/validate.json") @validations = read(validation_file) FileUtils.mkdir_p(conf) unless File.directory?(conf) load save unless File.exist?(@pref_file) end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
19 20 21 |
# File 'lib/aniview/interface/pref/pref.rb', line 19 def logger @logger end |
Instance Method Details
#get(key) ⇒ Object
Returns the preference at key or nil if there is no such preference.
64 65 66 |
# File 'lib/aniview/interface/pref/pref.rb', line 64 def get(key) @pref[key] if @pref.key?(key) end |
#items ⇒ Object
Returns an array of PrefItems.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/aniview/interface/pref/pref.rb', line 120 def items ignore = { "local_anime" => true, "schedule" => true, } @pref.map{ |title, preference| next if ignore.key? title if preference.class == Hash preference.map { |subtitle, subpreference| PrefItem.new(title + "_" + subtitle, subpreference, [title, subtitle]) } else preference = preference.join(":") if preference.class == Array PrefItem.new(title, preference, [title]) end }.flatten.compact end |
#load ⇒ Object
Returns nil.
154 155 156 157 158 |
# File 'lib/aniview/interface/pref/pref.rb', line 154 def load @pref = read(@pref_file) || read(@default_file) changed notify_observers end |
#parseDir(path) ⇒ Object
Returns the parsed directory string.
49 50 51 52 53 54 55 |
# File 'lib/aniview/interface/pref/pref.rb', line 49 def parseDir(path) { "$airing_dir" => @pref["airing_dir"], "$conf_dir" => @pref["conf_dir"], "~" => Dir.home }.inject(path) { |path, r| path.gsub(r[0], r[1]) } end |
#read(file) ⇒ Object
Returns parsed json.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/aniview/interface/pref/pref.rb', line 167 def read(file) begin File.open(file, "r") { |f| json = f.read if json.empty? return nil else begin JSON.parse json rescue JSON::ParserError raise Aniview::Error::InvalidPref.new end end } rescue Errno::ENOENT return nil end end |
#save ⇒ Object
Returns nil.
143 144 145 |
# File 'lib/aniview/interface/pref/pref.rb', line 143 def save File.open(@pref_file, "w") { |f| f.write(JSON.pretty_generate(@pref)) } end |
#set(trail, destination) ⇒ Object
Returns nil.
77 78 79 80 81 82 83 84 85 |
# File 'lib/aniview/interface/pref/pref.rb', line 77 def set(trail, destination) trail = [trail] if trail.class == String return unless valid_preference?(trail, destination) trail[0...-1].inject(@pref, :fetch)[trail.last] = destination save changed notify_observers end |
#valid_preference?(trail, destination) ⇒ Boolean
Returns true if preference is valid, false otherwise.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/aniview/interface/pref/pref.rb', line 96 def valid_preference?(trail, destination) skeys = [ "space", "up", "down", "left", "right", "enter" ] case trail.inject(@validations, :fetch) when "key" destination.length == 1 or skeys.include?(destination) when "path" destination.split(":").map { |d| Dir.exist?(parseDir d) }.reduce { |m, n| m and n } when "color" Aniview::View::Color.respond_to?(destination) when "int" destination.scan(/\D/).empty? and destination.length > 1 when "locked" false else true end end |