Class: Demode::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



11
12
13
14
15
# File 'lib/demode/configuration.rb', line 11

def initialize
  @check_enabled = nil
  @id_field = :id
  @replacements = []
end

Instance Attribute Details

#check_enabledObject

Returns the value of attribute check_enabled.



6
7
8
# File 'lib/demode/configuration.rb', line 6

def check_enabled
  @check_enabled
end

#id_fieldObject

Returns the value of attribute id_field.



8
9
10
# File 'lib/demode/configuration.rb', line 8

def id_field
  @id_field
end

#replacementsObject

Returns the value of attribute replacements.



9
10
11
# File 'lib/demode/configuration.rb', line 9

def replacements
  @replacements
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/demode/configuration.rb', line 35

def enabled?
  if check_enabled
    if check_enabled.respond_to?(:call)
      return check_enabled.call
    elsif check_enabled.is_a?(Boolean)
      return check_enabled == true
    else
      raise "check_enabled must be callable or boolean"
    end
  end
  false
end

#validate!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/demode/configuration.rb', line 17

def validate!
  if @replacements && @replacements.count > 0
    @replacements.each do |r|
      raise "Replacement must specify three parameters: class, method to replace, replacement field" if r.count != 3

      raise "Replacement first param must be a Class: #{r[0]}" if !r[0].is_a?(Class)
      raise "Replacement second param must be a symbol: #{r[1]}" if !r[1].is_a?(Symbol)
      raise "Replacement third param must be a symbol or proc: #{r[2]}" if !r[2].is_a?(Symbol) && !r[2].respond_to?(:call)

      raise "Unknown replacement field: #{r[2]}" if r[2].is_a?(Symbol) && !Generator.respond_to?(r[2])
    end
  end

  raise "Must specify a valid symbol for the id_field" if @id_field.nil? || !@id_field.is_a?(Symbol)

  true
end