Class: Raml::Parameter::AbstractParameter

Inherits:
Raml::PropertiesNode show all
Includes:
Documentable, Merge, Raml::Parent
Defined in:
lib/raml/node/parameter/abstract_parameter.rb

Constant Summary collapse

VALID_TYPES =
%w(string number integer date boolean file)

Instance Attribute Summary collapse

Attributes included from Raml::Parent

#children

Attributes included from Documentable

#description, #display_name

Attributes inherited from Raml::PropertiesNode

#optional

Attributes inherited from Node

#name, #parent

Instance Method Summary collapse

Methods included from Merge

#merge_properties

Methods included from Documentable

#html_description

Methods inherited from Raml::PropertiesNode

#_regexp_property, #non_scalar_properties, #scalar_properties

Methods inherited from Node

#document, relative_path

Constructor Details

#initialize(name, parameter_data, parent) ⇒ AbstractParameter

Returns a new instance of AbstractParameter.

Parameters:

  • name (String)

    the parameter name.

  • parameter_data (Hash, Array<Hash>)

    the parameter data. If the parameter supports multiple types, it should be an array of hashes, one hash each for each type.

  • parent (Raml::Node)

    the parameter’s parent node.



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/raml/node/parameter/abstract_parameter.rb', line 67

def initialize(name, parameter_data, parent)
  if parameter_data.is_a? Array
    @name       = name
    @children ||= []
    parameter_data.each do |parameter|
      @children << self.class.new(name, parameter, self)
    end
  elsif parameter_data.is_a? Hash
    super
  end
end

Instance Attribute Details

#defaultString, ...

Returns the default value.

Returns:

  • (String, Numeric, Boolean, nil)

    the default value.



# File 'lib/raml/node/parameter/abstract_parameter.rb', line 38

#enumArray<String>?

Returns the possible values. Only valid for parameters of type “string”.

Returns:

  • (Array<String>, nil)

    the possible values. Only valid for parameters of type “string”.



# File 'lib/raml/node/parameter/abstract_parameter.rb', line 16

#exampleString, ...

Returns an example of the value.

Returns:

  • (String, Numeric, Boolean, nil)

    an example of the value.



# File 'lib/raml/node/parameter/abstract_parameter.rb', line 35

#max_lengthInteger?

Returns the maximum value length. Only valid for parameters of type “string”.

Returns:

  • (Integer, nil)

    the maximum value length. Only valid for parameters of type “string”.



# File 'lib/raml/node/parameter/abstract_parameter.rb', line 26

#maximumNumeric?

Returns the maximum value length. Only valid for parameters of type “number” or “integer”.

Returns:

  • (Numeric, nil)

    the maximum value length. Only valid for parameters of type “number” or “integer”.



# File 'lib/raml/node/parameter/abstract_parameter.rb', line 32

#min_lengthInteger?

Returns the minimum value length. Only valid for parameters of type “string”.

Returns:

  • (Integer, nil)

    the minimum value length. Only valid for parameters of type “string”.



# File 'lib/raml/node/parameter/abstract_parameter.rb', line 23

#minimumNumeric?

Returns the minimum value length. Only valid for parameters of type “number” or “integer”.

Returns:

  • (Numeric, nil)

    the minimum value length. Only valid for parameters of type “number” or “integer”.



# File 'lib/raml/node/parameter/abstract_parameter.rb', line 29

#patternRegexp?

Returns a regular expression the value must match. Only valid for parameters of type “string”.

Returns:

  • (Regexp, nil)

    a regular expression the value must match. Only valid for parameters of type “string”.



# File 'lib/raml/node/parameter/abstract_parameter.rb', line 19

#repeatBoolean

Returns whether the parameter can be repeated.

Returns:

  • (Boolean)

    whether the parameter can be repeated.



# File 'lib/raml/node/parameter/abstract_parameter.rb', line 44

#requiredBoolean

Returns whether the parameter is required.

Returns:

  • (Boolean)

    whether the parameter is required.



# File 'lib/raml/node/parameter/abstract_parameter.rb', line 41

#typeString

Returns the value type. One of: “string”, “number”, “integer”, “date”, “boolean”, or “file”.

Returns:

  • (String)

    the value type. One of: “string”, “number”, “integer”, “date”, “boolean”, or “file”.



# File 'lib/raml/node/parameter/abstract_parameter.rb', line 12

#typesHash<String, Raml::Parameter::AbstractParameter> (readonly)

Returns if the parameter supports multiple types, the type alternatives, keyed by the type.

Returns:



51
52
53
# File 'lib/raml/node/parameter/abstract_parameter.rb', line 51

scalar_property :type       , :enum     , :pattern  , :min_length , 
:max_length , :minimum  , :maximum  , :example    , 
:repeat     , :required , :default

Instance Method Details

#has_multiple_types?Boolean

Returns true if the parameter supports multiple type alternatives, false otherwise.

Returns:

  • (Boolean)

    true if the parameter supports multiple type alternatives, false otherwise.



80
81
82
# File 'lib/raml/node/parameter/abstract_parameter.rb', line 80

def has_multiple_types?
  not children.empty?
end

#merge(other) ⇒ Object

Raises:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/raml/node/parameter/abstract_parameter.rb', line 85

def merge(other)
  raise MergeError, "#{self.class} names don't match." if name != other.name

  case [ has_multiple_types?, other.has_multiple_types? ]
  when [ true , true  ]
    match, no_match = other.types.values.partition { |param| types.include? param.type }

    # Merge parameters with the same type.
    match = Hash[ match.map { |param| [ param.type, param ] } ]
    types.each { |type, param| param.merge match[type] if match[type] }

    # Add parameters with no matching type.
    @children.concat no_match

  when [ true , false ]
    if types[other.type]
      types[other.type].merge other
    else
      @children << other
    end

  when [ false, true  ]
    if other.types[self.type]
      self.merge other.types[self.type]
      @children << self.clone
      @children.concat other.types.values.reject { |type| self.type == type.type }
      reset

    else
      @children << self.clone
      @children.concat other.types.values
      reset
    end

  when [ false, false ]
    super
  end

  self
end