Class: Archsight::Annotations::Annotation

Inherits:
Object
  • Object
show all
Defined in:
lib/archsight/annotations/annotation.rb

Overview

Annotation represents a single annotation definition with its schema and behavior

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @key = key
  @description = options[:description]
  @explicit_title = options[:title]
  @filter = options[:filter]
  @enum = options[:enum]
  @validator = options[:validator]
  @sidebar = options.fetch(:sidebar, true)
  @list = options.fetch(:list, false)
  @editor = options.fetch(:editor, true)
  @type = options[:type]

  # Auto-add filter if enum present
  @filter ||= :word if @enum

  # Derive format from filter if not explicitly set
  @format = options[:format] || derive_format

  # Build regex for pattern annotations
  @regex = build_regex if pattern?
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/archsight/annotations/annotation.rb', line 7

def description
  @description
end

#editorObject (readonly)

Returns the value of attribute editor.



7
8
9
# File 'lib/archsight/annotations/annotation.rb', line 7

def editor
  @editor
end

#enumObject (readonly)

Returns the value of attribute enum.



7
8
9
# File 'lib/archsight/annotations/annotation.rb', line 7

def enum
  @enum
end

#filterObject (readonly)

Returns the value of attribute filter.



7
8
9
# File 'lib/archsight/annotations/annotation.rb', line 7

def filter
  @filter
end

#formatObject (readonly)

Returns the value of attribute format.



7
8
9
# File 'lib/archsight/annotations/annotation.rb', line 7

def format
  @format
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/archsight/annotations/annotation.rb', line 7

def key
  @key
end

#listObject (readonly)

Returns the value of attribute list.



7
8
9
# File 'lib/archsight/annotations/annotation.rb', line 7

def list
  @list
end

Returns the value of attribute sidebar.



7
8
9
# File 'lib/archsight/annotations/annotation.rb', line 7

def sidebar
  @sidebar
end

#typeObject (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

Returns:

  • (Boolean)


105
106
107
# File 'lib/archsight/annotations/annotation.rb', line 105

def code?
  @format == :ruby
end

#code_languageObject



113
114
115
# File 'lib/archsight/annotations/annotation.rb', line 113

def code_language
  @format if code?
end

#example_valueObject

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

Returns:

  • (Boolean)


45
46
47
# File 'lib/archsight/annotations/annotation.rb', line 45

def filterable?
  @filter && @sidebar != false
end

#has_validation?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/archsight/annotations/annotation.rb', line 57

def has_validation?
  @enum || @validator || @type.is_a?(Class)
end

#list?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/archsight/annotations/annotation.rb', line 49

def list?
  @filter == :list
end

#list_display?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/archsight/annotations/annotation.rb', line 53

def list_display?
  @list == true
end

#markdown?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/archsight/annotations/annotation.rb', line 101

def markdown?
  @format == :markdown
end

#matches?(test_key) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


109
110
111
# File 'lib/archsight/annotations/annotation.rb', line 109

def multiline?
  @format == :multiline
end

#pattern?Boolean

Schema Methods ===

Returns:

  • (Boolean)


33
34
35
# File 'lib/archsight/annotations/annotation.rb', line 33

def pattern?
  key.include?("*")
end

#titleObject



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)

Returns:

  • (Boolean)


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