Module: Neo4j::ClassArguments

Defined in:
lib/neo4j/class_arguments.rb

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.active_node_model?(class_constant) ⇒ Boolean

Returns:

  • (Boolean)

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

def active_node_model?(class_constant)
  class_constant.included_modules.include?(Neo4j::ActiveNode)
end

.constantize_argument(class_argument) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/neo4j/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 !active_node_model?(class_constant)
        fail ArgumentError, "#{class_constant} is not an ActiveNode model"
      end
    end
  end
rescue NameError
  raise ArgumentError, "Could not find class: #{class_argument}"
end

.valid_argument?(class_argument) ⇒ Boolean

Returns:

  • (Boolean)

6
7
8
9
# File 'lib/neo4j/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/neo4j/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