Module: Pubid::Core::Identifier

Defined in:
lib/pubid/core/identifier.rb,
lib/pubid/core/identifier/base.rb

Defined Under Namespace

Classes: Base

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/pubid/core/identifier.rb', line 3

def config
  @config
end

Instance Method Details

#build_harmonized_stage_code(stage_or_code, substage = "00") ⇒ Object



58
59
60
# File 'lib/pubid/core/identifier.rb', line 58

def build_harmonized_stage_code(stage_or_code, substage = "00")
  HarmonizedStageCode.new(stage_or_code, substage, config: @config)
end

#build_stage(**args) ⇒ Object



42
43
44
# File 'lib/pubid/core/identifier.rb', line 42

def build_stage(**args)
  @config.stage_class.new(config: @config, **args)
end

#build_type(type, **args) ⇒ Object



54
55
56
# File 'lib/pubid/core/identifier.rb', line 54

def build_type(type, **args)
  @config.type_class.new(type, config: @config, **args)
end

#build_typed_stage(**args) ⇒ Object



62
63
64
# File 'lib/pubid/core/identifier.rb', line 62

def build_typed_stage(**args)
  @config.typed_stage_class.new(config: @config, **args)
end

#create(**opts) ⇒ Object

Resolve identifier’s class and create new identifier

See Also:

  • for available options


8
9
10
# File 'lib/pubid/core/identifier.rb', line 8

def create(**opts)
  resolve_identifier(opts)
end

#has_stage?(stage) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/pubid/core/identifier.rb', line 50

def has_stage?(stage)
  @config.stage_class.has_stage?(stage, config: @config)
end

#parse(*args) ⇒ Object

See Also:

  • Identifier::Base.parse


32
33
34
# File 'lib/pubid/core/identifier.rb', line 32

def parse(*args)
  Base.parse(*args)
end

#parse_stage(stage) ⇒ Object



46
47
48
# File 'lib/pubid/core/identifier.rb', line 46

def parse_stage(stage)
  @config.stage_class.parse(stage, config: @config)
end

#parseable?(pubid) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/pubid/core/identifier.rb', line 66

def parseable?(pubid)
  @config.prefixes.any? do |prefix|
    pubid.start_with?(prefix)
  end
end

#resolve_identifier(parameters = {}) ⇒ Object

Returns identifier’s class.

Parameters:

  • typed_stage_or_stage (String)

    typed stage or stage

Returns:

  • identifier’s class

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pubid/core/identifier.rb', line 14

def resolve_identifier(parameters = {})
  return @config.default_type.new(**parameters.dup.tap { |h| h.delete(:type) }) if parameters[:type].nil? && parameters[:stage].nil?

  @config.types.each do |identifier_type|
    return identifier_type.new(**parameters.dup.tap { |h| h.delete(:type) }) if identifier_type.type_match?(parameters)
  end

  # When stage is not typed stage and type is not defined
  if parameters[:type].nil? && (parameters[:stage].is_a?(Stage) || has_stage?(parameters[:stage]))
    return @config.default_type.new(stage: parameters[:stage], **parameters)
  end

  raise Errors::TypeStageParseError, "cannot parse typed stage or stage '#{parameters[:stage]}'" if parameters[:type].nil?

  raise Errors::ParseTypeError, "cannot parse type #{parameters[:type]}"
end

#set_config(config) ⇒ Object

Parse identifier from title



37
38
39
40
# File 'lib/pubid/core/identifier.rb', line 37

def set_config(config)
  @config = config
  @config.identifier_module = self
end