Class: Commenter::CommentSheet

Inherits:
Object
  • Object
show all
Defined in:
lib/commenter/comment_sheet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ CommentSheet

Returns a new instance of CommentSheet.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/commenter/comment_sheet.rb', line 9

def initialize(attributes = {})
  # Normalize input to symbols
  attrs = symbolize_keys(attributes)

  @version = attrs[:version] || "2012-03"
  @date = attrs[:date]
  @document = attrs[:document]
  @project = attrs[:project]
  @stage = attrs[:stage]
  @title_en = attrs[:title_en]
  @title_fr = attrs[:title_fr]
  @comments = (attrs[:comments] || []).map { |c| c.is_a?(Comment) ? c : Comment.from_hash(c) }
end

Instance Attribute Details

#commentsObject

Returns the value of attribute comments.



7
8
9
# File 'lib/commenter/comment_sheet.rb', line 7

def comments
  @comments
end

#dateObject

Returns the value of attribute date.



7
8
9
# File 'lib/commenter/comment_sheet.rb', line 7

def date
  @date
end

#documentObject

Returns the value of attribute document.



7
8
9
# File 'lib/commenter/comment_sheet.rb', line 7

def document
  @document
end

#projectObject

Returns the value of attribute project.



7
8
9
# File 'lib/commenter/comment_sheet.rb', line 7

def project
  @project
end

#stageObject

Returns the value of attribute stage.



7
8
9
# File 'lib/commenter/comment_sheet.rb', line 7

def stage
  @stage
end

#title_enObject

Returns the value of attribute title_en.



7
8
9
# File 'lib/commenter/comment_sheet.rb', line 7

def title_en
  @title_en
end

#title_frObject

Returns the value of attribute title_fr.



7
8
9
# File 'lib/commenter/comment_sheet.rb', line 7

def title_fr
  @title_fr
end

#versionObject

Returns the value of attribute version.



7
8
9
# File 'lib/commenter/comment_sheet.rb', line 7

def version
  @version
end

Class Method Details

.from_hash(hash) ⇒ Object



56
57
58
# File 'lib/commenter/comment_sheet.rb', line 56

def self.from_hash(hash)
  new(hash)
end

Instance Method Details

#add_comment(comment) ⇒ Object



23
24
25
# File 'lib/commenter/comment_sheet.rb', line 23

def add_comment(comment)
  @comments << (comment.is_a?(Comment) ? comment : Comment.from_hash(comment))
end

#schema_nameObject



48
49
50
# File 'lib/commenter/comment_sheet.rb', line 48

def schema_name
  version == "osd" ? "iso_comment_osd.yaml" : "iso_comment_2012-03.yaml"
end

#to_hObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/commenter/comment_sheet.rb', line 27

def to_h
  {
    version: @version,
    date: @date,
    document: @document,
    project: @project,
    stage: @stage,
    title_en: @title_en,
    title_fr: @title_fr,
    comments: @comments.map(&:to_h)
  }
end

#to_yaml_document(schema_dir = "schema") ⇒ Object



52
53
54
# File 'lib/commenter/comment_sheet.rb', line 52

def to_yaml_document(schema_dir = "schema")
  "# yaml-language-server: $schema=#{File.join(schema_dir.to_s, schema_name)}\n\n#{to_yaml_h.to_yaml}"
end

#to_yaml_hObject



40
41
42
43
44
45
46
# File 'lib/commenter/comment_sheet.rb', line 40

def to_yaml_h
  hash = to_h.merge(comments: @comments.map(&:to_yaml_h))
  # Remove nil-valued keys for cleaner YAML output
  hash.delete(:title_en) if hash[:title_en].nil?
  hash.delete(:title_fr) if hash[:title_fr].nil?
  stringify_keys(hash)
end