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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
# File 'lib/kennel/settings_as_methods.rb', line 6

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

Instance Method Details

#initialize(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kennel/settings_as_methods.rb', line 55

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

  options.each do |k, v|
    next if v.class == Proc
    raise ArgumentError, "Expected #{self.class.name}.new option :#{k} to be Proc, for example `#{k}: -> { 12 }`"
  end

  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)


81
82
83
84
85
# File 'lib/kennel/settings_as_methods.rb', line 81

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