class Yarrow::Content::Policy

Constants

DEFAULT_EXPANSION
DEFAULT_EXTENSIONS
DEFAULT_SOURCE_PATH
MODULE_SEPARATOR

Attributes

collection[R]
container[R]
entity[R]
expansion[R]
extensions[R]
module_prefix[R]
source_path[R]

Public Class Methods

from_spec(policy_label, policy_props, module_prefix="") click to toggle source

Construct a content policy from the given source specification.

# File lib/yarrow/content/policy.rb, line 13
def self.from_spec(policy_label, policy_props, module_prefix="")
  # TODO: validate length, structure etc

  # If the spec holds a symbol value then treat it as an entity mapping
  if policy_props.is_a?(Symbol)
    new(
      policy_label,
      policy_label,
      policy_props,
      DEFAULT_EXPANSION,
      DEFAULT_EXTENSIONS,
      policy_label.to_s,
      module_prefix
    )

  # If the spec holds a string value then treat it as a source path mapping
  elsif policy_props.is_a?(String)
    new(
      policy_label,
      policy_label,
      Yarrow::Symbols.to_singular(policy_label),
      DEFAULT_EXPANSION,
      DEFAULT_EXTENSIONS,
      policy_props,
      module_prefix
    )

  # Otherwise scan through the spec and fill in any gaps
  else
    # Use explicit collection name if provided
    collection = if policy_props.key?(:collection)
      policy_props[:collection]
    else
      # If an entity name is provided use its plural for the container name
      if policy_props.key?(:entity)
        Yarrow::Symbols.to_plural(policy_props[:entity])
      else
        Yarrow::Symbols.to_plural(policy_label)
      end
    end

    # Use explicit container name if provided
    container = if policy_props.key?(:container)
      policy_props[:container]
    else
      collection
    end

    # Use explicit entity name if provided
    entity = if policy_props.key?(:entity)
      policy_props[:entity]
    else
      if policy_props.key?(:collection)
        Yarrow::Symbols.to_singular(policy_props[:collection])
      else
        Yarrow::Symbols.to_singular(policy_label)
      end
    end

    # Set expansion strategy
    expansion = if policy_props.key?(:expansion)
      policy_props[:expansion]
    else
      DEFAULT_EXPANSION
    end

    # Set list of extensions to turn into documents
    extensions = if policy_props.key?(:extensions)
      policy_props[:extensions]
    else
      DEFAULT_EXTENSIONS
    end

    # If match path is provided, treat it as a basename
    source_path = if policy_props.key?(:source_path)
      policy_props[:source_path]
    else
      DEFAULT_SOURCE_PATH
    end

    # Construct the new policy
    new(
      container,
      collection,
      entity,
      expansion,
      extensions,
      source_path,
      module_prefix
    )
  end
end
new(container, collection, entity, expansion, extensions, source_path, module_prefix) click to toggle source
# File lib/yarrow/content/policy.rb, line 108
def initialize(container, collection, entity, expansion, extensions, source_path, module_prefix)
  @container = container
  @collection = collection
  @entity = entity
  @expansion = expansion.to_sym
  @extensions = extensions
  @source_path = source_path
  @module_prefix = module_prefix.split(MODULE_SEPARATOR)
end

Public Instance Methods

aggregator_const() click to toggle source
# File lib/yarrow/content/policy.rb, line 134
def aggregator_const
  case expansion
  when :filename_map then Expansion::FilenameMap
  when :directory_merge then Expansion::DirectoryMerge
  else
    raise "No match strategy exists for :#{expansion}"
  end
end
collection_const() click to toggle source
# File lib/yarrow/content/policy.rb, line 122
def collection_const
  begin
    @collection_const ||= Yarrow::Symbols.to_module_const([*module_prefix, collection])
  rescue NameError
    raise NameError, "cannot map undefined entity `#{collection}`"
  end
end
container_const() click to toggle source
# File lib/yarrow/content/policy.rb, line 118
def container_const
  @container_const ||= Yarrow::Symbols.to_module_const([*module_prefix, container])
end
entity_const() click to toggle source
# File lib/yarrow/content/policy.rb, line 130
def entity_const
  @entity_const ||= Yarrow::Symbols.to_module_const([*module_prefix, entity])
end
match_by_extension(candidate) click to toggle source
# File lib/yarrow/content/policy.rb, line 143
def match_by_extension(candidate)
  extensions.include?(candidate)
end