Class: Pubid::Core::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/pubid/core/type.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = nil, config:) ⇒ Type

Create new type

Parameters:

  • type (Symbol) (defaults to: nil)

Raises:



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pubid/core/type.rb', line 7

def initialize(type = nil, config:)
  @config = config
  if type.nil?
    type = @config.default_type.type[:key]
  end
  type = type.to_s.downcase.to_sym unless type.is_a?(Symbol)

  raise Errors::WrongTypeError, "#{type} type is not available" unless @config.type_names.key?(type)

  @type = type
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.has_type?(type, config:) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/pubid/core/type.rb', line 38

def self.has_type?(type, config:)
  config.type_names.any? do |_, v|
    v[:short] == type
  end
end

.parse(type_string, config:) ⇒ Object



19
20
21
22
23
24
# File 'lib/pubid/core/type.rb', line 19

def self.parse(type_string, config:)
  config.type_names.each do |type, values|
    return new(type, config: config) if values[:short] == type_string
  end
  raise Errors::ParseTypeError, "Cannot parse '#{type_string}' type"
end

Instance Method Details

#==(other) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/pubid/core/type.rb', line 30

def ==(other)
  return type == other if other.is_a?(Symbol)

  return false if other.nil?

  type == other.type
end

#to_s(format = :short) ⇒ Object



26
27
28
# File 'lib/pubid/core/type.rb', line 26

def to_s(format = :short)
  @config.type_names[type][format]
end