Class: RbYoutubeDL::Options
- Inherits:
-
Object
- Object
- RbYoutubeDL::Options
- Defined in:
- lib/rb-youtube-dl/options.rb
Overview
Option and configuration getting, setting, and storage, and all that
Instance Attribute Summary collapse
-
#banned_keys ⇒ Array
Array of keys that won’t be saved to the options store.
-
#store ⇒ Hash
Key value storage object.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get option with brackets syntax.
-
#[]=(key, value) ⇒ Object
Set option with brackets syntax.
-
#banned?(key) ⇒ Boolean
Check if key is a banned key.
-
#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 ⇒ RbYoutubeDL::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.
-
#with(hash) ⇒ RbYoutubeDL::Options
Merge options with given hash, removing banned keys, and returning a new instance of Options.
Constructor Details
#initialize(options = {}) ⇒ Options
Options initializer
13 14 15 16 17 18 19 20 21 |
# File 'lib/rb-youtube-dl/options.rb', line 13 def initialize( = {}) if .is_a? Hash @store = else @store = .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
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rb-youtube-dl/options.rb', line 102 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_keys ⇒ Array
Returns array of keys that won’t be saved to the options store.
8 9 10 |
# File 'lib/rb-youtube-dl/options.rb', line 8 def banned_keys @banned_keys end |
#store ⇒ Hash
Returns key value storage object.
5 6 7 |
# File 'lib/rb-youtube-dl/options.rb', line 5 def store @store end |
Instance Method Details
#[](key) ⇒ Object
Get option with brackets syntax
67 68 69 70 71 |
# File 'lib/rb-youtube-dl/options.rb', line 67 def [](key) remove_banned return nil if banned? key @store[key.to_sym] end |
#[]=(key, value) ⇒ Object
Set option with brackets syntax
78 79 80 81 82 |
# File 'lib/rb-youtube-dl/options.rb', line 78 def []=(key, value) remove_banned return nil if banned? key @store[key.to_sym] = value end |
#banned?(key) ⇒ Boolean
Check if key is a banned key
154 155 156 |
# File 'lib/rb-youtube-dl/options.rb', line 154 def banned?(key) @banned_keys.include? key end |
#configure {|config| ... } ⇒ Object
Set options using a block
57 58 59 60 61 |
# File 'lib/rb-youtube-dl/options.rb', line 57 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?
38 39 40 41 42 |
# File 'lib/rb-youtube-dl/options.rb', line 38 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.
48 49 50 51 52 |
# File 'lib/rb-youtube-dl/options.rb', line 48 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
120 121 122 123 124 125 126 127 128 |
# File 'lib/rb-youtube-dl/options.rb', line 120 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 ⇒ RbYoutubeDL::Options
Symbolizes and sanitizes keys and returns a copy of self
144 145 146 147 148 |
# File 'lib/rb-youtube-dl/options.rb', line 144 def sanitize_keys safe_copy = dup safe_copy.sanitize_keys! safe_copy end |
#sanitize_keys! ⇒ Object
Symbolizes and sanitizes keys in the option store
133 134 135 136 137 138 139 |
# File 'lib/rb-youtube-dl/options.rb', line 133 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_hash ⇒ Hash Also known as: to_h
Returns options as a hash
26 27 28 29 |
# File 'lib/rb-youtube-dl/options.rb', line 26 def to_hash remove_banned @store end |
#with(hash) ⇒ RbYoutubeDL::Options
Merge options with given hash, removing banned keys, and returning a new instance of Options.
89 90 91 92 93 94 |
# File 'lib/rb-youtube-dl/options.rb', line 89 def with(hash) merged = Options.new(@store.merge(hash.to_h)) merged.banned_keys = @banned_keys merged.send(:remove_banned) merged end |