Class: CrawlKit::Configuration
- Inherits:
-
Object
- Object
- CrawlKit::Configuration
- Defined in:
- lib/crawl_kit/configuration.rb
Class Method Summary collapse
- .accepted_options ⇒ Object
- .add_option(name, default_value = nil, options = {}, &transform) ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql)
Returns true if the two configuration objects have the same values.
-
#initialize(options = {}) ⇒ Configuration
constructor
Creates a new Configuration object.
- #inspect ⇒ Object
-
#to_h ⇒ Hash
Returns a hash of all configuration values.
-
#with(options = {}) ⇒ Object
Used to create a new Configuration object with the given modifications.
Constructor Details
#initialize(options = {}) ⇒ Configuration
Creates a new Configuration object.
8 9 10 11 12 13 14 15 |
# File 'lib/crawl_kit/configuration.rb', line 8 def initialize( = {}) .each_pair do |opt_name, value| opt_name = opt_name.to_sym if self.class..include?(opt_name) supplied[opt_name] = value end end end |
Class Method Details
.accepted_options ⇒ Object
61 62 63 |
# File 'lib/crawl_kit/configuration.rb', line 61 def @options ||= Set.new end |
.add_option(name, default_value = nil, options = {}, &transform) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/crawl_kit/configuration.rb', line 66 def add_option(name, default_value = nil, = {}, &transform) << name define_method(name) do value = supplied.has_key?(name) ? supplied[name] : default_value transform ? transform.call(value) : value end alias_method("#{name}?", name) if [:boolean] end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql
Returns true if the two configuration objects have the same values.
42 43 44 |
# File 'lib/crawl_kit/configuration.rb', line 42 def == other other.is_a?(self.class) and self.supplied == other.supplied end |
#inspect ⇒ Object
49 50 51 |
# File 'lib/crawl_kit/configuration.rb', line 49 def inspect "<#{self.class.name}>" end |
#to_h ⇒ Hash
Returns a hash of all configuration values.
33 34 35 36 37 38 |
# File 'lib/crawl_kit/configuration.rb', line 33 def to_h self.class..inject({}) do |h,k| h[k] = send(k) h end end |
#with(options = {}) ⇒ Object
Used to create a new Configuration object with the given modifications. The current configuration object is not modified.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/crawl_kit/configuration.rb', line 19 def with( = {}) # symbolize option keys = .inject({}) {|h,kv| h[kv.first.to_sym] = kv.last; h } values = supplied.merge() if supplied == values self # nothing changed else self.class.new(values) end end |