Class: Annotable::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/annotable/method.rb

Overview

An annotated method with its annotations.

one = Annotation.new(:one)
two = Annotation.new(:two)
some_method = Method.new(:some_method, one, two)

some_method.annotation_exist?(:one) # => true
some_method.annotation_exist?(:no) # => false
some_method.select_annotations(:one) # => [#<Annotable::Annotation @name=:one, @options={}, @params=[]>]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *annotations) ⇒ Method

Creates a new annotated method.

Parameters:

  • name (Symbol)

    The method name

  • annotations (Array<Annotation>)

    The annotations linked to this method

Raises:

  • (ArgumentError)


27
28
29
30
31
32
# File 'lib/annotable/method.rb', line 27

def initialize(name, *annotations)
  raise ArgumentError, "You must provide at least one annotation" if annotations.empty?

  @name = name
  @annotations = annotations
end

Instance Attribute Details

#annotationsArray<Annotation> (readonly)

Returns The annotations declared for this method.

Returns:

  • (Array<Annotation>)

    The annotations declared for this method



19
20
21
# File 'lib/annotable/method.rb', line 19

def annotations
  @annotations
end

#nameSymbol (readonly)

Returns The method name.

Returns:

  • (Symbol)

    The method name



17
18
19
# File 'lib/annotable/method.rb', line 17

def name
  @name
end

Instance Method Details

#annotation_exist?(name) ⇒ Boolean

Determines whether annotation exists based on its name.

Parameters:

  • name (Symbol)

    The annotation’s name to check for

Returns:

  • (Boolean)

    True if the annotation exists, false otherwise



41
42
43
# File 'lib/annotable/method.rb', line 41

def annotation_exist?(name)
  !annotations.find { |a| a.name == name }.nil?
end

#find_annotation(*names) ⇒ Annotation

Finds the first annotation matching one of the given names.

Parameters:

  • *names (Array<Symbol>)

    The annotation names to find

Returns:

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
# File 'lib/annotable/method.rb', line 67

def find_annotation(*names)
  raise ArgumentError, "You must provide at least one name to find" if names.empty?

  annotations.find do |a|
    names.include? a.name
  end
end

#select_annotations(*names) ⇒ Array<Annotation>

Returns all annotations matching the given names.

Parameters:

  • names (Array<Symbol>)

    Names of the annotations to select

Returns:

  • (Array<Annotation>)

    The matching annotations

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
# File 'lib/annotable/method.rb', line 52

def select_annotations(*names)
  raise ArgumentError, "You must provide at least one name to select" if names.empty?

  annotations.select do |a|
    names.include? a.name
  end
end