Class: Pubid::Nist::Identifier::Base
- Inherits:
-
Core::Identifier::Base
- Object
- Core::Identifier::Base
- Pubid::Nist::Identifier::Base
- Extended by:
- Forwardable
- Defined in:
- lib/pubid/nist/identifier/base.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) ⇒ Base
constructor
A new instance of Base.
- #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) ⇒ Base
Returns a new instance of Base.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/pubid/nist/identifier/base.rb', line 17 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.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def addendum @addendum end |
#appendix ⇒ Object
Returns the value of attribute appendix.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def appendix @appendix end |
#code ⇒ Object
Returns the value of attribute code.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def code @code end |
#edition ⇒ Object
Returns the value of attribute edition.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def edition @edition end |
#errata ⇒ Object
Returns the value of attribute errata.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def errata @errata end |
#index ⇒ Object
Returns the value of attribute index.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def index @index end |
#insert ⇒ Object
Returns the value of attribute insert.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def insert @insert end |
#part ⇒ Object
Returns the value of attribute part.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def part @part end |
#publisher ⇒ Object
Returns the value of attribute publisher.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def publisher @publisher end |
#revision ⇒ Object
Returns the value of attribute revision.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def revision @revision end |
#section ⇒ Object
Returns the value of attribute section.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def section @section end |
#series ⇒ Object
Returns the value of attribute series.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def series @series end |
#stage ⇒ Object
Returns the value of attribute stage.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def stage @stage end |
#supplement ⇒ Object
Returns the value of attribute supplement.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def supplement @supplement end |
#translation ⇒ Object
Returns the value of attribute translation.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def translation @translation end |
#update ⇒ Object
Returns the value of attribute update.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def update @update end |
#version ⇒ Object
Returns the value of attribute version.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def version @version end |
#volume ⇒ Object
Returns the value of attribute volume.
12 13 14 |
# File 'lib/pubid/nist/identifier/base.rb', line 12 def volume @volume end |
Class Method Details
.create(**opts) ⇒ Object
105 106 107 |
# File 'lib/pubid/nist/identifier/base.rb', line 105 def create(**opts) new(**opts) end |
.get_parser_class ⇒ Object
129 130 131 |
# File 'lib/pubid/nist/identifier/base.rb', line 129 def get_parser_class Parser end |
.get_renderer_class ⇒ Object
137 138 139 |
# File 'lib/pubid/nist/identifier/base.rb', line 137 def get_renderer_class Renderer::Base end |
.get_transformer_class ⇒ Object
133 134 135 |
# File 'lib/pubid/nist/identifier/base.rb', line 133 def get_transformer_class Transformer end |
.transform(params) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/pubid/nist/identifier/base.rb', line 109 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
75 76 77 78 79 80 |
# File 'lib/pubid/nist/identifier/base.rb', line 75 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
54 55 56 57 58 59 |
# File 'lib/pubid/nist/identifier/base.rb', line 54 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
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/pubid/nist/identifier/base.rb', line 61 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
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pubid/nist/identifier/base.rb', line 34 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/pubid/nist/identifier/base.rb', line 87 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
83 84 85 |
# File 'lib/pubid/nist/identifier/base.rb', line 83 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
48 49 50 51 52 |
# File 'lib/pubid/nist/identifier/base.rb', line 48 def weight instance_variables.inject(0) do |sum, var| sum + (instance_variable_get(var).nil? ? 0 : 1) end end |