Class: Excursion::Configuration

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

Constant Summary collapse

DEFAULT_CONFIGURATION_OPTIONS =
{
  # TODO
    # exclude_pattern: to exclude certain routes from being shared
    # include_pattern: to only include certain routes
  register_app: true, # whether or not to register the app automatically on init
  default_url_options: {}, # default_url_options used when building routes for this app
  retry_limit: 3 # retry limit for datastores that user remote servers
}

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

DEFAULT_CONFIGURATION_OPTIONS.keys.each do |key|

define_method "#{key.to_s}=" do |val|
  @changed[key] = [send(key), val]
  instance_variable_set "@#{key.to_s}", val
end

end



21
22
23
24
25
26
27
28
# File 'lib/excursion/configuration.rb', line 21

def method_missing(meth, *args)
  if meth.to_s.match(/\A(.*)=\Z/)
    @changed[$1] = [send($1), *args]
    instance_variable_set "@#{$1.to_s}", *args
  else
    instance_variable_get "@#{meth}"
  end
end

Instance Method Details

#changedObject

Returns a hash of all the changed keys and values after being reconfigured



31
32
33
34
35
# File 'lib/excursion/configuration.rb', line 31

def changed
  @changed = {}
  to_hash.each { |key,val| @changed[key] = [@saved_state[key], val] if @saved_state[key] != val }
  @changed
end

#changed?(key) ⇒ Boolean

Check whether a key was changed after being reconfigured

Returns:

  • (Boolean)


38
39
40
# File 'lib/excursion/configuration.rb', line 38

def changed?(key)
  changed.has_key?(key)
end

#configure(args = {}, &block) ⇒ Object

Pass arguments and/or a block to configure the available options



43
44
45
46
47
48
# File 'lib/excursion/configuration.rb', line 43

def configure(args={}, &block)
  save_state
  configure_with_args args
  configure_with_block &block if block_given?
  self
end

#configure_with_args(args) ⇒ Object

Accepts arguments which are used to configure available options



51
52
53
54
55
# File 'lib/excursion/configuration.rb', line 51

def configure_with_args(args)
  args.select { |k,v| DEFAULT_CONFIGURATION_OPTIONS.keys.include?(k) }.each do |key,val|
    instance_variable_set "@#{key.to_s}", val
  end
end

#configure_with_block(&block) ⇒ Object

Accepts a block which is used to configure available options



58
59
60
# File 'lib/excursion/configuration.rb', line 58

def configure_with_block(&block)
  self.instance_eval(&block) if block_given?
end

#save_stateObject

Saves a copy of the current state, to be used later to determine what was changed



63
64
65
66
# File 'lib/excursion/configuration.rb', line 63

def save_state
  @saved_state = clone.to_hash
  @changed = {}
end

#to_hashObject Also known as: to_h



68
69
70
71
72
73
74
# File 'lib/excursion/configuration.rb', line 68

def to_hash
  h = {}
  DEFAULT_CONFIGURATION_OPTIONS.keys.each do |key|
    h[key] = instance_variable_get "@#{key.to_s}"
  end
  h
end