Class: YoutubeDL::Options
- Inherits:
-
Object
- Object
- YoutubeDL::Options
- Defined in:
- lib/youtube-dl/options.rb
Overview
Option and configuration getting, setting, and storage, and all that
Instance Attribute Summary collapse
-
#store ⇒ Object
- Hash
-
key value storage object.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get option with brackets syntax.
-
#[]=(key, value) ⇒ Object
Set option with brackets syntax.
-
#configure {|config| ... } ⇒ Object
Set options using a block.
-
#each_paramized {|paramized_key, value| ... } ⇒ Object
Iterate through the paramized keys and values.
-
#each_paramized_key {|key, paramized_key| ... } ⇒ Object
Iterate through the keys and their paramized counterparts.
-
#initialize(options = {}) ⇒ Options
constructor
Options initializer.
-
#manipulate_keys!(&block) {|key| ... } ⇒ Object
Calls a block to do operations on keys See sanitize_keys! for examples.
-
#method_missing(method, *args, &block) ⇒ Object
Option getting and setting using ghost methods.
-
#sanitize_keys ⇒ YoutubeDL::Options
Symbolizes and sanitizes keys and returns a copy of self.
-
#sanitize_keys! ⇒ Object
Symbolizes and sanitizes keys in the option store.
-
#to_hash ⇒ Hash
(also: #to_h)
Returns options as a hash.
Constructor Details
#initialize(options = {}) ⇒ Options
Options initializer
12 13 14 15 16 17 18 |
# File 'lib/youtube-dl/options.rb', line 12 def initialize(={}) if .is_a? Hash @store = else @store = .to_h end 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
77 78 79 80 81 82 83 84 |
# File 'lib/youtube-dl/options.rb', line 77 def method_missing(method, *args, &block) if method.to_s.include? '=' method = method.to_s.tr('=', '').to_sym @store[method] = args.first else @store[method] end end |
Instance Attribute Details
#store ⇒ Object
- Hash
-
key value storage object
7 8 9 |
# File 'lib/youtube-dl/options.rb', line 7 def store @store end |
Instance Method Details
#[](key) ⇒ Object
Get option with brackets syntax
59 60 61 |
# File 'lib/youtube-dl/options.rb', line 59 def [](key) @store[key.to_sym] end |
#[]=(key, value) ⇒ Object
Set option with brackets syntax
67 68 69 |
# File 'lib/youtube-dl/options.rb', line 67 def []=(key, value) @store[key.to_sym] = value end |
#configure {|config| ... } ⇒ Object
Set options using a block
51 52 53 |
# File 'lib/youtube-dl/options.rb', line 51 def configure yield(self) if block_given? end |
#each_paramized {|paramized_key, value| ... } ⇒ Object
Iterate through the paramized keys and values.
TODO: Enumerable?
33 34 35 36 37 |
# File 'lib/youtube-dl/options.rb', line 33 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.
42 43 44 45 46 |
# File 'lib/youtube-dl/options.rb', line 42 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
92 93 94 95 96 97 98 99 100 |
# File 'lib/youtube-dl/options.rb', line 92 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_keys ⇒ YoutubeDL::Options
Symbolizes and sanitizes keys and returns a copy of self
114 115 116 117 118 |
# File 'lib/youtube-dl/options.rb', line 114 def sanitize_keys safe_copy = self.dup safe_copy.sanitize_keys! safe_copy end |
#sanitize_keys! ⇒ Object
Symbolizes and sanitizes keys in the option store
103 104 105 106 107 108 109 |
# File 'lib/youtube-dl/options.rb', line 103 def sanitize_keys! # Symbolize manipulate_keys! { |key_name| key_name.is_a?(Symbol) ? key_name : key_name.to_sym } # Underscoreize (because Cocaine doesn't like hyphens) manipulate_keys! { |key_name| key_name.to_s.tr('-', '_').to_sym } end |
#to_hash ⇒ Hash Also known as: to_h
Returns options as a hash
23 24 25 |
# File 'lib/youtube-dl/options.rb', line 23 def to_hash @store end |