Class: TidyFFI::OptionsContainer
- Inherits:
-
Object
- Object
- TidyFFI::OptionsContainer
- Defined in:
- lib/tidy_ffi/options_container.rb
Overview
:nodoc:
Defined Under Namespace
Classes: Proxy
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #clear! ⇒ Object
-
#initialize(ops = nil) ⇒ OptionsContainer
constructor
A new instance of OptionsContainer.
- #merge_with_options(options) ⇒ Object
- #method_missing(method, *args) ⇒ Object
- #to_hash! ⇒ Object
-
#validate_option(key, value) ⇒ Object
It’s a kinda bad method: it uses TidyFFI::Interface.option_valid and TidyFFI::Tidy.validate_options? Also it do second lookup into default options.
Constructor Details
#initialize(ops = nil) ⇒ OptionsContainer
Returns a new instance of OptionsContainer.
3 4 5 6 7 8 9 |
# File 'lib/tidy_ffi/options_container.rb', line 3 def initialize(ops = nil) if ops @options = ops.to_hash! else @options = {} end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/tidy_ffi/options_container.rb', line 38 def method_missing(method, *args) if method.to_s =~ /=$/ key, val = method.to_s.sub(/=$/, '').intern, args.first validate_option(key, val) @options[key] = val else @options[method] end end |
Instance Method Details
#==(obj) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/tidy_ffi/options_container.rb', line 23 def ==(obj) if obj.is_a?(Hash) @options == obj elsif obj.is_a?(OptionsContainer) obj == @options else false end end |
#clear! ⇒ Object
33 34 35 36 |
# File 'lib/tidy_ffi/options_container.rb', line 33 def clear! @options = {} self end |
#merge_with_options(options) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/tidy_ffi/options_container.rb', line 15 def () .each do |key, val| key = key.intern unless Symbol === key validate_option(key, val) @options[key] = val end end |
#to_hash! ⇒ Object
11 12 13 |
# File 'lib/tidy_ffi/options_container.rb', line 11 def to_hash! @options.dup end |
#validate_option(key, value) ⇒ Object
It’s a kinda bad method: it uses TidyFFI::Interface.option_valid and TidyFFI::Tidy.validate_options? Also it do second lookup into default options
50 51 52 53 54 55 56 57 58 |
# File 'lib/tidy_ffi/options_container.rb', line 50 def validate_option(key, value) return if !TidyFFI::Tidy. || TidyFFI::Interface.option_valid?(key, value) if TidyFFI::Interface.[key] raise TidyFFI::Tidy::InvalidOptionValue, "#{value} is not a valid value for key #{key}" else raise TidyFFI::Tidy::InvalidOptionName, "#{key} is an invalid option name" end end |