Class: Roro::Configurators::Reflector

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/roro/configurators/reflector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#adventure_name, #all_inflections, #build_paths, #children, #file_extension, #file_name, #read_yaml, #sanitize, #sentence_from, #sort_hash_deeply, #stack_is_adventure?, #stack_is_dotfile?, #stack_is_empty?, #stack_is_file?, #stack_is_ignored?, #stack_is_inflection?, #stack_is_inflection_stub?, #stack_is_itinerary_path?, #stack_is_nil?, #stack_is_node?, #stack_is_parent?, #stack_is_stack?, #stack_is_story?, #stack_is_story_path?, #stack_is_storyfile?, #stack_is_templates?, #stack_name, #stack_not_present?, #stack_parent, #stack_parent_path, #stack_stories, #stack_type, #stack_unpermitted?, #story_is_empty?, #story_name, #story_paths

Constructor Details

#initialize(stack = nil) ⇒ Reflector

Returns a new instance of Reflector.



12
13
14
# File 'lib/roro/configurators/reflector.rb', line 12

def initialize(stack = nil)
  @stack = stack || Roro::CLI.stacks
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



10
11
12
# File 'lib/roro/configurators/reflector.rb', line 10

def stack
  @stack
end

Instance Method Details

#adventure_canonical_slug(itinerary) ⇒ Object



214
215
216
217
218
219
220
221
222
# File 'lib/roro/configurators/reflector.rb', line 214

def adventure_canonical_slug(itinerary)
  array = []
  itinerary.each do |i|
    inflections = %w[adventures databases frameworks stories versions]
    keyword = i.split('/').last(3) - inflections
    array << keyword.join('_')
  end
  array.join('-')
end

#adventure_slug(itinerary) ⇒ Object



199
200
201
202
203
204
205
206
# File 'lib/roro/configurators/reflector.rb', line 199

def adventure_slug(itinerary)
  array = []
  itinerary.each do |i|
    keyword = i.split('/').last(3) - @implicit_tags
    array << keyword.join('_')
  end
  array.join('-')
end

#adventure_structureObject



102
103
104
105
106
107
# File 'lib/roro/configurators/reflector.rb', line 102

def adventure_structure
  {
    human: adventure_structure_human,
    choices: adventure_structure_choices
  }
end

#adventure_structure_choicesObject



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/roro/configurators/reflector.rb', line 119

def adventure_structure_choices
  {}.tap do |h|
    cases.each do |array|
      hash = {}
      array.reverse.each do |c|
        hash = { c => hash }
      end
      h.deep_merge(hash)
    end
  end
end

#adventure_structure_humanObject



109
110
111
112
113
114
115
116
117
# File 'lib/roro/configurators/reflector.rb', line 109

def adventure_structure_human
  {}.tap do |h|
    itineraries.each do |array|
      hash = {}
      array.reverse.each { |i| hash = { i.split(': ').last => hash } }
      h.deep_merge(hash)
    end
  end
end

#adventure_title(itinerary) ⇒ Object



208
209
210
211
212
# File 'lib/roro/configurators/reflector.rb', line 208

def adventure_title(itinerary)
  itinerary.map do |item|
    item.split(': ').last
  end.join(' ')
end

#append_tech_to_version(tag, path) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/roro/configurators/reflector.rb', line 178

def append_tech_to_version(tag, path)
  if stack_parent(path).eql?('versions')
    base = stack_name(stack_parent_path(stack_parent_path(path)))
    "#{base}#{tag.gsub(tag.chr, ':').gsub('_', '.')}"
  else
    tag
  end
end

#cases(stack = @stack, sibs = [], kase = [], kases = []) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/roro/configurators/reflector.rb', line 43

def cases(stack = @stack, sibs = [], kase = [], kases = [])
  case stack_type(stack)
  when :inflection_stub
    children(stack).each { |c| cases(c, sibs, kase, kases) }
  when :inflection
    children(stack).each_with_index do |c, i|
      cases(c, sibs.dup, kase.dup << i += 1, kases)
    end
  when :stack
    inflections = children(stack).select do |c|
      %i[inflection inflection_stub].include?(stack_type(c))
    end

    cases(inflections.shift, (sibs + inflections), kase, kases)
  when :story
    sibs.empty? ? kases << kase : cases(sibs.shift, sibs, kase, kases)
  end
  kases
end

#itineraries(stack = @stack, siblings = [], kase = [], kases = []) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/roro/configurators/reflector.rb', line 76

def itineraries(stack = @stack, siblings = [], kase = [], kases = [])
  case stack_type(stack)
  when :inflection_stub
    children(stack).each { |c| itineraries(c, siblings, kase, kases) }
  when :inflection
    children(stack).each do |c|
      dup = (kase.dup << "#{stack_name(stack)}: #{stack_name(c)}")
      itineraries(c, siblings.dup, dup, kases)
    end
  when :stack
    inflections = children(stack).select do |c|
      %i[inflection inflection_stub]
        .include?(stack_type(c))
    end

    itineraries(inflections.shift, (siblings + inflections), kase, kases)
  when :story
    if siblings.empty?
      kases << kase
    else
      itineraries(siblings.shift, siblings, kase, kases)
    end
  end
  kases
end

#itinerary_index(itinerary, stack) ⇒ Object



72
73
74
# File 'lib/roro/configurators/reflector.rb', line 72

def itinerary_index(itinerary, stack)
  stack_itineraries(stack).index(itinerary)
end

#log_to_mise(name, content) ⇒ Object



16
17
18
19
# File 'lib/roro/configurators/reflector.rb', line 16

def log_to_mise(name, content)
  path = "#{Dir.pwd}/mise/logs/#{name}.yml"
  File.open(path, 'w') { |f| f.write(content.to_yaml) }
end

#metadataObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/roro/configurators/reflector.rb', line 131

def 
  @implicit_tags = %w[git okonomi adventures databases frameworks stories versions]

  data = {}
  data[:adventures] = {}
  itineraries.each_with_index do |itinerary, index|
    data[:adventures][index] = {
      title: adventure_title(itinerary),
      slug: adventure_slug(itinerary),
      canonical_slug: adventure_canonical_slug(itinerary),
      tech_tags: tech_tags(itinerary),
      versioned_tech_tags: versioned_tech_tags(itinerary),
      unversioned_tech_tags: unversioned_tech_tags(itinerary)
    }
  end
  data
end

#reflection(stack = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/roro/configurators/reflector.rb', line 21

def reflection(stack = nil)
  stack ||= @stack
  reflection = {
    inflections: [],
    stacks: {},
    stories: [],
    picks: []
  }
  children(stack).each_with_index do |c, index|
    if %i[inflection inflection_stub].include?(stack_type(c))
      reflection[:inflections] << { stack_name(c).to_sym => reflection(c) }
    elsif [:stack].include?(stack_type(c))
      reflection[:stacks][index + 1] = reflection c
    elsif [:story].include?(stack_type(c))
      reflection[:picks] << index + 1
      story = c.split("#{Roro::CLI.stacks}/").last
      reflection[:stories] << story
    end
  end
  reflection
end

#stack_itineraries(stack) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/roro/configurators/reflector.rb', line 63

def stack_itineraries(stack)
  itineraries.select do |itinerary|
    parent = stack.split("#{Roro::CLI.stacks}/").last
    itinerary.any? do |i|
      i.match? parent
    end
  end
end

#tech_tags(kase = [], stack = @stack, tags = [], sibs = []) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/roro/configurators/reflector.rb', line 149

def tech_tags(kase = [], stack = @stack, tags = [], sibs = [])
  children = children(stack)
  case stack_type(stack)
  when :inflection_stub
    children(stack).each { |c| tech_tags(kase, c, tags, sibs) }
  when :inflection
    choice = children[kase.shift - 1]
    tech_tags(kase, choice, tags, sibs)
  when :stack
    inflections = children(stack).select do |c|
      %i[inflection inflection_stub]
        .include?(stack_type(c))
    end
    stack_stories(stack).each do |child|
      tech_tags(kase, child, tags, sibs) if stack_is_storyfile?(child)
    end
    tech_tags(kase, inflections.shift, tags, (sibs + inflections))
  when :storyfile
    tags << stack_name(stack).split('.').first
  when :story
    children(stack).each do |child|
      tech_tags(kase, child, tags, sibs)
    end
  end
  tech_tags(kase, sibs.shift, tags, sibs) unless sibs.empty?
  ignored = %w[_builder databases]
  tags.reject { |tag| ignored.include?(tag) }
end

#unversioned_tech_tags(itinerary) ⇒ Object



187
188
189
190
191
# File 'lib/roro/configurators/reflector.rb', line 187

def unversioned_tech_tags(itinerary)
  tech_tags(itinerary).reject do |t|
    t.match?(':')
  end
end

#versioned_tech_tags(itinerary) ⇒ Object



193
194
195
196
197
# File 'lib/roro/configurators/reflector.rb', line 193

def versioned_tech_tags(itinerary)
  tech_tags(itinerary).select do |t|
    t.match?(':')
  end
end