module Jekyll::Tasks::Menus

Obtain menus information

Public Instance Methods

per_collection(properties = ['name']) click to toggle source
# File lib/jekyll/tasks/menus.rb, line 44
def per_collection(properties = ['name'])
  result = []
  Collections.get.each do |collection|
    data = search_properties(collection, properties)
    hash = { 'collection' => collection, 'properties' => data }
    result << hash unless data.empty?
  end
  result
end
search_conflicts() click to toggle source
# File lib/jekyll/tasks/menus.rb, line 9
def search_conflicts
  conflicts = []
  conflicts << (search_conflicts_with_properties | search_conflicts_without_properties)
  conflicts.flatten!
end
search_conflicts_with_properties() click to toggle source
# File lib/jekyll/tasks/menus.rb, line 15
def search_conflicts_with_properties
  conflicts = []

  per_collection(%w[name father]).each do |el|
    properties = el['properties']

    index = search_hash(properties, 'name')
    name = index.nil? ? '' : properties[index]['name']

    index = search_hash(properties, 'father')
    father = index.nil? ? '' : properties[index]['father']

    conflicts << analyze_conflicts(el['collection'], name, father) if name != father
  end

  conflicts
end
search_conflicts_without_properties() click to toggle source
# File lib/jekyll/tasks/menus.rb, line 33
def search_conflicts_without_properties
  conflicts = []

  Collections.get.each do |collection|
    data = search_without_properties(collection) unless collection == 'pages'
    conflicts << data unless data.nil?
  end

  conflicts
end
search_properties(collection, properties, result = []) click to toggle source
# File lib/jekyll/tasks/menus.rb, line 66
def search_properties(collection, properties, result = [])
  files = markdown_files collection
  files.each do |file|
    data = YAML.load_file(file)
    properties.each do |property|
      search_property result, data, property
    end
  end
  order_properties(result, properties)
end
search_without_properties(collection) click to toggle source
# File lib/jekyll/tasks/menus.rb, line 54
def search_without_properties(collection)
  conflicts = []

  files = markdown_files collection
  files.each do |file|
    data = YAML.load_file(file)
    conflicts << analyze_conflicts_without_properties(data)
  end

  conflicts
end

Private Instance Methods

analyze_conflicts(collection, name, father, conflicts = []) click to toggle source
# File lib/jekyll/tasks/menus.rb, line 94
def analyze_conflicts(collection, name, father, conflicts = [])
  array = name - father
  conflicts << "There are some unused menus at #{collection} collection: #{array}" unless array.empty?

  array = father - name
  unless array.empty?
    conflicts << "There are some products pointed to non-existent menus at #{collection} collection: #{array}"
  end

  conflicts
end
analyze_conflicts_without_properties(data, conflicts = []) click to toggle source
# File lib/jekyll/tasks/menus.rb, line 79
def analyze_conflicts_without_properties(data, conflicts = [])
  conflicts << "Product without father: '#{data['title']}'" if Products.product_without_father data
  conflicts << "Menu without name: '#{data['title']}'" if menu_without_name data
  conflicts << "Menu without father: '#{data['title']}'" if menu_without_father data
  conflicts
end
markdown_files(colecction) click to toggle source
# File lib/jekyll/tasks/menus.rb, line 129
def markdown_files(colecction)
  Dir.glob("./_#{colecction}/**/*.md").to_a.sort
end
menu_without_father(data) click to toggle source
menu_without_name(data) click to toggle source
order_properties(result, properties) click to toggle source
# File lib/jekyll/tasks/menus.rb, line 114
def order_properties(result, properties)
  properties.each do |property|
    index = search_hash(result, property)
    result[index][property] = result[index][property].sort.uniq unless index.nil?
  end
  result
end
search_hash(list, property) click to toggle source
# File lib/jekyll/tasks/menus.rb, line 122
def search_hash(list, property)
  list.each_with_index do |hash, index|
    return index if hash[property]
  end
  nil
end
search_property(result, data, property) click to toggle source
# File lib/jekyll/tasks/menus.rb, line 106
def search_property(result, data, property)
  value = data["menu-#{property}"]
  index = search_hash(result, property)

  (result[index][property] << value unless value.nil?) if index
  (result << { property => value.nil? ? [] : [value] }) unless index
end