Class: Aniview::Interface::Pref

Inherits:
Bridge
  • Object
show all
Includes:
Observable
Defined in:
lib/aniview/interface/pref/pref.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Bridge

#make_hash

Constructor Details

#initialize(logger: nil) ⇒ Object

Returns A new pref object.

Parameters:

  • logger (defaults to: nil)

    The logger



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

#loggerObject

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.

Parameters:

  • key

    The key

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

#itemsObject

Returns an array of PrefItems.

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

#loadObject

Returns nil.

Parameters:

  • file

    The file

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.

Parameters:

  • path

    The path to parse

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.

Parameters:

  • file

    The file

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

#saveObject

Returns nil.

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.

Parameters:

  • trail

    The key or array of keys that specify where the preference is stored

  • destination

    The new value for the preference

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.

Parameters:

  • trail

    The array of keys that specify where the preference is stored

  • destination

    The new value for the preference

Returns:

  • (Boolean)

    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