Class: Archsight::Annotations::Annotation
- Inherits:
-
Object
- Object
- Archsight::Annotations::Annotation
- Defined in:
- lib/archsight/annotations/annotation.rb
Overview
Annotation represents a single annotation definition with its schema and behavior
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#editor ⇒ Object
readonly
Returns the value of attribute editor.
-
#enum ⇒ Object
readonly
Returns the value of attribute enum.
-
#filter ⇒ Object
readonly
Returns the value of attribute filter.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#list ⇒ Object
readonly
Returns the value of attribute list.
-
#sidebar ⇒ Object
readonly
Returns the value of attribute sidebar.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #code? ⇒ Boolean
- #code_language ⇒ Object
-
#example_value ⇒ Object
Example value for templates.
- #filterable? ⇒ Boolean
- #has_validation? ⇒ Boolean
-
#initialize(key, options = {}) ⇒ Annotation
constructor
A new instance of Annotation.
- #list? ⇒ Boolean
- #list_display? ⇒ Boolean
- #markdown? ⇒ Boolean
- #matches?(test_key) ⇒ Boolean
- #multiline? ⇒ Boolean
-
#pattern? ⇒ Boolean
Schema Methods ===.
- #title ⇒ Object
-
#valid?(value) ⇒ Boolean
Check if value is valid (convenience method).
-
#validate(value) ⇒ Object
Validate a value and return array of error messages (empty if valid).
-
#value_for(instance) ⇒ Object
Get value(s) from instance Returns array for list annotations, coerced single value otherwise.
Constructor Details
#initialize(key, options = {}) ⇒ Annotation
Returns a new instance of Annotation.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/archsight/annotations/annotation.rb', line 9 def initialize(key, = {}) @key = key @description = [:description] @explicit_title = [:title] @filter = [:filter] @enum = [:enum] @validator = [:validator] @sidebar = .fetch(:sidebar, true) @list = .fetch(:list, false) @editor = .fetch(:editor, true) @type = [:type] # Auto-add filter if enum present @filter ||= :word if @enum # Derive format from filter if not explicitly set @format = [:format] || derive_format # Build regex for pattern annotations @regex = build_regex if pattern? end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
7 8 9 |
# File 'lib/archsight/annotations/annotation.rb', line 7 def description @description end |
#editor ⇒ Object (readonly)
Returns the value of attribute editor.
7 8 9 |
# File 'lib/archsight/annotations/annotation.rb', line 7 def editor @editor end |
#enum ⇒ Object (readonly)
Returns the value of attribute enum.
7 8 9 |
# File 'lib/archsight/annotations/annotation.rb', line 7 def enum @enum end |
#filter ⇒ Object (readonly)
Returns the value of attribute filter.
7 8 9 |
# File 'lib/archsight/annotations/annotation.rb', line 7 def filter @filter end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
7 8 9 |
# File 'lib/archsight/annotations/annotation.rb', line 7 def format @format end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/archsight/annotations/annotation.rb', line 7 def key @key end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
7 8 9 |
# File 'lib/archsight/annotations/annotation.rb', line 7 def list @list end |
#sidebar ⇒ Object (readonly)
Returns the value of attribute sidebar.
7 8 9 |
# File 'lib/archsight/annotations/annotation.rb', line 7 def @sidebar end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/archsight/annotations/annotation.rb', line 7 def type @type end |
Instance Method Details
#code? ⇒ Boolean
105 106 107 |
# File 'lib/archsight/annotations/annotation.rb', line 105 def code? @format == :ruby end |
#code_language ⇒ Object
113 114 115 |
# File 'lib/archsight/annotations/annotation.rb', line 113 def code_language @format if code? end |
#example_value ⇒ Object
Example value for templates
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/archsight/annotations/annotation.rb', line 118 def example_value if @enum @enum.first || "TODO" elsif @type == Float 0.0 elsif @type == Integer 0 else "TODO" end end |
#filterable? ⇒ Boolean
45 46 47 |
# File 'lib/archsight/annotations/annotation.rb', line 45 def filterable? @filter && @sidebar != false end |
#has_validation? ⇒ Boolean
57 58 59 |
# File 'lib/archsight/annotations/annotation.rb', line 57 def has_validation? @enum || @validator || @type.is_a?(Class) end |
#list? ⇒ Boolean
49 50 51 |
# File 'lib/archsight/annotations/annotation.rb', line 49 def list? @filter == :list end |
#list_display? ⇒ Boolean
53 54 55 |
# File 'lib/archsight/annotations/annotation.rb', line 53 def list_display? @list == true end |
#markdown? ⇒ Boolean
101 102 103 |
# File 'lib/archsight/annotations/annotation.rb', line 101 def markdown? @format == :markdown end |
#matches?(test_key) ⇒ Boolean
37 38 39 |
# File 'lib/archsight/annotations/annotation.rb', line 37 def matches?(test_key) pattern? ? @regex.match?(test_key) : key == test_key end |
#multiline? ⇒ Boolean
109 110 111 |
# File 'lib/archsight/annotations/annotation.rb', line 109 def multiline? @format == :multiline end |
#pattern? ⇒ Boolean
Schema Methods ===
33 34 35 |
# File 'lib/archsight/annotations/annotation.rb', line 33 def pattern? key.include?("*") end |
#title ⇒ Object
41 42 43 |
# File 'lib/archsight/annotations/annotation.rb', line 41 def title @explicit_title || key.split("/").last.capitalize end |
#valid?(value) ⇒ Boolean
Check if value is valid (convenience method)
97 98 99 |
# File 'lib/archsight/annotations/annotation.rb', line 97 def valid?(value) validate(value).empty? end |
#validate(value) ⇒ Object
Validate a value and return array of error messages (empty if valid)
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/archsight/annotations/annotation.rb', line 84 def validate(value) errors = [] #: Array[String] return errors if value.nil? validate_enum(value, errors) validate_custom(value, errors) if errors.empty? validate_type(value, errors) if errors.empty? validate_code(value, errors) if errors.empty? errors end |
#value_for(instance) ⇒ Object
Get value(s) from instance Returns array for list annotations, coerced single value otherwise
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/archsight/annotations/annotation.rb', line 65 def value_for(instance) raw = instance.annotations[key] if list? return [] if raw.nil? || raw.to_s.empty? raw.to_s.split(/,|\n/).map(&:strip).reject(&:empty?) else return nil if raw.nil? case @type when Integer then raw.to_i when Float then raw.to_f else raw end end end |