class Alchemy::ElementDefinition

Public Class Methods

add(element) click to toggle source

Add additional page definitions to collection.

Useful for extending the elements from an Alchemy module.

Usage Example

Call +Alchemy::ElementDefinition.add(your_definition)+ in your engine.rb file.

@param [Array || Hash]

You can pass a single element definition as Hash, or a collection of elements as Array.
# File lib/alchemy/element_definition.rb, line 26
def add(element)
  all
  if element.is_a?(Array)
    @definitions += element
  elsif element.is_a?(Hash)
    @definitions << element
  else
    raise TypeError
  end
end
all() click to toggle source

Returns the definitions from elements.yml file.

Place a elements.yml file inside your apps config/alchemy folder to define your own set of elements

# File lib/alchemy/element_definition.rb, line 11
def all
  @definitions ||= read_definitions_file.map(&:with_indifferent_access)
end
definitions_file_path() click to toggle source

The absolute elements.yml file path @return [Pathname]

# File lib/alchemy/element_definition.rb, line 51
def definitions_file_path
  Rails.root.join("config", "alchemy", "elements.yml")
end
get(name) click to toggle source

Returns one element definition by given name.

# File lib/alchemy/element_definition.rb, line 39
def get(name)
  return {} if name.blank?

  all.detect { |a| a["name"] == name }
end
reset!() click to toggle source
# File lib/alchemy/element_definition.rb, line 45
def reset!
  @definitions = nil
end

Private Class Methods

read_definitions_file() click to toggle source

Reads the element definitions from config/alchemy/elements.yml.

# File lib/alchemy/element_definition.rb, line 59
def read_definitions_file
  if File.exist?(definitions_file_path)
    YAML.safe_load(
      ERB.new(File.read(definitions_file_path)).result,
      permitted_classes: YAML_PERMITTED_CLASSES,
      aliases: true
    ) || []
  else
    raise LoadError,
      "Could not find elements.yml file! Please run `rails generate alchemy:install`"
  end
end