Module: Kennel::SettingsAsMethods

Included in:
Models::Base
Defined in:
lib/kennel/settings_as_methods.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

SETTING_OVERRIDABLE_METHODS =
[].freeze
AS_PROCS =
->(options) do
  # Fragile; depends on the fact that in both locations where AS_PROCS is
  # used, we're 2 frames away from the user code.
  file, line, = caller(2..2).first.split(":", 3)

  options.transform_values do |v|
    if v.class == Proc
      v
    else
      eval "-> { v }", nil, file, line.to_i
    end
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



20
21
22
23
# File 'lib/kennel/settings_as_methods.rb', line 20

def self.included(base)
  base.extend ClassMethods
  base.instance_variable_set(:@settings, [])
end

Instance Method Details

#initialize(options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/kennel/settings_as_methods.rb', line 68

def initialize(options = {})
  super()

  unless options.is_a?(Hash)
    raise ArgumentError, "Expected #{self.class.name}.new options to be a Hash, got a #{options.class}"
  end

  AS_PROCS.call(options).each do |name, block|
    self.class.send :validate_setting_exist, name
    define_singleton_method name, &block
  end

  # need expand_path so it works wih rake and when run individually
  pwd = /^#{Regexp.escape(Dir.pwd)}\//
  @invocation_location = caller.detect do |l|
    if found = File.expand_path(l).sub!(pwd, "")
      break found
    end
  end
end

#raise_with_location(error, message) ⇒ Object

Raises:

  • (error)


89
90
91
92
93
# File 'lib/kennel/settings_as_methods.rb', line 89

def raise_with_location(error, message)
  message = message.dup
  message << " on #{@invocation_location}" if @invocation_location
  raise error, message
end