Module: Demode

Defined in:
lib/demode.rb,
lib/demode/version.rb,
lib/demode/generate.rb,
lib/demode/configuration.rb,
lib/demode/generator/fields.rb,
lib/demode/generator/person.rb,
lib/demode/generator/company.rb

Defined Under Namespace

Modules: Fields, Generate, Generator Classes: Configuration

Constant Summary collapse

VERSION =
"0.0.3"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

A configuration object. Must like a hash and return sensible values for all configuration options. See Demode::Configuration



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

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Call this method to modify the configuration in your initializers

Yields:



13
14
15
16
17
18
19
20
21
# File 'lib/demode.rb', line 13

def configure
  self.configuration ||= Configuration.new

  yield(configuration) if block_given?
  
  configuration.validate!

  setup if configuration.enabled?
end

.setupObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/demode.rb', line 23

def setup
  id_field = self.configuration.id_field
  self.configuration.replacements.each do |klass, method, replacement|

    # alias the method we're about to replace
    replaced_method = "_old_#{method}".to_sym
    klass.send(:alias_method, replaced_method, method)

    # replace the method with a call to our generator
    klass.send(:define_method, method) do
      id = self.send(id_field)
      if replacement.is_a?(Symbol)
        Demode::Generate.get(id, replacement)
      elsif replacement.is_a?(Proc)
        replacement.call(self)
      end
    end

  end
end