Class: Metanorma::Release::Publication

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/release/publication.rb

Constant Summary collapse

METADATA_VERSION =
1
DRAFT_STAGES =
%w[
  20 30 40 50
  working-draft committee-draft draft-standard final-draft
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier:, slug:, title:, edition:, stage:, doctype:, revdate:, files:, channels:, source: nil, metadata_formats: nil) ⇒ Publication

Returns a new instance of Publication.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/metanorma/release/publication.rb', line 71

def initialize(identifier:, slug:, title:, edition:, stage:, doctype:,
               revdate:, files:, channels:, source: nil, metadata_formats: nil)
  @identifier = identifier
  @slug = slug
  @title = title
  @edition = edition
  @stage = stage
  @doctype = doctype
  @revdate = revdate
  @files = files.freeze
  @channels = channels.freeze
  @source = source
  @metadata_formats = 
  freeze
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



68
69
70
# File 'lib/metanorma/release/publication.rb', line 68

def channels
  @channels
end

#doctypeObject (readonly)

Returns the value of attribute doctype.



68
69
70
# File 'lib/metanorma/release/publication.rb', line 68

def doctype
  @doctype
end

#editionObject (readonly)

Returns the value of attribute edition.



68
69
70
# File 'lib/metanorma/release/publication.rb', line 68

def edition
  @edition
end

#filesObject (readonly)

Returns the value of attribute files.



68
69
70
# File 'lib/metanorma/release/publication.rb', line 68

def files
  @files
end

#identifierObject (readonly)

Returns the value of attribute identifier.



68
69
70
# File 'lib/metanorma/release/publication.rb', line 68

def identifier
  @identifier
end

#revdateObject (readonly)

Returns the value of attribute revdate.



68
69
70
# File 'lib/metanorma/release/publication.rb', line 68

def revdate
  @revdate
end

#slugObject (readonly)

Returns the value of attribute slug.



68
69
70
# File 'lib/metanorma/release/publication.rb', line 68

def slug
  @slug
end

#sourceObject (readonly)

Returns the value of attribute source.



68
69
70
# File 'lib/metanorma/release/publication.rb', line 68

def source
  @source
end

#stageObject (readonly)

Returns the value of attribute stage.



68
69
70
# File 'lib/metanorma/release/publication.rb', line 68

def stage
  @stage
end

#titleObject (readonly)

Returns the value of attribute title.



68
69
70
# File 'lib/metanorma/release/publication.rb', line 68

def title
  @title
end

Class Method Details

.from_json(json_string) ⇒ Object



175
176
177
# File 'lib/metanorma/release/publication.rb', line 175

def self.from_json(json_string)
  PublicationSerializer.from_json(json_string)
end

.from_metadata_hash(data) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/metanorma/release/publication.rb', line 179

def self.(data)
  ident = data["identifier"] || data["id"]
  new(
    identifier: ident,
    slug: SlugStrategy.slug_from_identifier(ident),
    title: data["title"],
    edition: data["edition"],
    stage: data["stage"],
    doctype: data["doctype"],
    revdate: data["revdate"],
    files: [],
    channels: data["channels"] || [],
    source: nil,
    metadata_formats: data["formats"],
  )
end

.from_release_body(body) ⇒ Object



171
172
173
# File 'lib/metanorma/release/publication.rb', line 171

def self.from_release_body(body)
  PublicationSerializer.from_release_body(body)
end

Instance Method Details

#base_dirObject



91
92
93
# File 'lib/metanorma/release/publication.rb', line 91

def base_dir
  files.any? ? File.dirname(files.first.path) : "."
end

#content_hash(from_directory: nil) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/metanorma/release/publication.rb', line 95

def content_hash(from_directory: nil)
  dir = if from_directory && base_dir == "."
          from_directory
        else
          base_dir
        end
  ContentHash.of_directory(dir, base: slug)
end

#draft?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/metanorma/release/publication.rb', line 108

def draft?
  DRAFT_STAGES.include?(stage.to_s)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
# File 'lib/metanorma/release/publication.rb', line 112

def eql?(other)
  other.is_a?(self.class) && identifier == other.identifier &&
    edition == other.edition && stage == other.stage
end

#file?(format) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/metanorma/release/publication.rb', line 104

def file?(format)
  formats.include?(format)
end

#formatsObject



87
88
89
# File 'lib/metanorma/release/publication.rb', line 87

def formats
  @metadata_formats || files.map(&:format)
end

#hashObject



117
118
119
# File 'lib/metanorma/release/publication.rb', line 117

def hash
  [identifier, edition, stage].hash
end

#to_hObject



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/metanorma/release/publication.rb', line 121

def to_h
  h = {
    "id" => slug, "identifier" => identifier, "title" => title,
    "edition" => edition, "stage" => stage, "doctype" => doctype,
    "channels" => channels, "formats" => formats,
    "revdate" => revdate
  }
  h["source"] = source.to_h if source
  h["files"] = files.map(&:to_h) unless files.empty?
  h
end

#to_json(*_args) ⇒ Object



167
168
169
# File 'lib/metanorma/release/publication.rb', line 167

def to_json(*_args)
  PublicationSerializer.to_json(self)
end

#to_release_bodyObject



163
164
165
# File 'lib/metanorma/release/publication.rb', line 163

def to_release_body
  PublicationSerializer.to_release_body(self)
end

#with_channels(new_channels) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/metanorma/release/publication.rb', line 133

def with_channels(new_channels)
  self.class.new(
    identifier: identifier, slug: slug, title: title,
    edition: edition, stage: stage, doctype: doctype,
    revdate: revdate, files: files,
    channels: new_channels, source: source,
    metadata_formats: @metadata_formats
  )
end

#with_files_and_source(new_files, release, repo) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/metanorma/release/publication.rb', line 143

def with_files_and_source(new_files, release, repo)
  source = PublicationSource.new(
    owner: repo.owner, repo: repo.repo,
    tag: release.tag_name,
    url: release.html_url,
    date: release.published_at
  )
  pub_files = new_files.map do |f|
    name = File.basename(f)
    ext = File.extname(name).delete_prefix(".")
    PublicationFile.new(format: ext, name: name, path: f)
  end
  self.class.new(
    identifier: identifier, slug: slug, title: title,
    edition: edition, stage: stage, doctype: doctype,
    revdate: revdate, files: pub_files,
    channels: channels, source: source
  )
end