Class: Ro::Script::Builder

Inherits:
Object show all
Defined in:
lib/ro/script/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script:) ⇒ Builder

Returns a new instance of Builder.



9
10
11
# File 'lib/ro/script/builder.rb', line 9

def initialize(script:)
  @script = script
end

Class Method Details

.run!Object



4
5
6
# File 'lib/ro/script/builder.rb', line 4

def run!(...)
  new(...).run!
end

Instance Method Details

#data_for(value) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ro/script/builder.rb', line 186

def data_for(value)
  {}.tap do |hash|
    case value
    when Array
      value.each { |node| hash.update(data_for(node)) }
    when Ro::Node
      hash[value.identifier] = value.to_hash
    else
      raise ArgumentError, "#{value.class}(#{value.inspect})"
    end
  end
end

#filtered_nodes_for(list) ⇒ Object



207
208
209
# File 'lib/ro/script/builder.rb', line 207

def filtered_nodes_for(list)
  list.select { |node| select_node?(node) }
end

#global_metaObject



150
151
152
# File 'lib/ro/script/builder.rb', line 150

def global_meta
  { url: @url }
end

#meta_for(meta) ⇒ Object



154
155
156
# File 'lib/ro/script/builder.rb', line 154

def meta_for(meta)
  global_meta.merge(meta)
end

#nodes_for(list) ⇒ Object



199
200
201
# File 'lib/ro/script/builder.rb', line 199

def nodes_for(list)
  filtered_nodes_for(sorted_nodes_for(list))
end

#page_for(paginator) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/ro/script/builder.rb', line 176

def page_for(paginator)
  paginator.except(:data).merge(
    {
      curr: paginator[:index],
      prev: ((paginator[:index] - 1) >= paginator[:first] ? (paginator[:index] - 1) : nil),
      next: ((paginator[:index] + 1) <= paginator[:last] ? (paginator[:index] + 1) : nil)
    }
  )
end

#paginator_for(count, size) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/ro/script/builder.rb', line 165

def paginator_for(count, size)
  {
    count:,
    size:,
    first: 0,
    last: count - 1,
    index: 0,
    data: []
  }
end

#rel_for(list, index) ⇒ Object



158
159
160
161
162
163
# File 'lib/ro/script/builder.rb', line 158

def rel_for(list, index)
  { curr: list[index].identifier, prev: nil, next: nil }.tap do |rel|
    rel[:prev] = list[index - 1].identifier if (index - 1) >= 0
    rel[:next] = list[index + 1].identifier if (index + 1) <= (list.size - 1)
  end
end

#run!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ro/script/builder.rb', line 13

def run!
  @url = Ro.config.url
  @root = Ro.config.root
  @directory = Ro.config.build
  @page_size = Ro.config.page_size
  @collections = Ro.root.collections.sort_by(&:name)

  @build = {}
  @nodes = []

  @started_at = Time.now.to_f
  @finished_at = nil
  @elapsed = nil

  # for all node collections...
  #
  @collections.each do |collection|
    type = collection.type
    name = collection.name
    nodes = nodes_for(collection)

    count = nodes.size
    last = count - 1
    page_count = (count / @page_size.to_f).ceil
    paginator = paginator_for(page_count, @page_size)

    nodes.each_with_index do |node, index|
      @nodes << node
      id = node.id

      # enhance with links to next/prev
      #
      rel = rel_for(nodes, index)
      node.attributes[:_meta][:rel] = rel

      # node data
      #
      data = data_for(node)
      _meta = meta_for(type:, id:)
      path = "#{type}/#{id}/index.json"
      @build[path] = { data:, _meta: }

      # paginate data
      #
      paginator[:data].push(node)
      generate_page = ((paginator[:data].size == paginator[:size]) || (index == last))
      next unless generate_page

      page = page_for(paginator)
      data = data_for(paginator[:data])
      _meta = meta_for(type:, page:)
      pageno = page[:index] + 1
      path = "#{type}/index-#{pageno}.json" # eg. posts/page-$pageno.json
      @build[path] = { data:, _meta: }

      paginator[:data].clear
      paginator[:index] += 1
    end

    data = data_for(nodes)
    _meta = meta_for(type:)
    path = "#{type}/index.json" # eg. posts/index.json
    @build[path] = { data:, _meta: }
  end

  # annnnd for all nodes...
  #
  count = @nodes.size
  last = count - 1
  page_count = (count / @page_size.to_f).ceil
  paginator = paginator_for(page_count, @page_size)

  @nodes.each_with_index do |node, index|
    # enhance with links to next/prev
    #
    rel = rel_for(@nodes, index)
    node.attributes[:_meta][:rel] = rel

    paginator[:data].push(node)
    should_generate_page = ((paginator[:data].size == paginator[:size]) || (index == last))
    next unless should_generate_page

    types = paginator[:data].map(&:type).sort.uniq
    page = page_for(paginator)
    data = data_for(paginator[:data])
    _meta = meta_for(types:, page:)

    pageno = page[:index] + 1
    path = "index-#{pageno}.json" # eg. page-$pageno.json
    @build[path] = { data:, _meta: }

    paginator[:data].clear
    paginator[:index] += 1
  end

  # index.json
  #
  types = @collections.map(&:type).sort
  data = data_for(@nodes)
  _meta = meta_for(types:)
  path = 'index.json'
  @build[path] = { data:, _meta: }

  # now output the build
  #
  @script.say("ro.build: #{@root} -> #{@directory}", color: :magenta)

  FileUtils.rm_rf(@directory)
  FileUtils.mkdir_p(File.dirname(@directory))
  #FileUtils.cp_r(@root, @directory)

  Ro::Path.for(@directory).glob('**/**') do |entry|
    next unless test('f', entry)

    @script.say("ro.build: #{entry}", color: :blue)
  end

  @build.each do |subpath, data|
    path = Ro::Path.for(@directory, subpath)
    @script.say("ro.build: #{path}", color: :yellow)
    Ro.error! "#{path} would be clobbered" if path.exist?

    if data.is_a?(String)
      path.binwrite(data)
    else
      path.binwrite(JSON.pretty_generate(data))
    end
  end

  # show stats
  #
  @finished_at = Time.now.to_f
  @elapsed = (@finished_at - @started_at).round(2)

  @script.say("ro.build: #{@root} -> #{@directory} in #{@elapsed}s", color: :green)
end

#select_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


211
212
213
214
215
216
217
218
219
# File 'lib/ro/script/builder.rb', line 211

def select_node?(node)
  now = Time.now.utc

  hidden = Ro.cast(:bool, node.attributes.fetch(:hidden) { false })
  published = Ro.cast(:bool, node.attributes.fetch(:published) { true })
  published_at = Ro.cast(:time, node.attributes.fetch(:published_at) { now.iso8601 })

  ((not hidden) && (published) && (published_at <= now))
end

#sorted_nodes_for(list) ⇒ Object



203
204
205
# File 'lib/ro/script/builder.rb', line 203

def sorted_nodes_for(list)
  list.to_a.sort
end