Class: Valkyrie::ValueMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/value_mapper.rb

Overview

ValueMapper is a way to handle coordinating extendable casting of values depending on what the value is. It’s used in many of the adapters in Valkyrie.

Typically a root node is defined as a sub-class ValueMapper to separate value mappers for a distinct purpose, but it’s not a requirement.

Examples:

Defining a ValueMapper which converts symbols to strings.

class ParentMapper < ValueMapper
end
class Stringify < ValueMapper
  ParentMapper.register(self)
  def self.handles?(value)
    value.kind_of?(Symbol)
  end
  def result
    value.to_s
  end
end

Use a ValueMapper

ParentMapper.for(:symbol).result # => "symbol"

Direct Known Subclasses

Persistence::Fedora::Persister::ModelConverter::FedoraValue, Persistence::Fedora::Persister::ModelConverter::MappedFedoraValue, Persistence::Fedora::Persister::ModelConverter::NestedProperty, Persistence::Fedora::Persister::ModelConverter::OrderedMembers, Persistence::Fedora::Persister::ModelConverter::OrderedProperties, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::BooleanValue, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::CreatedAtValue, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::DateTimeValue, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::DenylistedValue, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::DifferentSubject, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::FedoraValue, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::FloatValue, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::IntegerValue, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::InternalModelValue, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::InternalURI, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::LiteralValue, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::MemberID, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::NestedValue, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::OrderedProperty, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::TimeValue, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::UpdatedAtValue, Persistence::Fedora::Persister::OrmConverter::GraphToAttributes::ValkyrieIDValue, Persistence::Shared::JSONValueMapper::DateValue, Persistence::Shared::JSONValueMapper::EnumeratorValue, Persistence::Shared::JSONValueMapper::HashValue, Persistence::Shared::JSONValueMapper::IDValue, Persistence::Shared::JSONValueMapper::NestedRecord, Persistence::Shared::JSONValueMapper::PostgresValue, Persistence::Shared::JSONValueMapper::URIValue, Persistence::Solr::ModelConverter::BooleanPropertyValue, Persistence::Solr::ModelConverter::DateTimePropertyValue, Persistence::Solr::ModelConverter::EnumerableValue, Persistence::Solr::ModelConverter::FloatPropertyValue, Persistence::Solr::ModelConverter::IDPropertyValue, Persistence::Solr::ModelConverter::IntegerPropertyValue, Persistence::Solr::ModelConverter::LiteralPropertyValue, Persistence::Solr::ModelConverter::NestedObjectValue, Persistence::Solr::ModelConverter::SharedStringPropertyValue, Persistence::Solr::ModelConverter::SolrMapperValue, Persistence::Solr::ModelConverter::StringPropertyValue, Persistence::Solr::ModelConverter::URIPropertyValue, Persistence::Solr::ORMConverter::BooleanValue, Persistence::Solr::ORMConverter::DateTimeValue, Persistence::Solr::ORMConverter::EnumerableValue, Persistence::Solr::ORMConverter::FloatValue, Persistence::Solr::ORMConverter::IDValue, Persistence::Solr::ORMConverter::IntegerValue, Persistence::Solr::ORMConverter::NestedEnumerable, Persistence::Solr::ORMConverter::NestedResourceConverter, Persistence::Solr::ORMConverter::NestedResourceHash, Persistence::Solr::ORMConverter::NestedResourceID, Persistence::Solr::ORMConverter::NestedResourceLiteral, Persistence::Solr::ORMConverter::NestedResourceURI, Persistence::Solr::ORMConverter::NestedResourceValue, Persistence::Solr::ORMConverter::PropertyValue, Persistence::Solr::ORMConverter::RDFLiteralPropertyValue, Persistence::Solr::ORMConverter::SolrValue, Persistence::Solr::ORMConverter::URIValue

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, calling_mapper) ⇒ ValueMapper

Returns a new instance of ValueMapper.



57
58
59
60
# File 'lib/valkyrie/value_mapper.rb', line 57

def initialize(value, calling_mapper)
  @value = value
  @calling_mapper = calling_mapper
end

Class Attribute Details

.value_castersArray<Valkyrie::ValueMapper>

Returns Registered value casters.

Returns:



33
34
35
# File 'lib/valkyrie/value_mapper.rb', line 33

def self.value_casters
  @value_casters ||= []
end

Instance Attribute Details

#calling_mapperObject (readonly)

Returns the value of attribute calling_mapper.



56
57
58
# File 'lib/valkyrie/value_mapper.rb', line 56

def calling_mapper
  @calling_mapper
end

#valueObject (readonly)

Returns the value of attribute value.



56
57
58
# File 'lib/valkyrie/value_mapper.rb', line 56

def value
  @value
end

Class Method Details

.for(value) ⇒ Object

Find the value caster for a given value.

Parameters:

  • value (Anything)

    The value to find a caster for.



43
44
45
46
47
# File 'lib/valkyrie/value_mapper.rb', line 43

def self.for(value)
  (value_casters + [self]).find do |value_caster|
    value_caster.handles?(value)
  end.new(value, self)
end

.handles?(_value) ⇒ Boolean

Test whether this caster handles a given value.

Parameters:

  • value (Anything)

Returns:

  • (Boolean)


52
53
54
# File 'lib/valkyrie/value_mapper.rb', line 52

def self.handles?(_value)
  true
end

.register(value_caster) ⇒ Object

Register a value caster.

Parameters:



28
29
30
# File 'lib/valkyrie/value_mapper.rb', line 28

def self.register(value_caster)
  self.value_casters += [value_caster]
end

Instance Method Details

#resultObject

Returns Casted value.

Returns:

  • Casted value.



63
64
65
# File 'lib/valkyrie/value_mapper.rb', line 63

def result
  value
end