Class: Yarrow::Content::Policy

Inherits:
Schema::Entity show all
Defined in:
lib/yarrow/content/policy.rb

Constant Summary collapse

DEFAULT_EXPANSION =
:filename_map
DEFAULT_EXTENSIONS =
[".md", ".yml", ".htm"]
DEFAULT_SOURCE_PATH =
"."
DEFAULT_MODULE_PREFIX =
""
MODULE_SEPARATOR =
"::"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Schema::Entity

[], attribute, dictionary, inherited, #initialize, #merge, #to_h

Constructor Details

This class inherits a constructor from Yarrow::Schema::Entity

Class Method Details

.from_spec(policy_label, policy_props = DEFAULT_SOURCE_PATH, module_prefix = DEFAULT_MODULE_PREFIX) ⇒ Object

Construct a content policy from the given source specification.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/yarrow/content/policy.rb', line 26

def self.from_spec(policy_label, policy_props=DEFAULT_SOURCE_PATH, module_prefix=DEFAULT_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({
      container: policy_label,
      collection: policy_label,
      entity: policy_props,
      expansion: DEFAULT_EXPANSION,
      extensions: DEFAULT_EXTENSIONS,
      source_path: policy_label.to_s,
      module_prefix: module_prefix
    })

  # If the spec holds a string value then treat it as a source path mapping
  elsif policy_props.is_a?(String)
    new(
      container: policy_label,
      collection: policy_label,
      entity: Yarrow::Symbols.to_singular(policy_label),
      expansion: DEFAULT_EXPANSION,
      extensions: DEFAULT_EXTENSIONS,
      source_path: policy_props,
      module_prefix: 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.
      # Otherwise fall back to a container name or policy label. 
      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. Otherwise fall back to the collection name.
    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: container,
      collection: collection,
      entity: entity,
      expansion: expansion,
      extensions: extensions,
      source_path: source_path,
      module_prefix: module_prefix
    })
  end
end

Instance Method Details

#aggregator_constObject



140
141
142
143
144
145
146
147
# File 'lib/yarrow/content/policy.rb', line 140

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_constObject



128
129
130
131
132
133
134
# File 'lib/yarrow/content/policy.rb', line 128

def collection_const
  begin
    @collection_const ||= Yarrow::Symbols.to_module_const([*module_prefix_parts, collection])
  rescue NameError
    raise NameError, "cannot map undefined entity `#{collection}`"
  end
end

#container_constObject



124
125
126
# File 'lib/yarrow/content/policy.rb', line 124

def container_const
  @container_const ||= Yarrow::Symbols.to_module_const([*module_prefix_parts, container])
end

#entity_constObject



136
137
138
# File 'lib/yarrow/content/policy.rb', line 136

def entity_const
  @entity_const ||= Yarrow::Symbols.to_module_const([*module_prefix_parts, entity])
end

#match_by_extension(candidate) ⇒ Object



149
150
151
# File 'lib/yarrow/content/policy.rb', line 149

def match_by_extension(candidate)
  extensions.include?(candidate)
end

#module_prefix_partsObject



120
121
122
# File 'lib/yarrow/content/policy.rb', line 120

def module_prefix_parts
  module_prefix.split(MODULE_SEPARATOR)
end