Module: ActiveGraph::ClassArguments

Defined in:
lib/active_graph/class_arguments.rb

Constant Summary collapse

INVALID_CLASS_ARGUMENT_ERROR =
'option must be String, Symbol, false, nil, or an Array of Symbols/Strings'

Class Method Summary collapse

Class Method Details

.constantize_argument(class_argument) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_graph/class_arguments.rb', line 21

def constantize_argument(class_argument)
  case class_argument
  when 'any', :any, false, nil
    nil
  when Array
    class_argument.map(&method(:constantize_argument))
  else
    class_argument.to_s.constantize.tap do |class_constant|
      if !node_model?(class_constant)
        fail ArgumentError, "#{class_constant} is not an Node model"
      end
    end
  end
rescue NameError
  raise ArgumentError, "Could not find class: #{class_argument}"
end

.node_model?(class_constant) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/active_graph/class_arguments.rb', line 17

def node_model?(class_constant)
  class_constant.included_modules.include?(ActiveGraph::Node)
end

.valid_argument?(class_argument) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
# File 'lib/active_graph/class_arguments.rb', line 6

def valid_argument?(class_argument)
  [NilClass, String, Symbol, FalseClass].include?(class_argument.class) ||
    (class_argument.is_a?(Array) && class_argument.all? { |c| [Symbol, String].include?(c.class) })
end

.validate_argument!(class_argument, context) ⇒ Object



11
12
13
14
15
# File 'lib/active_graph/class_arguments.rb', line 11

def validate_argument!(class_argument, context)
  return if valid_argument?(class_argument)

  fail ArgumentError, "#{context} #{INVALID_CLASS_ARGUMENT_ERROR} (was #{class_argument.inspect})"
end