Module: Expectant::Utils

Defined in:
lib/expectant/utils.rb

Class Method Summary collapse

Class Method Details

.define_with_collision_policy(target, method_name, collision:, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/expectant/utils.rb', line 25

def define_with_collision_policy(target, method_name, collision:, &block)
  method_name = method_name.to_sym
  if target.method_defined?(method_name) || target.private_method_defined?(method_name)
    case collision
    when :error
      raise ConfigurationError, "Method #{method_name} already defined"
    when :force
      begin
        target.send(:remove_method, method_name)
      rescue
        nil
      end
      block.call
    else
      raise ConfigurationError, "Unknown collision policy: #{collision}"
    end
  else
    block.call
  end
end

.singularize(word) ⇒ Object



9
10
11
# File 'lib/expectant/utils.rb', line 9

def singularize(word)
  ActiveSupport::Inflector.singularize(word.to_s).to_sym
end

.validator_method_name(schema_name, configuration) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/expectant/utils.rb', line 13

def validator_method_name(schema_name, configuration)
  prefix = configuration.validator_prefix
  suffix = configuration.validator_suffix

  parts = []
  parts << prefix if prefix
  parts << schema_name
  parts << suffix if suffix

  parts.join("_")
end