Class: Moka::OrderGivers::PageOrderGiver

Inherits:
Thor::Group
  • Object
show all
Includes:
SiteTree, Thor::Actions
Defined in:
lib/commands/order_pages.rb

Constant Summary

Constants included from SiteTree

SiteTree::RESERVED_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.counter_to_letters(c) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/commands/order_pages.rb', line 103

def self.counter_to_letters(c)
  letters = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z)
  if c < letters.size
    return letters[c]
  else
    return PageOrderGiver.counter_to_letters(c/letters.size - 1) + letters[c%letters.size]
  end
end

.letters_to_counter(l) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/commands/order_pages.rb', line 112

def self.letters_to_counter(l)
  letters = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z)
  l.gsub(/[^a-z]/, "")
  if l.size == 0
    return -1
  end
  unless letters.index(l).nil?
    return letters.index(l)
  else
    return letters.index(l[-1]) + letters.size*(PageOrderGiver.letters_to_counter(l[0..-2]))
  end
end

Instance Method Details

#ask_for_new_orderObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/commands/order_pages.rb', line 53

def ask_for_new_order
  counter = 0
  say ""
  say "The current page order in group '#{group_name}' is the following:\n"
  @group.pages.order_by(:order).each do |page|
    say "#{PageOrderGiver.counter_to_letters(counter)} - #{page.name}"
    counter += 1
  end
  say ""
  @input = ask "Specify a new ordering as a string of space-separated indices, or press enter to do nothing (example: to switch the second and the third items insert 'a c b'):\n"
end

#dump_manifestObject



83
84
85
86
87
88
# File 'lib/commands/order_pages.rb', line 83

def dump_manifest
  f = File.open( File.expand_path('manifest.yml', MOKA_ROOT), 'w' ) do |out|
    YAML.dump( @manifest, out )
  end
  say_status "update", "manifest.yml", :green
end

#load_manifestObject



40
41
42
43
# File 'lib/commands/order_pages.rb', line 40

def load_manifest
  @manifest = YAML.load_file(File.expand_path("manifest.yml", MOKA_ROOT))
  @site = SiteNode.new("site", @manifest["site"])
end

#notify_new_orderObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/commands/order_pages.rb', line 90

def notify_new_order
  say ""
  say "The new page order in group '#{group_name}' is the following:\n"
  counter = 0
  @site = SiteNode.new("", @manifest["site"])
  @group = @site.find_group(group_name)
  @group.pages.each do |page|
    say "#{PageOrderGiver.counter_to_letters(counter)} - #{page.name}"
    counter += 1
  end
  say ""
end

#provide_help_if_neededObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/commands/order_pages.rb', line 20

def provide_help_if_needed
  if options[:help]
    say <<-EOT

    Usage:

     moka order_pages [group]

    Examples:
     moka order_pages
 launch a CLI utility to display and edit the order of pages in group 'root'

     moka order_pages mygroup
 launch a CLI utility to display and edit the order of pages in group 'mygroup'

    EOT
    exit
  end
end

#reorderObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/commands/order_pages.rb', line 65

def reorder
  input_array = @input.gsub(/[^a-z\s]/, "").split(/\s+/).map{|i| PageOrderGiver.letters_to_counter(i)}.uniq.delete_if{|i| i < 0 or i > @group.pages.size - 1}
  if input_array.size <= 0
    exit
  end
  counter = 0
  counter_two = 0
  @group.pages.order_by(:order).each do |page|
    unless input_array.index(counter).nil?
      @manifest["site"][@group.name][page.name]["order"] = input_array.index(counter) + 1
    else
      @manifest["site"][@group.name][page.name]["order"] = input_array.size + counter_two + 1
      counter_two += 1
    end
    counter += 1
  end
end

#verify_group_existenceObject



45
46
47
48
49
50
51
# File 'lib/commands/order_pages.rb', line 45

def verify_group_existence
  @group = @site.find_group(group_name)
  if @group.nil?
    say_status "ERROR:", "cannot find group '#{group_name}'", :red
    exit
  end
end