Class: Wp2txt::Config
- Inherits:
-
Object
- Object
- Wp2txt::Config
- Defined in:
- lib/wp2txt/config.rb
Overview
Configuration management for wp2txt Loads and saves settings from ~/.wp2txt/config.yml
Constant Summary collapse
- DEFAULT_CONFIG_PATH =
Default configuration file path
File.("~/.wp2txt/config.yml")
- DEFAULT_CACHE_DIR =
Default cache directory
File.("~/.wp2txt/cache")
- DUMP_EXPIRY_RANGE =
Validation ranges
(1..365)
- CATEGORY_EXPIRY_RANGE =
(1..90)
- DEPTH_RANGE =
(0..10)
- VALID_FORMATS =
%w[text json].freeze
- DEFAULTS =
Default values
{ dump_expiry_days: 30, category_expiry_days: 7, cache_directory: DEFAULT_CACHE_DIR, default_format: "text", default_depth: 0 }.freeze
Instance Attribute Summary collapse
-
#cache_directory ⇒ Object
readonly
Returns the value of attribute cache_directory.
-
#category_expiry_days ⇒ Object
readonly
Returns the value of attribute category_expiry_days.
-
#default_depth ⇒ Object
readonly
Returns the value of attribute default_depth.
-
#default_format ⇒ Object
readonly
Returns the value of attribute default_format.
-
#dump_expiry_days ⇒ Object
readonly
Returns the value of attribute dump_expiry_days.
Class Method Summary collapse
-
.create_default(path = default_path, force: false) ⇒ Boolean
Create default configuration file.
-
.default_path ⇒ String
Default configuration file path.
-
.from_hash(data) ⇒ Config
Create Config from hash.
-
.load(path = default_path) ⇒ Config
Load configuration from file.
Instance Method Summary collapse
-
#initialize(dump_expiry_days: DEFAULTS[:dump_expiry_days], category_expiry_days: DEFAULTS[:category_expiry_days], cache_directory: DEFAULTS[:cache_directory], default_format: DEFAULTS[:default_format], default_depth: DEFAULTS[:default_depth]) ⇒ Config
constructor
A new instance of Config.
-
#save(path = self.class.default_path) ⇒ Object
Save configuration to file.
-
#to_h ⇒ Hash
Convert to hash representation.
Constructor Details
#initialize(dump_expiry_days: DEFAULTS[:dump_expiry_days], category_expiry_days: DEFAULTS[:category_expiry_days], cache_directory: DEFAULTS[:cache_directory], default_format: DEFAULTS[:default_format], default_depth: DEFAULTS[:default_depth]) ⇒ Config
Returns a new instance of Config.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/wp2txt/config.rb', line 33 def initialize( dump_expiry_days: DEFAULTS[:dump_expiry_days], category_expiry_days: DEFAULTS[:category_expiry_days], cache_directory: DEFAULTS[:cache_directory], default_format: DEFAULTS[:default_format], default_depth: DEFAULTS[:default_depth] ) @dump_expiry_days = clamp(dump_expiry_days.to_i, DUMP_EXPIRY_RANGE) @category_expiry_days = clamp(category_expiry_days.to_i, CATEGORY_EXPIRY_RANGE) @cache_directory = cache_directory.to_s.empty? ? DEFAULT_CACHE_DIR : cache_directory.to_s @default_format = validate_format(default_format.to_s) @default_depth = clamp(default_depth.to_i, DEPTH_RANGE) end |
Instance Attribute Details
#cache_directory ⇒ Object (readonly)
Returns the value of attribute cache_directory.
30 31 32 |
# File 'lib/wp2txt/config.rb', line 30 def cache_directory @cache_directory end |
#category_expiry_days ⇒ Object (readonly)
Returns the value of attribute category_expiry_days.
30 31 32 |
# File 'lib/wp2txt/config.rb', line 30 def category_expiry_days @category_expiry_days end |
#default_depth ⇒ Object (readonly)
Returns the value of attribute default_depth.
30 31 32 |
# File 'lib/wp2txt/config.rb', line 30 def default_depth @default_depth end |
#default_format ⇒ Object (readonly)
Returns the value of attribute default_format.
30 31 32 |
# File 'lib/wp2txt/config.rb', line 30 def default_format @default_format end |
#dump_expiry_days ⇒ Object (readonly)
Returns the value of attribute dump_expiry_days.
30 31 32 |
# File 'lib/wp2txt/config.rb', line 30 def dump_expiry_days @dump_expiry_days end |
Class Method Details
.create_default(path = default_path, force: false) ⇒ Boolean
Create default configuration file
88 89 90 91 92 93 94 |
# File 'lib/wp2txt/config.rb', line 88 def self.create_default(path = default_path, force: false) return false if File.exist?(path) && !force config = new config.save(path) true end |
.default_path ⇒ String
Default configuration file path
80 81 82 |
# File 'lib/wp2txt/config.rb', line 80 def self.default_path DEFAULT_CONFIG_PATH end |
.from_hash(data) ⇒ Config
Create Config from hash
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/wp2txt/config.rb', line 65 def self.from_hash(data) cache = data[:cache] || {} defaults = data[:defaults] || {} new( dump_expiry_days: cache[:dump_expiry_days] || DEFAULTS[:dump_expiry_days], category_expiry_days: cache[:category_expiry_days] || DEFAULTS[:category_expiry_days], cache_directory: cache[:directory] || DEFAULTS[:cache_directory], default_format: defaults[:format] || DEFAULTS[:default_format], default_depth: defaults[:depth] || DEFAULTS[:default_depth] ) end |
.load(path = default_path) ⇒ Config
Load configuration from file
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/wp2txt/config.rb', line 50 def self.load(path = default_path) return new unless File.exist?(path) begin data = YAML.safe_load(File.read(path), symbolize_names: true) || {} from_hash(data) rescue Psych::SyntaxError, StandardError # Return defaults on parse error new end end |
Instance Method Details
#save(path = self.class.default_path) ⇒ Object
Save configuration to file
98 99 100 101 102 103 |
# File 'lib/wp2txt/config.rb', line 98 def save(path = self.class.default_path) FileUtils.mkdir_p(File.dirname(path)) content = generate_yaml File.write(path, content) end |
#to_h ⇒ Hash
Convert to hash representation
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/wp2txt/config.rb', line 107 def to_h { cache: { dump_expiry_days: @dump_expiry_days, category_expiry_days: @category_expiry_days, directory: @cache_directory }, defaults: { format: @default_format, depth: @default_depth } } end |