Class: SoFarSoGood::Subchapter

Inherits:
Object
  • Object
show all
Defined in:
lib/so_far_so_good/subchapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Subchapter

Returns a new instance of Subchapter.



10
11
12
13
14
15
16
# File 'lib/so_far_so_good/subchapter.rb', line 10

def initialize(hash)
  @volume  = hash[:volume]
  @chapter = hash[:chapter]
  @name    = hash[:name]
  @year    = SoFarSoGood::YEAR
  @title   = SoFarSoGood::TITLE
end

Instance Attribute Details

#chapterObject (readonly)

Returns the value of attribute chapter.



7
8
9
# File 'lib/so_far_so_good/subchapter.rb', line 7

def chapter
  @chapter
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/so_far_so_good/subchapter.rb', line 8

def name
  @name
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/so_far_so_good/subchapter.rb', line 5

def title
  @title
end

#volumeObject (readonly)

Returns the value of attribute volume.



6
7
8
# File 'lib/so_far_so_good/subchapter.rb', line 6

def volume
  @volume
end

#yearObject (readonly)

Returns the value of attribute year.



4
5
6
# File 'lib/so_far_so_good/subchapter.rb', line 4

def year
  @year
end

Instance Method Details

#[](number) ⇒ Object



60
61
62
# File 'lib/so_far_so_good/subchapter.rb', line 60

def [](number)
  subparts.find { |s| s.number == number }
end

#inspectObject



64
65
66
# File 'lib/so_far_so_good/subchapter.rb', line 64

def inspect
  "#<SoFarSoGood::Subchapter year=#{year} title=#{title} volume=#{volume} chapter=#{chapter} name=\"#{name}\">"
end

#numbers(options = {}) ⇒ Object



18
19
20
# File 'lib/so_far_so_good/subchapter.rb', line 18

def numbers(options = {})
  @numbers ||= subparts(options).map { |s| s.number }
end

#subjects(options = {}) ⇒ Object



22
23
24
# File 'lib/so_far_so_good/subchapter.rb', line 22

def subjects(options = {})
  @subjects ||= subparts(options).map { |s| s.subject }
end

#subparts(options = {}) ⇒ Object



26
27
28
29
# File 'lib/so_far_so_good/subchapter.rb', line 26

def subparts(options = {})
  @subparts ||= sections.map { |node| SoFarSoGood::Subpart.new(to_hash.merge(:node => node)) }
  @subparts.select { |subpart| options.all? { |k,v| subpart.send(k) == v } }
end

#to_csv(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/so_far_so_good/subchapter.rb', line 41

def to_csv(options = {})
  CSV.generate do |csv|
    csv << HEADINGS
    subparts(options).each do |c|
      csv << [c.number , c.subject]
    end
  end
end

#to_hashObject



50
51
52
53
54
55
56
57
58
# File 'lib/so_far_so_good/subchapter.rb', line 50

def to_hash
  {
    :year    => year,
    :title   => title,
    :volume  => volume,
    :chapter => chapter,
    :name    => name
  }
end

#to_json(options = {}) ⇒ Object



37
38
39
# File 'lib/so_far_so_good/subchapter.rb', line 37

def to_json(options = {})
  subparts(options).to_json(options)
end

#to_md(options = {}) ⇒ Object



31
32
33
34
35
# File 'lib/so_far_so_good/subchapter.rb', line 31

def to_md(options = {})
  links = options.delete(:links) == true
  rows = subparts(options).map { |c| [ links ? "[#{c.number}](#{c.link})" : c.number , c.subject] }
  Terminal::Table.new(:rows => rows, :style => { :border_i => "|" }, :headings => HEADINGS).to_s
end