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
  options.transform_values do |v|
    if v.class == Proc
      v
    else
      -> { v }
    end
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



16
17
18
19
# File 'lib/kennel/settings_as_methods.rb', line 16

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

Instance Method Details

#initialize(options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kennel/settings_as_methods.rb', line 64

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)


85
86
87
88
89
# File 'lib/kennel/settings_as_methods.rb', line 85

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