Class: Inkjet::Configuration

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

Constant Summary collapse

DEFAULT_CONFIGURATION_OPTIONS =
{:tabstop => 2}

Instance Method Summary collapse

Instance Method Details

#changedObject

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



15
16
17
18
19
# File 'lib/inkjet/configuration.rb', line 15

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)


22
23
24
# File 'lib/inkjet/configuration.rb', line 22

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

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

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



27
28
29
30
31
32
# File 'lib/inkjet/configuration.rb', line 27

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



35
36
37
38
39
# File 'lib/inkjet/configuration.rb', line 35

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



42
43
44
# File 'lib/inkjet/configuration.rb', line 42

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



47
48
49
50
# File 'lib/inkjet/configuration.rb', line 47

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

#to_hashObject Also known as: to_h



52
53
54
55
56
57
58
# File 'lib/inkjet/configuration.rb', line 52

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