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



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aniview/interface/pref/pref.rb', line 29

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.



20
21
22
# File 'lib/aniview/interface/pref/pref.rb', line 20

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



65
66
67
# File 'lib/aniview/interface/pref/pref.rb', line 65

def get(key)
  @pref[key] if @pref.key?(key)
end

#get_pref(trail) ⇒ Object



102
103
104
# File 'lib/aniview/interface/pref/pref.rb', line 102

def get_pref(trail)
  trail.inject(@pref, :fetch)
end

#itemsObject

Returns an array of PrefItems.

Returns:

  • an array of PrefItems



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/aniview/interface/pref/pref.rb', line 141

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



175
176
177
178
179
# File 'lib/aniview/interface/pref/pref.rb', line 175

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



50
51
52
53
54
55
56
# File 'lib/aniview/interface/pref/pref.rb', line 50

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

#pref_type(trail) ⇒ Object



98
99
100
# File 'lib/aniview/interface/pref/pref.rb', line 98

def pref_type(trail)
  trail.inject(@validations, :fetch)
end

#read(file) ⇒ Object

Returns parsed json.

Parameters:

  • file

    The file

Returns:

  • parsed json



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/aniview/interface/pref/pref.rb', line 188

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



164
165
166
# File 'lib/aniview/interface/pref/pref.rb', line 164

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/aniview/interface/pref/pref.rb', line 78

def set(trail, destination)
  trail = [trail] if trail.class == String

  destination = case destination
                when "true"
                  true
                when "false"
                  false
                else
                  destination
                end

  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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/aniview/interface/pref/pref.rb', line 115

def valid_preference?(trail, destination)
  skeys = [ "space", "up", "down", "left", "right", "enter" ]

  case pref_type(trail)
  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 "boolean"
    destination.class == TrueClass or destination.class == FalseClass
  when "locked"
    false
  else 
    true
  end
end