Class: Pubid::Builder::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pubid/builder/base.rb

Overview

Base class for all flavor-specific builders.

Provides the shared build loop that converts parsed data into identifier objects. Flavors subclass and override:

  • select_class(data) to dispatch by data shape (default returns default_identifier_class).

  • cast(key, value) to transform raw parsed values into components.

  • handle_key(identifier, key, value) for non-attribute side effects.

The build loop:

  1. Flattens array-of-hashes input (from Parslet transforms)

  2. Asks select_class(data) for the identifier class

  3. Iterates each key-value pair

  4. Calls cast(key, value) to convert raw values to component objects

  5. Assigns the result to the identifier via setter

  6. Silently skips unknown attributes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier_class = nil) ⇒ Base

Returns a new instance of Base.



24
25
26
# File 'lib/pubid/builder/base.rb', line 24

def initialize(identifier_class = nil)
  @identifier_class = identifier_class
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



22
23
24
# File 'lib/pubid/builder/base.rb', line 22

def identifier
  @identifier
end

Instance Method Details

#build(data) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/pubid/builder/base.rb', line 28

def build(data)
  data = flatten_array(data) if data.is_a?(Array)
  identifier_class = @identifier_class || select_class(data)
  identifier = identifier_class.new
  assign_attributes(identifier, data)
  identifier
end