Class: Cani::Config
- Inherits:
-
Object
show all
- Defined in:
- lib/cani/config.rb
Constant Summary
collapse
- FILE =
File.join(Dir.home, '.config', 'cani', 'config.yml').freeze
- DIRECTORY =
File.dirname(FILE).freeze
- COMP_DIR =
File.join(DIRECTORY, 'completions').freeze
- FISH_DIR =
File.join(Dir.home, '.config', 'fish').freeze
- FISH_COMP_DIR =
File.join(FISH_DIR, 'completions').freeze
- DEFAULTS =
{
'expire' => 86_400,
'source' => 'https://raw.githubusercontent.com/Fyrd/caniuse/master/data.json',
'versions' => 1,
'browsers' => %w[ie edge chrome firefox safari ios_saf opera android bb],
'navigate' => 'always',
'notes' => 'relevant'
}.freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(**opts) ⇒ Config
Returns a new instance of Config.
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/cani/config.rb', line 22
def initialize(**opts)
@settings = DEFAULTS.merge opts
if File.exist? file
if (yml = YAML.load_file(file))
@settings.merge! yml
end
else
install!
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mtd, *args, &block) ⇒ Object
118
119
120
|
# File 'lib/cani/config.rb', line 118
def method_missing(mtd, *args, &block)
settings.key?(mtd.to_s) ? settings[mtd.to_s] : super
end
|
Instance Attribute Details
#settings ⇒ Object
Returns the value of attribute settings.
3
4
5
|
# File 'lib/cani/config.rb', line 3
def settings
@settings
end
|
Instance Method Details
#comp_dir ⇒ Object
42
43
44
|
# File 'lib/cani/config.rb', line 42
def comp_dir
COMP_DIR
end
|
#directory ⇒ Object
38
39
40
|
# File 'lib/cani/config.rb', line 38
def directory
DIRECTORY
end
|
#file ⇒ Object
34
35
36
|
# File 'lib/cani/config.rb', line 34
def file
FILE
end
|
#fish_comp_dir ⇒ Object
50
51
52
|
# File 'lib/cani/config.rb', line 50
def fish_comp_dir
FISH_COMP_DIR
end
|
#fish_dir ⇒ Object
46
47
48
|
# File 'lib/cani/config.rb', line 46
def fish_dir
FISH_DIR
end
|
#install! ⇒ Object
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/cani/config.rb', line 63
def install!
hrs = (DEFAULTS['expire'] / 3600.to_f).round 2
days = (hrs / 24.to_f).round 2
wk = (days / 7.to_f).round 2
mo = (days / 30.to_f).round 2
tstr = if mo >= 1
"#{mo == mo.to_i ? mo.to_i : mo} month#{mo != 1 ? 's' : ''}"
elsif wk >= 1
"#{wk == wk.to_i ? wk.to_i : wk} week#{wk != 1 ? 's' : ''}"
elsif days >= 1
"#{days == days.to_i ? days.to_i : days} day#{days != 1 ? 's' : ''}"
else
"#{hrs == hrs.to_i ? hrs.to_i : hrs} hour#{hrs != 1 ? 's' : ''}"
end
FileUtils.mkdir_p directory
File.open file, 'w' do |f|
f << "---\n"
f << "# this is the default configuration file for the \"Cani\" RubyGem.\n"
f << "# it contains some options to control what is shown, when new data\n"
f << "# is fetched, where it should be fetched from.\n"
f << "# documentation: https://github.com/sidofc/cani\n"
f << "# rubygems: https://rubygems.org/gems/cani\n\n"
f << "# the \"expire\" key defines the interval at which new data is\n"
f << "# fetched from \"source\". It's value is passed in as seconds\n"
f << "# default value: #{DEFAULTS['expire']} # => #{tstr}\n"
f << "expire: #{expire}\n\n"
f << "# the \"source\" key is used to fetch the data required for\n"
f << "# this command to work.\n"
f << "source: #{source}\n\n"
f << "# navigating means reopening the previously open window when going back by pressing <escape>\n"
f << "# or opening the next menu by selecting an entry in fzf with <enter>\n"
f << "# there are two different navigation modes:\n"
f << "# * 'always' - always navigate back to the previous menu, exit only at root menu with <escape>\n"
f << "# * 'forward' - only allow navigating forward and backwards upto the menu that cani was initially open\n"
f << "navigate: #{navigate}\n\n"
f << "# the notes property defines how notes should be displayed\n"
f << "# there are two different modes:\n"
f << "# * 'all' - show all notes, regardless of relevance\n"
f << "# * 'relevant' - show only relevant (visible in an era) notes\n"
f << "notes: #{notes}\n\n"
f << "# the \"versions\" key defines how many versions of support\n"
f << "# will be shown in the \"use\" command\n"
f << "# e.g. `-ie +edge` becomes `--ie ++edge` when this is set to 2, etc...\n"
f << "versions: #{versions}\n\n"
f << "# the \"browsers\" key defines which browsers are shown\n"
f << "# in the \"use\" command\n"
f << "browsers:\n"
f << browsers.map { |bn| " - #{bn}" }.join("\n") + "\n"
f << (Cani.api.browsers.map(&:name) - browsers).map { |bn| " # - #{bn}" }.join("\n")
end
Completions.install!
end
|
#nav_type?(type) ⇒ Boolean
59
60
61
|
# File 'lib/cani/config.rb', line 59
def nav_type?(type)
navigate == type.to_s
end
|
#remove! ⇒ Object
54
55
56
57
|
# File 'lib/cani/config.rb', line 54
def remove!
File.unlink file if File.exist? file
FileUtils.rm_rf directory if Dir.exist? directory
end
|
#respond_to_missing?(mtd, include_private = false) ⇒ Boolean
122
123
124
|
# File 'lib/cani/config.rb', line 122
def respond_to_missing?(mtd, include_private = false)
settings.key? mtd.to_s
end
|