class TMBundle::Menu
Public Class Methods
exists?(uuid)
click to toggle source
# File lib/tm_bundle/menu.rb, line 29 def self.exists?(uuid) uuids.key? uuid end
find_or_create_new_item(uuid, order, attributes = {})
click to toggle source
# File lib/tm_bundle/menu.rb, line 13 def self.find_or_create_new_item(uuid, order, attributes = {}) item = if uuid.squeeze == '-' Separator.new(uuid, order, attributes) elsif exists?(uuid) uuids[uuid].checkout(attributes.to_h) else Item.new(uuid, order, attributes.to_h) end items.add item if item.is_a?(Item) && item.root? unless item.is_a? Separator uuids[uuid] ||= item end item end
new(tree, path)
click to toggle source
# File lib/tm_bundle/menu.rb, line 33 def initialize(tree, path) @path = path submenus, items, @excludedItems = *tree.values_at("submenus", "items", "excludedItems") process_subtree(submenus) end
Public Instance Methods
iterate_to_plist(hash, parent = nil)
click to toggle source
# File lib/tm_bundle/menu.rb, line 73 def iterate_to_plist(hash, parent = nil) ary = [] hash.each do |name, tree| uuid = tree[:uuid] if tree[:items] ary << uuid @menu[:submenus][uuid] = { :items => iterate_to_plist(tree[:items]), :name => name } else ary << uuid end end ary end
make_hash_tree!(items)
click to toggle source
# File lib/tm_bundle/menu.rb, line 58 def make_hash_tree!(items) items.reduce(Hash[]) do |m, item| hash = { uuid: item.uuid } hash[:items] = make_hash_tree!(item.items) if item.items.any? m.merge! item.name => hash end end
prepare_items_to_plist(hash)
click to toggle source
# File lib/tm_bundle/menu.rb, line 67 def prepare_items_to_plist(hash) @menu = { submenus: {}, items: [], excludedItems: Array.wrap(@excludedItems) } @menu[:items] = Array.wrap(iterate_to_plist(hash)) @menu end
process_subtree(submenus)
click to toggle source
# File lib/tm_bundle/menu.rb, line 39 def process_subtree(submenus) submenus.each do |uuid, hash| self.class.find_or_create_new_item(uuid, submenus.keys.index(uuid), hash.symbolize_keys) end end
read_yaml_hieararchy!(file)
click to toggle source
# File lib/tm_bundle/menu.rb, line 52 def read_yaml_hieararchy!(file) yaml = file.read yaml.gsub!(/^(\s*)(.*)\s*#\s*(.*)$/){|s,n| "#{$1}%s \n#{$1} :uuid: %s" % [$2, $3] } prepare_items_to_plist YAML.load(yaml) end
write_yaml_hierarchy!(prefix = nil)
click to toggle source
# File lib/tm_bundle/menu.rb, line 45 def write_yaml_hierarchy!(prefix = nil) p prefix tree = make_hash_tree! uuids.values.select(&:root?) yaml = tree.to_yaml.gsub(/^(.*)\s*$\n\s*:uuid:(.*)$/){|s,n| "%-60s # %s" % [$1, $2] } (Pathname.new(@path)/"menu#{prefix}.yml").write(yaml) end