Sha256: ddd9ddd44ec0873c9432aac9cbc42e5d8904aaaedc08aa16eafb9adcf6169c0d

Contents?: true

Size: 1.29 KB

Versions: 33

Compression:

Stored size: 1.29 KB

Contents

module ActiveGraph
  module ClassArguments
    class << self
      INVALID_CLASS_ARGUMENT_ERROR = 'option must be String, Symbol, false, nil, or an Array of Symbols/Strings'

      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

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

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

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

      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
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
activegraph-10.1.1 lib/active_graph/class_arguments.rb
activegraph-11.0.1-java lib/active_graph/class_arguments.rb
activegraph-11.0.0-java lib/active_graph/class_arguments.rb
activegraph-10.1.0 lib/active_graph/class_arguments.rb
activegraph-11.0.0.beta.4-java lib/active_graph/class_arguments.rb
activegraph-10.0.2 lib/active_graph/class_arguments.rb
activegraph-11.0.0.beta.3-java lib/active_graph/class_arguments.rb
activegraph-11.0.0.beta.2-java lib/active_graph/class_arguments.rb
activegraph-11.0.0.beta.1-java lib/active_graph/class_arguments.rb
activegraph-10.0.1 lib/active_graph/class_arguments.rb
activegraph-10.0.0 lib/active_graph/class_arguments.rb
activegraph-10.0.0.pre.beta.11 lib/active_graph/class_arguments.rb
activegraph-10.0.0.pre.beta.10 lib/active_graph/class_arguments.rb