Class: ArTTY::Cache

Inherits:
JSONConfig
  • Object
show all
Extended by:
JSONConfig::Keys
Defined in:
lib/arTTY/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Cache

Returns a new instance of Cache.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/arTTY/cache.rb', line 80

def initialize(file = nil)
    file ||= "#{ENV["HOME"]}/.cache/arTTY/art.json"
    @defaults = {
        "art" => Hash.new,
        "version" => ArTTY.current_version
    }

    @cachefile = Pathname.new(file).expand_path
    @cachedir = @cachefile.dirname
    @imagesdir = "arTTY_images"
    super(@cachefile, false)

    refresh if (get_version != ArTTY.current_version)
end

Instance Method Details

#artObject



16
17
18
# File 'lib/arTTY/cache.rb', line 16

def art
    return get_art.keys.sort
end

#get_file_for(name) ⇒ Object



68
69
70
# File 'lib/arTTY/cache.rb', line 68

def get_file_for(name)
    return get_art[name]["file"]
end

#get_height_for(name) ⇒ Object



72
73
74
# File 'lib/arTTY/cache.rb', line 72

def get_height_for(name)
    return get_art[name]["height"]
end

#get_width_for(name) ⇒ Object



76
77
78
# File 'lib/arTTY/cache.rb', line 76

def get_width_for(name)
    return get_art[name]["width"]
end

#refresh(download = false) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/arTTY/cache.rb', line 95

def refresh(download = false)
    download_and_extract if (download)

    set_art(Hash.new)
    set_version(ArTTY.current_version)

    [
        "#{@cachedir}/#{@imagesdir}",
        "#{ENV["HOME"]}/.config/arTTY/#{@imagesdir}"
    ].each do |dir|
        Dir["#{dir}/**/*.json"].each do |file|
            img = ArTTY::Art.new(file)
            get_art[img.name] = {
                "file" => file,
                "height" => img.height,
                "width" => img.width
            }
        end
    end

    save
end