Class: Hashtags::Base

Inherits:
Struct
  • Object
show all
Defined in:
lib/hashtags/base.rb

Overview

Base class, from which all Hashtag types inherit In case you wish to add a new type next to • ResourceType • Resource • User • Variable you might want to inherit from Base.

Direct Known Subclasses

Resource, ResourceType, User, Variable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#strObject

Returns the value of attribute str

Returns:

  • (Object)

    the current value of str



10
11
12
# File 'lib/hashtags/base.rb', line 10

def str
  @str
end

Class Method Details

.cache_keyObject

used to expire field with tags

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/hashtags/base.rb', line 61

def self.cache_key
  raise NotImplementedError
end

.descendantsObject



19
20
21
# File 'lib/hashtags/base.rb', line 19

def self.descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

.help_valuesObject

implement to show which values particular trigger offers fe # -> lists all available resource types



81
82
# File 'lib/hashtags/base.rb', line 81

def self.help_values
end

.json_for_query(query) ⇒ Object

return JSON version of resources that match query this is returned when user starts typing (the query)

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/hashtags/base.rb', line 111

def self.json_for_query(query)
  raise NotImplementedError
end

.json_regexp(regexp) ⇒ Object

converts Ruby tegexp to JS regexp



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hashtags/base.rb', line 121

def self.json_regexp(regexp)
  str = regexp.inspect
              .sub('\\A', '^')
              .sub('\\Z', '$')
              .sub('\\z', '$')
              .sub(/^\//, '')
              .sub(/\/[a-z]*$/, '')
              .gsub(/\(\?#.+\)/, '')
              .gsub(/\(\?-\w+:/, '(')
              .gsub(/\s/, '')
  Regexp.new(str).source
end

.match_indexObject

index of the match group from the above regexp to be used for matching

Raises:

  • (NotImplementedError)


93
94
95
# File 'lib/hashtags/base.rb', line 93

def self.match_index
  raise NotImplementedError
end

.match_regexpObject

trigger dropdown when user input matches this regexp

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/hashtags/base.rb', line 88

def self.match_regexp
  raise NotImplementedError
end

.match_templateObject

matched against as user types typically this would equal to the .template (match what you see)



56
57
58
# File 'lib/hashtags/base.rb', line 56

def self.match_template
  template
end

.pathObject

implement to use custom controller



71
72
# File 'lib/hashtags/base.rb', line 71

def self.path
end

.regexpObject

regexp to recognize the complete tag

Raises:

  • (NotImplementedError)


66
67
68
# File 'lib/hashtags/base.rb', line 66

def self.regexp
  raise NotImplementedError
end

.replaceObject

tag that gets inserted into the field

Raises:

  • (NotImplementedError)


98
99
100
# File 'lib/hashtags/base.rb', line 98

def self.replace
  raise NotImplementedError
end

.resource_classesObject



23
24
25
# File 'lib/hashtags/base.rb', line 23

def self.resource_classes
  Resource.descendants
end

.strategy(hashtag_classes) ⇒ Object

to be passed to textcomplete



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

def self.strategy(hashtag_classes)
  {
    class_name: to_s,
    match_regexp: json_regexp(match_regexp),
    match_index: match_index,
    match_template: match_template,
    path: path,
    replace: replace,
    template: template,
    values: values(hashtag_classes)
  }
end

.templateObject

fragment to be displayed in the dropdown menu

Raises:

  • (NotImplementedError)


103
104
105
# File 'lib/hashtags/base.rb', line 103

def self.template
  raise NotImplementedError
end

.to_hashtag(str) ⇒ Object



15
16
17
# File 'lib/hashtags/base.rb', line 15

def self.to_hashtag(str)
  new(str).to_hashtag
end

.to_markup(str) ⇒ Object



11
12
13
# File 'lib/hashtags/base.rb', line 11

def self.to_markup(str)
  new(str).to_markup
end

.triggerObject

for example @ # $ …

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/hashtags/base.rb', line 50

def self.trigger
  raise NotImplementedError
end

.user_classesObject



27
28
29
# File 'lib/hashtags/base.rb', line 27

def self.user_classes
  User.descendants
end

.values(hashtag_classes = Variable.descendants) ⇒ Object

implement to preload hash tag values, fe in case there is limited number (ie variable names)



76
77
# File 'lib/hashtags/base.rb', line 76

def self.values(hashtag_classes)
end

.variable_classesObject



31
32
33
# File 'lib/hashtags/base.rb', line 31

def self.variable_classes
  Variable.descendants
end

Instance Method Details

#hashtag(match) ⇒ Object

the proper hashtag (so it can be updated automatically)

Raises:

  • (NotImplementedError)


155
156
157
# File 'lib/hashtags/base.rb', line 155

def hashtag(match)
  raise NotImplementedError
end

#markup(match) ⇒ Object

what is the hashtag replaced with in the end

Raises:

  • (NotImplementedError)


160
161
162
# File 'lib/hashtags/base.rb', line 160

def markup(match)
  raise NotImplementedError
end

#to_hashtagObject

Updates hash tags



145
146
147
148
149
150
# File 'lib/hashtags/base.rb', line 145

def to_hashtag
  str.to_s.gsub(self.class.regexp) do |match|
    ht = hashtag(Regexp.last_match)
    match = ht || match
  end
end

#to_markupObject

Converts hash tags to markup



137
138
139
140
141
142
# File 'lib/hashtags/base.rb', line 137

def to_markup
  str.to_s.gsub(self.class.regexp) do |match|
    m = markup(Regexp.last_match)
    match = m || match
  end
end