Sha256: 090a6ac49d68da146ace63dc51464de67a4efc356551d26275fd587fabdc89ce

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

require 'demode/configuration'

require 'demode/generate'

module Demode

  class << self
    # A configuration object. Must like a hash and return sensible values for all
    # configuration options. See Demode::Configuration
    attr_accessor :configuration

    # Call this method to modify the configuration in your initializers
    def configure
      self.configuration ||= Configuration.new

      yield(configuration) if block_given?
      
      configuration.validate!

      setup if configuration.enabled?
    end

    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

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
demode-0.0.3 lib/demode.rb
demode-0.0.2 lib/demode.rb