Class: Zizia::MetadataMapper

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

Overview

A null/base mapper that maps no fields.

Real mapper implementations need to provide ‘#fields`, enumerating over `Symbols` that represent the fields on the target object (e.g. an `ActiveFedora::Base`/`Hyrax::WorkBehavior`) that the mapper can handle. For each field in `#fields`, the mapper must respond to a matching method (i.e. `:title` => `#title`), and return the value(s) that should be set to the target’s attributes upon mapping.

To ease the implementation of field methods, this base class provides a ‘#method_missing` that forwards missing method names to a `#map_field` method. `#map_field` can be implemented to provide a generalized field mapping when a common pattern will be used for many methods. Callers should avoid relying on this protected method directly, since mappers may implement individual field methods in any other way (e.g. `def title; end`) to route around `#map_field`. Implementations are also free to override `#method_missing` if desired.

Mappers generally operate over some input ‘#metadata`. Example metadata types that mappers could be implemented over include `Hash`, `CSV`, `XML`, `RDF::Graph`, etc…; mappers are free to interpret or ignore the contents of their underlying metadata data structures at their leisure. Values for fields are /usually/ derived from the `#metadata`, but can also be generated from complex logic or even hard-coded.

Examples:

Using a MetadataMapper

mapper = MyMapper.new
mapper. = 
mapper.fields # => [:title, :author, :description]

mapper.title # => 'Some Title'

mapper.fields.map { |field| mapper.send(field) }

See Also:

Direct Known Subclasses

HashMapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



62
63
64
65
# File 'lib/zizia/metadata_mapper.rb', line 62

def method_missing(method_name, *args, &block)
  return map_field(method_name) if fields.include?(method_name)
  super
end

Instance Attribute Details

#metadataObject

Returns:

  • (Object)


46
47
48
# File 'lib/zizia/metadata_mapper.rb', line 46

def 
  @metadata
end

Instance Method Details

#field?(name) ⇒ Boolean

Parameters:

  • name (Symbol)

Returns:

  • (Boolean)


52
53
54
# File 'lib/zizia/metadata_mapper.rb', line 52

def field?(name)
  fields.include?(name)
end

#fieldsEnumerable<Symbol>

Returns The fields the mapper can process.

Returns:

  • (Enumerable<Symbol>)

    The fields the mapper can process



58
59
60
# File 'lib/zizia/metadata_mapper.rb', line 58

def fields
  []
end

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

Returns:

  • (Boolean)


67
68
69
# File 'lib/zizia/metadata_mapper.rb', line 67

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