Class: Annotable::Method
- Inherits:
-
Object
- Object
- Annotable::Method
- 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
-
#annotations ⇒ Array<Annotation>
readonly
The annotations declared for this method.
-
#name ⇒ Symbol
readonly
The method name.
Instance Method Summary collapse
-
#annotation_exist?(name) ⇒ Boolean
Determines whether annotation exists based on its name.
-
#find_annotation(*names) ⇒ Annotation
Finds the first annotation matching one of the given names.
-
#initialize(name, *annotations) ⇒ Method
constructor
Creates a new annotated method.
-
#select_annotations(*names) ⇒ Array<Annotation>
Returns all annotations matching the given names.
Constructor Details
#initialize(name, *annotations) ⇒ Method
Creates a new annotated method.
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
#annotations ⇒ Array<Annotation> (readonly)
Returns The annotations declared for this method.
19 20 21 |
# File 'lib/annotable/method.rb', line 19 def annotations @annotations end |
#name ⇒ Symbol (readonly)
Returns 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.
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.
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.
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 |