Class: Aniview::Interface::Pref

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

Instance Method Summary collapse

Constructor Details

#initializePref

Returns a new instance of Pref.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aniview/interface/pref/pref.rb', line 10

def initialize
    
  @home = Dir.home
  @conf = @home + "/.config/aniview"
    
  File.open(File.join(File.dirname(__FILE__), "/validate.json"), "r") {
    |f| @validate = JSON.parse(f.read)
  }
    
  FileUtils.mkdir_p(@conf) unless File.directory?(@conf)
    
  @pref_file = @conf + "/aniview.json"
    
  self.defaults if not File.exist?(@pref_file)
  self.load
end

Instance Method Details

#defaults(should_save: true) ⇒ Object



37
38
39
40
# File 'lib/aniview/interface/pref/pref.rb', line 37

def defaults(should_save:true)
  File.open(File.join(File.dirname(__FILE__), "/defaults.json"), "r") {|f| @pref = JSON.parse(f.read)}
  self.save if should_save
end

#get(s) ⇒ Object



105
106
107
# File 'lib/aniview/interface/pref/pref.rb', line 105

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

#getAllObject



109
110
111
112
113
114
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 109

def getAll
  ignore = {
    "local_anime" => true,
    "schedule" => true,
  }
    
  r = {}
  @pref.each{ |item| 
    next if ignore.key? item[0]
    title = item[0]
    if item[1].class == String
      val = item[1]
    elsif item[1].class == Array
      val = item[1].join(":")
    elsif item[1].class == Hash
      item[1].each { |subitem|
        subtitle = title + "_" + subitem[0]
        subval   = subitem[1]
        r.merge!(PrefItem.new(subtitle, subval, []) => [PrefItem.new("", "", [item[0], subitem[0]])])
      }
      next
    end
    r.merge!(PrefItem.new(title, val, []) => [ PrefItem.new("", "", [item[0]]) ] )
  }
  return r
end

#loadObject



140
141
142
143
144
145
146
# File 'lib/aniview/interface/pref/pref.rb', line 140

def load
  begin
    File.open(@pref_file, "r") {|f| @pref = JSON.parse(f.read)}
    #rescue JSON::ParserError
    #defaults should_save: false
  end
end

#parseDir(path) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/aniview/interface/pref/pref.rb', line 27

def parseDir path
  path = path.gsub("$airing_dir", @pref["airing_dir"])
  path = path.gsub("$conf_dir", @pref["conf_dir"])
  path = path.gsub("~", Dir.home)

  path += "/" if path[-1] != "/"

  return path
end

#saveObject



136
137
138
# File 'lib/aniview/interface/pref/pref.rb', line 136

def save
  File.open(@pref_file, "w") { |f| f.write(JSON.pretty_generate(@pref)) }
end

#set(s, val) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/aniview/interface/pref/pref.rb', line 42

def set(s, val)
  trail = [s] if s.class == String
  trail = s   if s.class == Array
  if valpref(trail, val, @validate)
    @pref = setpref(trail, val, @pref)
  end
    
  save
end

#setpref(trail, destination, map) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/aniview/interface/pref/pref.rb', line 52

def setpref(trail, destination, map)
  if trail.length == 1
    map[trail[0]] = destination
    return map
  else
    
    blaze = trail[0]
    map[blaze] = setpref(trail[1..-1], destination, map[blaze])
    
  end
  return map
end

#valpref(trail, destination, map) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/aniview/interface/pref/pref.rb', line 65

def valpref(trail, destination, map)
  
  if trail.length == 1
    skeys = [
      "space",
      "up",
      "down",
      "left",
      "right",
      "enter"
    ]
    
    case map[trail[0]]
      when "key"
        return false if destination.length != 1 and not skeys.include? destination

      when "path"
        destination.split(":").each { |d|
          return false if not Dir.exist?(parseDir d)
        }

      when "color"
        return false if not Aniview::View::Color.respond_to? destination

      when "int"
        return false if not destination.scan(/\D/).empty? and destination.length > 1
    
      when "locked"
        return false
    
    end
    
    return true
    
  else
    blaze = trail[0]
    return valpref(trail[1..-1], destination, map[blaze])
  end
end