Class: YtDlp::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/yt-dlp/options.rb

Overview

Option and configuration getting, setting, and storage, and all that

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Options

Options initializer

Parameters:

  • options (Hash) (defaults to: {})

    a hash of options



15
16
17
18
19
20
21
22
23
# File 'lib/yt-dlp/options.rb', line 15

def initialize(options = {})
  @store = if options.is_a? Hash
             options
           else
             options.to_h
           end

  @banned_keys = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &_block) ⇒ Object

Option getting and setting using ghost methods

Parameters:

  • method (Symbol)

    method name

  • args (Array)

    list of arguments passed

  • block (Proc)

    implicit block given

Returns:

  • (Object)

    the value of method in the options store



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/yt-dlp/options.rb', line 106

def method_missing(method, *args, &_block)
  remove_banned
  if method.to_s.include? '='
    method = method.to_s.tr('=', '').to_sym
    return nil if banned? method

    @store[method] = args.first
  else
    return nil if banned? method

    @store[method]
  end
end

Instance Attribute Details

#banned_keysArray

Returns array of keys that won’t be saved to the options store.

Returns:

  • (Array)

    array of keys that won’t be saved to the options store



10
11
12
# File 'lib/yt-dlp/options.rb', line 10

def banned_keys
  @banned_keys
end

#storeHash

Returns key value storage object.

Returns:

  • (Hash)

    key value storage object



7
8
9
# File 'lib/yt-dlp/options.rb', line 7

def store
  @store
end

Instance Method Details

#[](key) ⇒ Object

Get option with brackets syntax

Parameters:

  • key (Object)

    key

Returns:

  • (Object)

    value



69
70
71
72
73
74
# File 'lib/yt-dlp/options.rb', line 69

def [](key)
  remove_banned
  return nil if banned? key

  @store[key.to_sym]
end

#[]=(key, value) ⇒ Object

Set option with brackets syntax

Parameters:

  • key (Object)

    key

  • value (Object)

    value

Returns:

  • (Object)

    whatever Hash#= returns



81
82
83
84
85
86
# File 'lib/yt-dlp/options.rb', line 81

def []=(key, value)
  remove_banned
  return if banned? key

  @store[key.to_sym] = value
end

#banned?(key) ⇒ Boolean

Check if key is a banned key

Parameters:

  • key (Object)

    key to check

Returns:

  • (Boolean)

    true if key is banned, false if not.



160
161
162
# File 'lib/yt-dlp/options.rb', line 160

def banned?(key)
  @banned_keys.include? key
end

#configure {|config| ... } ⇒ Object

Set options using a block

Yields:

  • (config)

    self



59
60
61
62
63
# File 'lib/yt-dlp/options.rb', line 59

def configure
  yield(self) if block_given?
  remove_banned
  self
end

#each_paramized {|paramized_key, value| ... } ⇒ Object

Iterate through the paramized keys and values.

TODO: Enumerable?

Yields:

  • (paramized_key, value)

Returns:

  • (Object)

    @store



40
41
42
43
44
# File 'lib/yt-dlp/options.rb', line 40

def each_paramized
  @store.each do |key, value|
    yield(paramize(key), value)
  end
end

#each_paramized_key {|key, paramized_key| ... } ⇒ Object

Iterate through the keys and their paramized counterparts.

Yields:

  • (key, paramized_key)

Returns:

  • (Object)

    @store



50
51
52
53
54
# File 'lib/yt-dlp/options.rb', line 50

def each_paramized_key
  @store.each_key do |key|
    yield(key, paramize(key))
  end
end

#manipulate_keys!(&block) {|key| ... } ⇒ Object

Calls a block to do operations on keys See sanitize_keys! for examples

Parameters:

  • block (Proc)

    Block with operations on keys

Yield Parameters:

  • key (Object)

    Original key

Yield Returns:

  • (Object)

    Manipulated key



126
127
128
129
130
131
132
133
134
# File 'lib/yt-dlp/options.rb', line 126

def manipulate_keys!(&block)
  @store.keys.each do |old_name|
    new_name = block.call(old_name)
    unless new_name == old_name
      @store[new_name] = @store[old_name]
      @store.delete(old_name)
    end
  end
end

#sanitize_keysYtDlp::Options

Symbolizes and sanitizes keys and returns a copy of self

Returns:



150
151
152
153
154
# File 'lib/yt-dlp/options.rb', line 150

def sanitize_keys
  safe_copy = dup
  safe_copy.sanitize_keys!
  safe_copy
end

#sanitize_keys!Object

Symbolizes and sanitizes keys in the option store

Returns:

  • (Object)

    @store



139
140
141
142
143
144
145
# File 'lib/yt-dlp/options.rb', line 139

def sanitize_keys!
  # Symbolize
  manipulate_keys! { |key_name| key_name.is_a?(Symbol) ? key_name : key_name.to_sym }

  # Underscoreize (because Terrapin doesn't like hyphens)
  manipulate_keys! { |key_name| key_name.to_s.tr('-', '_').to_sym }
end

#to_hashHash Also known as: to_h

Returns options as a hash

Returns:

  • (Hash)

    hash of options



28
29
30
31
# File 'lib/yt-dlp/options.rb', line 28

def to_hash
  remove_banned
  @store
end

#with(hash) ⇒ YtDlp::Options

Merge options with given hash, removing banned keys, and returning a new instance of Options.

Parameters:

  • hash (Hash)

    Hash to merge options with

Returns:



93
94
95
96
97
98
# File 'lib/yt-dlp/options.rb', line 93

def with(hash)
  merged = Options.new(@store.merge(hash.to_h))
  merged.banned_keys = @banned_keys
  merged.send(:remove_banned)
  merged
end