Class: CrawlKit::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/crawl_kit/configuration.rb

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  options.each_pair do |opt_name, value|
    opt_name = opt_name.to_sym
    if self.class.accepted_options.include?(opt_name)
      supplied[opt_name] = value
    end
  end
end

Class Method Details

.accepted_optionsObject



61
62
63
# File 'lib/crawl_kit/configuration.rb', line 61

def accepted_options
  @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, options = {}, &transform)
  accepted_options << 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 options[:boolean]
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql

Returns true if the two configuration objects have the same values.

Returns:

  • (Boolean)

    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

#inspectObject



49
50
51
# File 'lib/crawl_kit/configuration.rb', line 49

def inspect
  "<#{self.class.name}>"
end

#to_hHash

Returns a hash of all configuration values.

Returns:

  • (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.accepted_options.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(options = {})
  # symbolize option keys
  options = options.inject({}) {|h,kv| h[kv.first.to_sym] = kv.last; h }

  values = supplied.merge(options)

  if supplied == values
    self # nothing changed
  else
    self.class.new(values)
  end
end