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

#defaultsObject



34
35
36
37
# File 'lib/aniview/interface/pref/pref.rb', line 34

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

#get(s) ⇒ Object



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

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

#getAllObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/aniview/interface/pref/pref.rb', line 106

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



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

def load
	File.open(@pref_file, "r") {|f| @pref = JSON.parse(f.read)}
end

#parseDir(path) ⇒ Object



27
28
29
30
31
32
# 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)
   return path
end

#saveObject



133
134
135
# File 'lib/aniview/interface/pref/pref.rb', line 133

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

#set(s, val) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/aniview/interface/pref/pref.rb', line 39

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



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aniview/interface/pref/pref.rb', line 49

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



62
63
64
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
# File 'lib/aniview/interface/pref/pref.rb', line 62

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