Class: Pubid::Nist::Identifier
- Inherits:
-
Core::Identifier::Base
- Object
- Core::Identifier::Base
- Pubid::Nist::Identifier
- Extended by:
- Forwardable
- Defined in:
- lib/pubid/nist/identifier.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#addendum ⇒ Object
Returns the value of attribute addendum.
-
#appendix ⇒ Object
Returns the value of attribute appendix.
-
#code ⇒ Object
Returns the value of attribute code.
-
#edition ⇒ Object
Returns the value of attribute edition.
-
#errata ⇒ Object
Returns the value of attribute errata.
-
#index ⇒ Object
Returns the value of attribute index.
-
#insert ⇒ Object
Returns the value of attribute insert.
-
#part ⇒ Object
Returns the value of attribute part.
-
#publisher ⇒ Object
Returns the value of attribute publisher.
-
#revision ⇒ Object
Returns the value of attribute revision.
-
#section ⇒ Object
Returns the value of attribute section.
-
#series ⇒ Object
Returns the value of attribute series.
-
#stage ⇒ Object
Returns the value of attribute stage.
-
#supplement ⇒ Object
Returns the value of attribute supplement.
-
#translation ⇒ Object
Returns the value of attribute translation.
-
#update ⇒ Object
Returns the value of attribute update.
-
#version ⇒ Object
Returns the value of attribute version.
-
#volume ⇒ Object
Returns the value of attribute volume.
Class Method Summary collapse
- .create(**opts) ⇒ Object
- .get_parser_class ⇒ Object
- .get_renderer_class ⇒ Object
- .get_transformer_class ⇒ Object
- .transform(params) ⇒ Object
- .update_old_code(code) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(publisher: "NIST", series:, number: nil, stage: nil, supplement: nil, edition_month: nil, edition_year: nil, edition_day: nil, update: nil, edition: nil, **opts) ⇒ Identifier
constructor
A new instance of Identifier.
- #merge(document) ⇒ Object
- #parse_edition(edition_month, edition_year, edition_day) ⇒ Object
- #to_json(*args) ⇒ Object
- #to_s(format = :short, without_edition: false) ⇒ Object
-
#weight ⇒ Object
returns weight based on amount of defined attributes.
Constructor Details
#initialize(publisher: "NIST", series:, number: nil, stage: nil, supplement: nil, edition_month: nil, edition_year: nil, edition_day: nil, update: nil, edition: nil, **opts) ⇒ Identifier
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pubid/nist/identifier.rb', line 16 def initialize(publisher: "NIST", series:, number: nil, stage: nil, supplement: nil, edition_month: nil, edition_year: nil, edition_day: nil, update: nil, edition: nil, **opts) @publisher = publisher.is_a?(Publisher) ? publisher : Publisher.new(publisher: publisher.to_s) @series = series.is_a?(Series) ? series : Series.new(series: series) @code = number @stage = Stage.new(**stage) if stage @supplement = (supplement.is_a?(Array) && "") || supplement if edition_month || edition_year @edition = parse_edition(edition_month, edition_year, edition_day) elsif edition @edition = Edition.new(number: edition) end @update = update opts.each { |key, value| send("#{key}=", value.to_s) } end |
Instance Attribute Details
#addendum ⇒ Object
Returns the value of attribute addendum.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def addendum @addendum end |
#appendix ⇒ Object
Returns the value of attribute appendix.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def appendix @appendix end |
#code ⇒ Object
Returns the value of attribute code.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def code @code end |
#edition ⇒ Object
Returns the value of attribute edition.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def edition @edition end |
#errata ⇒ Object
Returns the value of attribute errata.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def errata @errata end |
#index ⇒ Object
Returns the value of attribute index.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def index @index end |
#insert ⇒ Object
Returns the value of attribute insert.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def insert @insert end |
#part ⇒ Object
Returns the value of attribute part.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def part @part end |
#publisher ⇒ Object
Returns the value of attribute publisher.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def publisher @publisher end |
#revision ⇒ Object
Returns the value of attribute revision.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def revision @revision end |
#section ⇒ Object
Returns the value of attribute section.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def section @section end |
#series ⇒ Object
Returns the value of attribute series.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def series @series end |
#stage ⇒ Object
Returns the value of attribute stage.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def stage @stage end |
#supplement ⇒ Object
Returns the value of attribute supplement.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def supplement @supplement end |
#translation ⇒ Object
Returns the value of attribute translation.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def translation @translation end |
#update ⇒ Object
Returns the value of attribute update.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def update @update end |
#version ⇒ Object
Returns the value of attribute version.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def version @version end |
#volume ⇒ Object
Returns the value of attribute volume.
11 12 13 |
# File 'lib/pubid/nist/identifier.rb', line 11 def volume @volume end |
Class Method Details
.create(**opts) ⇒ Object
104 105 106 |
# File 'lib/pubid/nist/identifier.rb', line 104 def create(**opts) new(**opts) end |
.get_parser_class ⇒ Object
128 129 130 |
# File 'lib/pubid/nist/identifier.rb', line 128 def get_parser_class Parser end |
.get_renderer_class ⇒ Object
136 137 138 |
# File 'lib/pubid/nist/identifier.rb', line 136 def get_renderer_class Renderer::Base end |
.get_transformer_class ⇒ Object
132 133 134 |
# File 'lib/pubid/nist/identifier.rb', line 132 def get_transformer_class Transformer end |
.transform(params) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/pubid/nist/identifier.rb', line 108 def transform(params) # run transform through each element, # like running transformer.apply(number: 1) and transformer.apply(year: 1999) # instead of running transformer on whole hash, like running transformer.apply({ number: 1, year: 1999 }) # where rule for number or year only will be not applied # transformation only applied to rules matching the whole hash identifier_params = params.map do |k, v| get_transformer_class.new.apply({ k => v }, params) end.inject({}, :merge) if identifier_params[:addendum] return Addendum.new(base: new( **identifier_params.dup.tap { |h| h.delete(:addendum) } ), **identifier_params[:addendum]) end new(**identifier_params) end |
.update_old_code(code) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/pubid/nist/identifier.rb', line 74 def self.update_old_code(code) UPDATE_CODES.each do |from, to| code = code.gsub(from.match?(/^\/.*\/$/) ? Regexp.new(from[1..-2]) : from, to) end code end |
Instance Method Details
#==(other) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/pubid/nist/identifier.rb', line 53 def ==(other) other.instance_variables.each do |var| return false if instance_variable_get(var) != other.instance_variable_get(var) end true end |
#merge(document) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pubid/nist/identifier.rb', line 60 def merge(document) document.instance_variables.each do |var| val = document.instance_variable_get(var) current_val = instance_variable_get(var) if [:@series, :@publisher].include?(var) || (val && current_val.nil?) || (val && current_val.to_s.length < val.to_s.length) instance_variable_set(var, val) end end self end |
#parse_edition(edition_month, edition_year, edition_day) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pubid/nist/identifier.rb', line 33 def parse_edition(edition_month, edition_year, edition_day) if edition_month date = Date.parse("#{edition_day || '01'}/#{edition_month}/#{edition_year}") if edition_day Edition.new(month: date.month, year: date.year, day: date.day) else Edition.new(month: date.month, year: date.year) end else Edition.new(year: edition_year.to_i) end end |
#to_json(*args) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/pubid/nist/identifier.rb', line 86 def to_json(*args) result = { styles: { short: to_s(:short), abbrev: to_s(:abbrev), long: to_s(:long), mr: to_s(:mr), } } instance_variables.each do |var| val = instance_variable_get(var) result[var.to_s.gsub('@', '')] = val unless val.nil? end result.to_json(*args) end |
#to_s(format = :short, without_edition: false) ⇒ Object
82 83 84 |
# File 'lib/pubid/nist/identifier.rb', line 82 def to_s(format = :short, without_edition: false) self.class.get_renderer_class.new(to_h(deep: false)).render(format: format, without_edition: without_edition) end |
#weight ⇒ Object
returns weight based on amount of defined attributes
47 48 49 50 51 |
# File 'lib/pubid/nist/identifier.rb', line 47 def weight instance_variables.inject(0) do |sum, var| sum + (instance_variable_get(var).nil? ? 0 : 1) end end |