Class: ROM::Files::Schema

Inherits:
Schema
  • Object
show all
Defined in:
lib/rom/files/schema.rb,
lib/rom/files/schema/inferrer.rb,
lib/rom/files/schema/attributes_inferrer.rb

Overview

Specialized schema for files adapter

Defined Under Namespace

Classes: AttributesInferrer, Inferrer

Instance Method Summary collapse

Instance Method Details

#call(relation) ⇒ Relation

Abstract method for creating a new relation based on schema definition

This can be used by views to generate a new relation automatically. In example a schema can project a relation, join any additional relations if it include_patterns attributes from other relations etc.

Default implementation is a no-op and it simply returns back untouched relation

Parameters:

Returns:



26
27
28
# File 'lib/rom/files/schema.rb', line 26

def call(relation)
  relation.new(relation.dataset.with(row_proc: row_proc), schema: self)
end

#contents_for(tuple) ⇒ String

Parameters:

  • tuple (Hash)

Returns:

  • (String)


56
57
58
59
60
61
# File 'lib/rom/files/schema.rb', line 56

def contents_for(tuple)
  contents = attributes.each_with_object([]) do |attr, result|
    result << tuple[attr.name] if attr.meta[DATA]
  end.compact
  contents.join if contents.any?
end

#finalize_associations!(relations:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal hook used during setup process



68
69
70
71
72
73
74
# File 'lib/rom/files/schema.rb', line 68

def finalize_associations!(relations:)
  super do
    associations.map do |definition|
      Files::Associations.const_get(definition.type).new(definition, relations)
    end
  end
end

#identify(tuple) ⇒ Pathname

Parameters:

  • tuple (Hash)

Returns:



46
47
48
49
50
51
52
# File 'lib/rom/files/schema.rb', line 46

def identify(tuple)
  path = (primary_key_names || [ID]).map do |name|
    tuple[name]
  end.compact
  return unless path.any?
  Pathname(path.shift).join(*path)
end

#load_attributes(pathname) ⇒ Hash{Symbol => Object}

Parameters:

Returns:

  • (Hash{Symbol => Object})


37
38
39
40
41
42
# File 'lib/rom/files/schema.rb', line 37

def load_attributes(pathname)
  attributes.each_with_object({}) do |attribute, result|
    result[attribute.name] = attribute.(pathname)
    result
  end
end

#row_procMethod, Proc

Returns:

  • (Method, Proc)


31
32
33
# File 'lib/rom/files/schema.rb', line 31

def row_proc
  method(:load_attributes)
end