Module: Shaped

Defined in:
lib/shaped.rb,
lib/shaped.rb,
lib/shaped/version.rb

Defined Under Namespace

Modules: Shapes Classes: InvalidShapeDescription, Shape

Constant Summary collapse

VERSION =
'0.14.0'

Class Method Summary collapse

Class Method Details

.Shape(*shape_descriptions) ⇒ Object

rubocop:disable Naming/MethodName, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/shaped.rb', line 13

def self.Shape(*shape_descriptions)
  validation_options = shape_descriptions.extract_options!
  if shape_descriptions.size >= 2
    Shaped::Shapes::Any.new(*shape_descriptions, validation_options)
  else
    # If the shape_descriptions argument list was just one hash, then `extract_options!` would
    # have removed it, making `shape_descriptions` an empty array, so we need to "restore" the
    # "validation options" to their actual role of a Hash `shape_description` here.
    shape_description =
      case
      when shape_descriptions.empty? && validation_options.is_a?(Hash)
        validation_options
      else
        shape_descriptions.first
      end

    case shape_description
    when Shaped::Shape then shape_description
    when Hash then Shaped::Shapes::Hash.new(shape_description)
    when Array then Shaped::Shapes::Array.new(shape_description)
    when Symbol then Shaped::Shapes::Method.new(shape_description)
    when Class then Shaped::Shapes::Class.new(shape_description, validation_options)
    else
      if shape_description.respond_to?(:call)
        Shaped::Shapes::Callable.new(shape_description)
      else
        Shaped::Shapes::Equality.new(shape_description)
      end
    end
  end
end