Sha256: 50c4a21dbdba5c0737a9ba68700f4ffd6516c53cc8397cf4aad89ae442411fd8

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module Turf; class Lookup

  def find(message)
    if local_class && default_class
      message = "The following methods are defined in the #{local_class} class but not the #{default_class} class: #{methods_in_local_and_not_default.join(', ')}.  All methods defined in #{local_class} must also be defined in #{default_class}."
      raise message unless methods_in_local_and_not_default.empty?
    end
    raise "No Turf classes found... these must be defined and required" if classes.empty?
    lookup_path.each do |obj|
      return obj.send(message) if obj.respond_to?(message)
    end
    raise NoMethodError, "The #{message} method could not be found in any of these Turf configuration classes: #{classes.join(", ")}"
  end

  private

  def methods_in_local_and_not_default
    local_class.instance_methods(false) - default_class.instance_methods(false)
  end

  def lookup_path
    classes.map(&:new)
  end

  def classes
    [
      (local_class unless env == "test"),
      env_class,
      default_class
    ].compact
  end

  def local_class
    class_or_nil { Local }
  end

  def default_class
    class_or_nil { Default }
  end

  def env_class
    class_or_nil { Object.const_get("Turf::#{env.capitalize}") }
  end

  def class_or_nil(&block)
    begin(instance_eval(&block))
      instance_eval(&block)
    rescue NameError
    end
  end

  def env
    return Rails.env if defined?(Rails)
    raise "The RAILS_ENV environment variable must be set" unless ENV['RAILS_ENV']
    ENV['RAILS_ENV']
  end

end; end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
turf-1.2.0 lib/turf/lookup.rb