Module: Roro::Utilities

Instance Method Summary collapse

Instance Method Details

#adventure_name(location) ⇒ Object



212
213
214
# File 'lib/roro/common/utilities.rb', line 212

def adventure_name(location)
  "Adventure #{stack_parent(location)} #{location.split(Roro::CLI.stacks).last}" # .split('/')
end

#all_inflections(stack) ⇒ Object



128
129
130
# File 'lib/roro/common/utilities.rb', line 128

def all_inflections(stack)
  children(stack).select { |c| stack_is_inflection?(c) }
end

#build_paths(stack, story_paths = nil) ⇒ Object



160
161
162
163
164
165
# File 'lib/roro/common/utilities.rb', line 160

def build_paths(stack, story_paths = nil)
  story_paths ||= []
  story_paths << stack if stack_is_story_path?(stack)
  children(stack).each { |c| build_paths(c, story_paths) }
  story_paths
end

#children(stack) ⇒ Object



132
133
134
# File 'lib/roro/common/utilities.rb', line 132

def children(stack)
  Dir.glob("#{stack}/*")
end

#file_extension(file) ⇒ Object



196
197
198
# File 'lib/roro/common/utilities.rb', line 196

def file_extension(file)
  file.split('.').last
end

#file_name(story_file) ⇒ Object



192
193
194
# File 'lib/roro/common/utilities.rb', line 192

def file_name(story_file)
  story_file.split('/').last.split('.').first
end

#read_yaml(yaml_file) ⇒ Object



208
209
210
# File 'lib/roro/common/utilities.rb', line 208

def read_yaml(yaml_file)
  JSON.parse(YAML.load_file(yaml_file).to_json, symbolize_names: true)
end

#sanitize(hash) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/roro/common/utilities.rb', line 173

def sanitize(hash)
  (hash ||= {}).transform_keys!(&:to_sym).each do |key, value|
    case value
    when Array
      value.each { |vs| sanitize(vs) }
    when Hash
      sanitize(value)
    when true
      hash[key] = true
    when String || Symbol
      hash[key] = value.to_sym
    end
  end
end

#sentence_from(array) ⇒ Object



204
205
206
# File 'lib/roro/common/utilities.rb', line 204

def sentence_from(array)
  array[1] ? "#{array[0..-2].join(', ')} and #{array[-1]}" : array[0]
end

#sort_hash_deeply(unsorted) ⇒ Object



167
168
169
170
171
# File 'lib/roro/common/utilities.rb', line 167

def sort_hash_deeply(unsorted)
  sorted ||= {}
  unsorted.sort.to_h.map { |k, v| sorted[k] = sort_hash_deeply(v) }
  sorted
end

#stack_is_adventure?(stack) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
# File 'lib/roro/common/utilities.rb', line 149

def stack_is_adventure?(stack)
  !stack_type(stack).eql?(:templates) &&
    stack_type(stack_parent_path(stack)).eql?(:inflection) &&
    %i[stack story].include?(stack_type(stack))
end

#stack_is_dotfile?(stack) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/roro/common/utilities.rb', line 44

def stack_is_dotfile?(stack)
  stack_is_file?(stack) &&
    %w[keep gitkeep].include?(file_extension(stack))
end

#stack_is_empty?(stack) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
158
# File 'lib/roro/common/utilities.rb', line 155

def stack_is_empty?(stack)
  !stack_is_file?(stack) &&
    Dir.glob("#{stack}/**").empty?
end

#stack_is_file?(stack) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/roro/common/utilities.rb', line 40

def stack_is_file?(stack)
  File.file?(stack)
end

#stack_is_ignored?(stack) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
# File 'lib/roro/common/utilities.rb', line 123

def stack_is_ignored?(stack)
  ignored = %w[test_dummy story_test test]
  ignored.include?(stack.split('/').last)
end

#stack_is_inflection?(stack) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
# File 'lib/roro/common/utilities.rb', line 75

def stack_is_inflection?(stack)
  stack_stories(stack).empty? &&
    !File.file?(stack) &&
    !stack_is_ignored?(stack) &&
    !stack_is_templates?(stack)
end

#stack_is_inflection_stub?(stack) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/roro/common/utilities.rb', line 60

def stack_is_inflection_stub?(stack)
  return unless stack_is_inflection?(stack)

  choices = children(stack).select do |c|
    stack_is_inflection?(stack) &&
      !stack_is_stack?(stack) &&
      !stack_is_story?(stack) &&
      !stack_is_templates?(stack) &&
      !stack_is_ignored?(stack) &&
      !stack_is_storyfile?(stack) &&
      stack_is_stack?(c) || stack_is_story?(c)
  end
  choices.size < 2
end

#stack_is_itinerary_path?(stack) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/roro/common/utilities.rb', line 101

def stack_is_itinerary_path?(stack)
  !stack_is_parent?(stack) &&
    !stack_is_templates?(stack) &&
    stack_is_node?(stack)
end

#stack_is_nil?(stack) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/roro/common/utilities.rb', line 91

def stack_is_nil?(stack)
  !File.exist?(stack)
end

#stack_is_node?(stack) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/roro/common/utilities.rb', line 136

def stack_is_node?(stack)
  children(stack).any? { |w| w.include?('.yml') } && !stack_is_templates?(stack)
end

#stack_is_parent?(stack) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/roro/common/utilities.rb', line 107

def stack_is_parent?(stack)
  children(stack).any? { |c| stack_is_inflection?(c) }
end

#stack_is_stack?(stack) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/roro/common/utilities.rb', line 30

def stack_is_stack?(stack)
  children(stack).any? do |c|
    stack_is_inflection?(c) || stack_is_story?(c)
  end
end

#stack_is_story?(stack) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/roro/common/utilities.rb', line 36

def stack_is_story?(stack)
  children(stack).any? { |c| story_name(stack).eql?(file_name(c)) }
end

#stack_is_story_path?(stack) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/roro/common/utilities.rb', line 95

def stack_is_story_path?(stack)
  !stack_is_parent?(stack) &&
    !stack_is_templates?(stack) &&
    stack_is_node?(stack)
end

#stack_is_storyfile?(stack) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/roro/common/utilities.rb', line 49

def stack_is_storyfile?(stack)
  stack_is_file?(stack) &&
    %w[yml yaml].include?(file_extension(stack))
end

#stack_is_templates?(stack) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/roro/common/utilities.rb', line 119

def stack_is_templates?(stack)
  stack.split('/').last.match?('templates')
end

#stack_name(stack) ⇒ Object



188
189
190
# File 'lib/roro/common/utilities.rb', line 188

def stack_name(stack)
  stack.split('/').last
end

#stack_not_present?(stack) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/roro/common/utilities.rb', line 87

def stack_not_present?(stack)
  !File.exist?(stack)
end

#stack_parent(stack) ⇒ Object



140
141
142
143
# File 'lib/roro/common/utilities.rb', line 140

def stack_parent(stack)
  tree = stack.split('/')
  tree[-2]
end

#stack_parent_path(stack) ⇒ Object



145
146
147
# File 'lib/roro/common/utilities.rb', line 145

def stack_parent_path(stack)
  stack.split("/#{stack_name(stack)}").first
end

#stack_stories(stack) ⇒ Object



115
116
117
# File 'lib/roro/common/utilities.rb', line 115

def stack_stories(stack)
  children(stack).select { |c| stack_is_storyfile?(c) }
end

#stack_type(stack) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/roro/common/utilities.rb', line 8

def stack_type(stack)
  if stack_not_present?(stack)
    :nonexistent
  elsif stack_is_ignored?(stack)
    :ignored
  elsif stack_is_dotfile?(stack)
    :dotfile
  elsif stack_is_storyfile?(stack)
    :storyfile
  elsif stack_is_templates?(stack)
    :templates
  elsif stack_is_inflection_stub?(stack)
    :inflection_stub
  elsif stack_is_inflection?(stack)
    :inflection
  elsif stack_is_stack?(stack)
    :stack
  elsif stack_is_story?(stack)
    :story
  end
end

#stack_unpermitted?(stack) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/roro/common/utilities.rb', line 54

def stack_unpermitted?(stack)
  stack_is_file?(stack) &&
    !stack_is_storyfile?(stack) &&
    !stack_is_dotfile?(stack)
end

#story_is_empty?(story) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/roro/common/utilities.rb', line 82

def story_is_empty?(story)
  stack_is_storyfile?(story) &&
    !read_yaml(story)
end

#story_name(stack) ⇒ Object



200
201
202
# File 'lib/roro/common/utilities.rb', line 200

def story_name(stack)
  stack.split('/').last
end

#story_paths(stack) ⇒ Object



111
112
113
# File 'lib/roro/common/utilities.rb', line 111

def story_paths(stack)
  children(stack).select { |c| stack_is_story_path?(c) }
end