Class: Hashtags::Builder

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Builder

Returns a new instance of Builder.



25
26
27
# File 'lib/hashtags/builder.rb', line 25

def initialize(options = {})
  super(options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



2
3
4
# File 'lib/hashtags/builder.rb', line 2

def options
  @options
end

Class Method Details

.dom_data(options = {}) ⇒ Object



17
18
19
# File 'lib/hashtags/builder.rb', line 17

def self.dom_data(options = {})
  new(options).dom_data
end

.help(options = {}) ⇒ Object



21
22
23
# File 'lib/hashtags/builder.rb', line 21

def self.help(options = {})
  new(options).help
end

.to_hashtag(str, options = {}) ⇒ Object



7
8
9
# File 'lib/hashtags/builder.rb', line 7

def self.to_hashtag(str, options = {})
  new(options).to_hashtag(str)
end

.to_markup(str, options = {}) ⇒ Object



3
4
5
# File 'lib/hashtags/builder.rb', line 3

def self.to_markup(str, options = {})
  new(options).to_markup(str)
end

.used_hashtag_classes(str, options = {}) ⇒ Object



11
12
13
14
15
# File 'lib/hashtags/builder.rb', line 11

def self.used_hashtag_classes(str, options = {})
  new(options).hashtag_classes.select do |cls|
    cls.regexp.match?(str)
  end
end

Instance Method Details

#dom_dataObject

render textcomplete dom data



40
41
42
43
44
45
# File 'lib/hashtags/builder.rb', line 40

def dom_data
  { hashtags: {
    path: Engine.routes.url_helpers.hashtags_resources_path,
    strategies: hashtag_strategies
  } }
end

#filter_classes(cls) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/hashtags/builder.rb', line 54

def filter_classes(cls)
  return [] unless cls.present?
  res = cls
  res &= options[:only] if options[:only]
  res -= options[:except] if options[:except]
  res
end

#hashtag_classesObject



62
63
64
65
66
67
68
# File 'lib/hashtags/builder.rb', line 62

def hashtag_classes
  filter_classes(
    Resource.resource_classes +
    User.user_classes +
    Variable.variable_classes
  )
end

#helpObject

render help string



48
49
50
51
52
# File 'lib/hashtags/builder.rb', line 48

def help
  hashtag_classes.group_by(&:trigger).map do |trigger, cls|
    OpenStruct.new(hashtag_classes: cls, trigger: trigger, help_values: cls.map(&:help_values).flatten.compact.sort)
  end
end

#to_hashtag(str) ⇒ Object

collects hashtags from all hashtags classes



35
36
37
# File 'lib/hashtags/builder.rb', line 35

def to_hashtag(str)
  hashtag_classes.inject(str) { |res, cls| cls.to_hashtag(res) }
end

#to_markup(str) ⇒ Object

collects markup from all hashtags classes



30
31
32
# File 'lib/hashtags/builder.rb', line 30

def to_markup(str)
  hashtag_classes.inject(str) { |res, cls| cls.to_markup(res) }
end