Class: ArchSpec::MethodSignature

Inherits:
Data
  • Object
show all
Defined in:
lib/archspec/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



9
10
11
# File 'lib/archspec/model.rb', line 9

def block
  @block
end

#forwardObject (readonly)

Returns the value of attribute forward

Returns:

  • (Object)

    the current value of forward



9
10
11
# File 'lib/archspec/model.rb', line 9

def forward
  @forward
end

#keyword_restObject (readonly)

Returns the value of attribute keyword_rest

Returns:

  • (Object)

    the current value of keyword_rest



9
10
11
# File 'lib/archspec/model.rb', line 9

def keyword_rest
  @keyword_rest
end

#keywordsObject (readonly)

Returns the value of attribute keywords

Returns:

  • (Object)

    the current value of keywords



9
10
11
# File 'lib/archspec/model.rb', line 9

def keywords
  @keywords
end

#optionalObject (readonly)

Returns the value of attribute optional

Returns:

  • (Object)

    the current value of optional



9
10
11
# File 'lib/archspec/model.rb', line 9

def optional
  @optional
end

#optional_keywordsObject (readonly)

Returns the value of attribute optional_keywords

Returns:

  • (Object)

    the current value of optional_keywords



9
10
11
# File 'lib/archspec/model.rb', line 9

def optional_keywords
  @optional_keywords
end

#requiredObject (readonly)

Returns the value of attribute required

Returns:

  • (Object)

    the current value of required



9
10
11
# File 'lib/archspec/model.rb', line 9

def required
  @required
end

#restObject (readonly)

Returns the value of attribute rest

Returns:

  • (Object)

    the current value of rest



9
10
11
# File 'lib/archspec/model.rb', line 9

def rest
  @rest
end

Instance Method Details

#accepts_arity?(arity) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/archspec/model.rb', line 19

def accepts_arity?(arity)
  return true if forward

  maximum = rest ? Float::INFINITY : required + optional
  arity >= required && arity <= maximum
end

#accepts_keywords?(names) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
# File 'lib/archspec/model.rb', line 26

def accepts_keywords?(names)
  return true if forward

  names = names.to_set
  return false unless keywords.all? { |name| names.include?(name) }
  return true if keyword_rest

  names.all? { |name| keywords.include?(name) || optional_keywords.include?(name) }
end

#describeObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/archspec/model.rb', line 36

def describe
  positional =
    if rest
      "#{required}+ positional"
    elsif optional.positive?
      "#{required}..#{required + optional} positional"
    else
      "#{required} positional"
    end
  named = (keywords.map { |name| "#{name}:" } + optional_keywords.map { |name| "#{name}:?" }).join(', ')
  [positional, ("keywords #{named}" unless named.empty?), ('**keywords' if keyword_rest), ('&block' if block),
   ('forwarding' if forward)].compact.join(', ')
end