Class: SiteDiff::Config::Preset
- Inherits:
-
Object
- Object
- SiteDiff::Config::Preset
- Defined in:
- lib/sitediff/config/preset.rb
Overview
Preset helper.
Constant Summary collapse
- DIRECTORY =
Directory in which presets live.
TODO: Move this outside “lib”.
"#{Pathname.new(__dir__).dirname}/presets".freeze
Class Method Summary collapse
-
.all ⇒ Array
Get all possible rules.
-
.exist?(name, exception: false) ⇒ Boolean
Checks whether a preset exists.
-
.file(name) ⇒ Object
Returns the path to a preset file.
-
.read(name) ⇒ Hash
Reads preset rules.
Class Method Details
.all ⇒ Array
Get all possible rules.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/sitediff/config/preset.rb', line 42 def self.all # Load and cache preset names. if @all.nil? @all = [] pattern = "#{DIRECTORY}/*.yaml" Dir.glob(pattern) do |file| @all << File.basename(file, '.yaml') end end @all end |
.exist?(name, exception: false) ⇒ Boolean
Checks whether a preset exists.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/sitediff/config/preset.rb', line 57 def self.exist?(name, exception: false) result = File.exist?(file(name)) # Raise an exception, if required. if exception && !result raise Config::InvalidConfig, "Preset not found: #{name}" end result end |
.file(name) ⇒ Object
Returns the path to a preset file.
70 71 72 |
# File 'lib/sitediff/config/preset.rb', line 70 def self.file(name) DIRECTORY + "/#{name}.yaml" end |
.read(name) ⇒ Hash
Reads preset rules.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sitediff/config/preset.rb', line 25 def self.read(name) @cache = {} if @cache.nil? # Load and cache preset config. if @cache[name].nil? exist? name, exception: true @cache[name] = Config.load_conf file(name) end @cache[name] end |