Class: TMBundle::Menu

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/tm_bundle/item.rb,
lib/tm_bundle/menu.rb

Defined Under Namespace

Classes: Item, Separator

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, path) ⇒ Menu

Returns a new instance of Menu.



33
34
35
36
37
# 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

Class Method Details

.exists?(uuid) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/tm_bundle/menu.rb', line 29

def self.exists?(uuid)
  uuids.key? uuid
end

.find_or_create_new_item(uuid, order, attributes = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#iterate_to_plist(hash, parent = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



58
59
60
61
62
63
64
65
# 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) ⇒ Object



67
68
69
70
71
# 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) ⇒ Object



39
40
41
42
43
# 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) ⇒ Object



52
53
54
55
56
# 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) ⇒ Object



45
46
47
48
49
50
# 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