Class: Zizia::InputRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/zizia/input_record.rb

Overview

Examples:

Building an importer with the factory

record = InputRecord.from({some: :metadata}, mapper: MyMapper.new)
record.some # => :metadata

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapper: Zizia.config.metadata_mapper_class.new) ⇒ InputRecord

Returns a new instance of InputRecord.

Parameters:

  • mapper (#map_fields) (defaults to: Zizia.config.metadata_mapper_class.new)


17
18
19
# File 'lib/zizia/input_record.rb', line 17

def initialize(mapper: Zizia.config..new)
  self.mapper = mapper
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Respond to methods matching mapper fields



54
55
56
57
# File 'lib/zizia/input_record.rb', line 54

def method_missing(method_name, *args, &block)
  return super unless mapper.field?(method_name)
  mapper.public_send(method_name)
end

Instance Attribute Details

#mapper#map_fields

Returns:

  • (#map_fields)


13
14
15
# File 'lib/zizia/input_record.rb', line 13

def mapper
  @mapper
end

Class Method Details

.from(metadata:, mapper: Zizia.config.metadata_mapper_class.new) ⇒ InputRecord

Returns an input record mapping metadata with the given mapper.

Parameters:

  • metadata (Object)
  • mapper (#map_fields) (defaults to: Zizia.config.metadata_mapper_class.new)

Returns:

  • (InputRecord)

    an input record mapping metadata with the given mapper



28
29
30
31
# File 'lib/zizia/input_record.rb', line 28

def from(metadata:, mapper: Zizia.config..new)
  mapper. = 
  new(mapper: mapper)
end

Instance Method Details

#attributesHash<Symbol, Object>

Returns:

  • (Hash<Symbol, Object>)


36
37
38
39
40
# File 'lib/zizia/input_record.rb', line 36

def attributes
  mapper.fields.each_with_object({}) do |field, attrs|
    attrs[field] = public_send(field)
  end
end

#representative_fileString?

Returns an identifier for the representative file; nil if none is given.

Returns:

  • (String, nil)

    an identifier for the representative file; nil if none is given.



45
46
47
48
49
50
# File 'lib/zizia/input_record.rb', line 45

def representative_file
  return mapper.representative_file if
    mapper.respond_to?(:representative_file)

  nil
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



61
62
63
# File 'lib/zizia/input_record.rb', line 61

def respond_to_missing?(method_name, include_private = false)
  mapper.field?(method_name) || super
end