Class: Pref

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

Instance Method Summary collapse

Constructor Details

#initializePref

Returns a new instance of Pref.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/aniview/util/pref.rb', line 4

def initialize

	@home = Dir.home
	@conf = @home + "/.aw"

	dirname = File.dirname(@conf)
	unless File.directory?(dirname)
	  FileUtils.mkdir_p(dirname)
	end

	@pref_file = @conf + "/r_anime"

	self.defaults if not File.exist?(@pref_file)
	self.load
end

Instance Method Details

#defaultsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/aniview/util/pref.rb', line 20

def defaults
	@pref = Hash[
		"anime_locations" => @home + "/airing/shows/" + ":" + @home + "/anime/",		
		"clr" => Hash[
			"main"     => "blue",
			"primary"  => "white",
			"secondary"=> "cyan", 
			"selected" => "red"
		],
		"menu_titles" => {
			"1" => "unwatched",
			"2" => "anime library",
			"3" => "MyAnimeList",

			"7" => "torrents",
			"8" => "subscriptions",
			"9" => "schedule",
			"0" => "preferences"
		},
		"anime_library_title_format_str"                   => " %t - $r@ %D %S ",
		"anime_library_title_show_format_str"              => " %t - %c@ %D %S ",
		"anime_library_title_episode_format_str"           => "  %t@ %D %S %t",
		"anime_library_unwatched_title_format_str"         => " %t@ $q@ %D %S ",
		"anime_library_unwatched_title_show_format_str"    => " %t - %c@ %D %S ",
		"anime_library_unwatched_title_episode_format_str" => "  %t@ %D %S %r  ",

		"preferences_title_format_str" => " %t",
		"preferences_item_format_str" => " %t@.%v",

		#"watch_file"   => @conf + "/watched",
		"log_file"     => @conf + "/aw.log",
		"watch_log"    => @conf + "/watchlog",

		"local_anime"  => nil
	]
	self.save
end

#get(s) ⇒ Object



76
77
78
79
# File 'lib/aniview/util/pref.rb', line 76

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

#getAllObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/aniview/util/pref.rb', line 81

def getAll
	ignore = {
		"local_anime" => 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



117
118
119
# File 'lib/aniview/util/pref.rb', line 117

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

#saveObject



113
114
115
# File 'lib/aniview/util/pref.rb', line 113

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

#saveLocalAnime(dump) ⇒ Object



108
109
110
111
# File 'lib/aniview/util/pref.rb', line 108

def saveLocalAnime dump
	@pref["local_anime"] = dump
	self.save
end

#set(s, val) ⇒ Object



58
59
60
61
62
63
# File 'lib/aniview/util/pref.rb', line 58

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

#setpref(s, val, bpref) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/aniview/util/pref.rb', line 65

def setpref(s, val, bpref)
	if s.length == 1
		bpref[s[0]]=val
		return bpref
	else
		key = s.delete_at(0)
		bpref[key] = setpref(s, val, bpref[key])
	end
	return bpref
end