Class: Wp2txt::Config

Inherits:
Object
  • Object
show all
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.expand_path("~/.wp2txt/config.yml")
DEFAULT_CACHE_DIR =

Default cache directory

File.expand_path("~/.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

Class Method Summary collapse

Instance Method Summary collapse

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_directoryObject (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_daysObject (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_depthObject (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_formatObject (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_daysObject (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

Parameters:

  • path (String) (defaults to: default_path)

    Path to config file

  • force (Boolean) (defaults to: false)

    Overwrite existing file

Returns:

  • (Boolean)

    True if file was created



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_pathString

Default configuration file path

Returns:

  • (String)

    Path to default config file



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

Parameters:

  • data (Hash)

    Configuration hash

Returns:

  • (Config)

    Configuration object



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

Parameters:

  • path (String) (defaults to: default_path)

    Path to config file (default: ~/.wp2txt/config.yml)

Returns:

  • (Config)

    Configuration object



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

Parameters:

  • path (String) (defaults to: self.class.default_path)

    Path to config 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_hHash

Convert to hash representation

Returns:

  • (Hash)

    Configuration as hash



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