Class: Brief::Document::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/brief/document/section.rb,
lib/brief/document/section/builder.rb,
lib/brief/document/section/mapping.rb

Defined Under Namespace

Classes: Builder, Mapping

Constant Summary collapse

Headings =
%w(h1 h2 h3 h4 h5 h6)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, fragment, config) ⇒ Section

Returns a new instance of Section.



7
8
9
10
11
# File 'lib/brief/document/section.rb', line 7

def initialize(title, fragment, config)
  @title = title
  @fragment = fragment
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/brief/document/section.rb', line 3

def config
  @config
end

#fragmentObject

Returns the value of attribute fragment.



2
3
4
# File 'lib/brief/document/section.rb', line 2

def fragment
  @fragment
end

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/brief/document/section.rb', line 2

def title
  @title
end

Instance Method Details

#itemsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/brief/document/section.rb', line 13

def items
  return @items if @items

  data = []

  config.selectors.each do |selector|
    settings = config.selector_config[selector]

    if Headings.include?(selector)
      headings = fragment.css('article > h2')
      articles = headings.map(&:parent)

      unless settings.empty?
        articles.compact.each do |article|
          data.push(settings.inject({}.to_mash) do |memo, pair|
            attribute, selector = pair
            result = article.css(selector)
            memo[attribute] = result.length > 1 ? result.map(&:text) : result.text
            memo
          end)
        end
      end
    end
  end

  @items = data
end