Module: MarkdownComposer::Take
- Defined in:
- lib/markdown_composer/take.rb
Class Method Summary collapse
- .alternate_indexes(count, group_size) ⇒ Object
- .apply(items, take, diagnostics: nil, path: nil, seed: 0) ⇒ Object
- .integer(value) ⇒ Object
- .middle_indexes(count, selected_count) ⇒ Object
- .parse(value) ⇒ Object
- .percent_indexes(count, percent, direction) ⇒ Object
- .resolve_position(position, count) ⇒ Object
- .validate(take) ⇒ Object
Class Method Details
.alternate_indexes(count, group_size) ⇒ Object
125 126 127 |
# File 'lib/markdown_composer/take.rb', line 125 def alternate_indexes(count, group_size) (0...count).select { |index| ((index / group_size) % 2).zero? } end |
.apply(items, take, diagnostics: nil, path: nil, seed: 0) ⇒ Object
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 |
# File 'lib/markdown_composer/take.rb', line 39 def apply(items, take, diagnostics: nil, path: nil, seed: 0) take = parse(take) if take.is_a?(String) || take.is_a?(Symbol) take ||= { "all" => true } errors = validate(take) errors.each { || diagnostics&.error("take.invalid", , path: path) } return [] if errors.any? return items.dup if take.empty? || take["all"] selected_indexes = [] count = items.length selected_indexes.concat((0...[take["first"].to_i, count].min).to_a) if take["first"] selected_indexes.concat(((count - take["last"].to_i)...count).to_a) if take["last"] selected_indexes.concat(Array(take["position"]).map { |position| resolve_position(position, count) }.compact) if take["position"] Array(take["ranges"]).each do |range| from = resolve_position(range["from"], count) to = resolve_position(range["to"], count) selected_indexes.concat((from..to).to_a) if from && to && from <= to end if take["skip"] selected_indexes = (take["skip"].to_i...count).to_a end if take["skip_last"] selected_indexes = (0...[count - take["skip_last"].to_i, 0].max).to_a end selected_indexes = (0...count).select { |index| (index + 1) % take["every"].to_i == 0 } if take["every"] selected_indexes = (0...count).select { |index| (index + 1).odd? } if take["odd"] selected_indexes = (0...count).select { |index| (index + 1).even? } if take["even"] selected_indexes = (0...count).to_a - Array(take["except"]).map { |position| resolve_position(position, count) }.compact if take["except"] selected_indexes = percent_indexes(count, take["top_percent"].to_i, :top) if take["top_percent"] selected_indexes = percent_indexes(count, take["bottom_percent"].to_i, :bottom) if take["bottom_percent"] selected_indexes = middle_indexes(count, take["middle"].to_i) if take["middle"] selected_indexes = middle_indexes(count, (count * take["middle_percent"].to_i / 100.0).ceil) if take["middle_percent"] selected_indexes = alternate_indexes(count, take["alternate"].to_i) if take["alternate"] selected_indexes = (0...count).to_a.sample(take["random"].to_i, random: Random.new(seed)).sort if take["random"] selected_indexes.uniq.sort.map { |index| items[index] }.compact end |
.integer(value) ⇒ Object
102 103 104 |
# File 'lib/markdown_composer/take.rb', line 102 def integer(value) value.to_s == "last" ? -1 : value.to_i end |
.middle_indexes(count, selected_count) ⇒ Object
119 120 121 122 123 |
# File 'lib/markdown_composer/take.rb', line 119 def middle_indexes(count, selected_count) selected_count = [ selected_count, count ].min start = [(count - selected_count) / 2, 0].max (start...(start + selected_count)).to_a end |
.parse(value) ⇒ Object
7 8 9 10 11 12 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 |
# File 'lib/markdown_composer/take.rb', line 7 def parse(value) text = value.to_s.strip text = text[1..-2] if text.start_with?("[") && text.end_with?("]") return { "all" => true } if text.empty? || text == "all" clauses = text.split(";").map(&:strip) clauses.each_with_object({}) do |clause, hash| case clause when /\Aodd\z/ hash["odd"] = true when /\Aeven\z/ hash["even"] = true when /\A(\d+|-?\d+)\.\.(last|-?\d+|\d+)\z/ (hash["ranges"] ||= []) << { "from" => integer(Regexp.last_match(1)), "to" => integer(Regexp.last_match(2)) } when /\A([^:]+):(.+)\z/ key = Regexp.last_match(1).strip raw = Regexp.last_match(2).strip case key when "position", "except" hash[key] = raw.split(",").map { |part| integer(part.strip) } when "range" from, to = raw.split("..", 2) (hash["ranges"] ||= []) << { "from" => integer(from), "to" => integer(to) } else hash[key] = integer(raw) end else hash[clause] = true end end end |
.percent_indexes(count, percent, direction) ⇒ Object
114 115 116 117 |
# File 'lib/markdown_composer/take.rb', line 114 def percent_indexes(count, percent, direction) selected_count = (count * percent / 100.0).ceil direction == :top ? (0...selected_count).to_a : ((count - selected_count)...count).to_a end |
.resolve_position(position, count) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/markdown_composer/take.rb', line 106 def resolve_position(position, count) value = position.to_i return nil if value.zero? index = value.positive? ? value - 1 : count + value index if index >= 0 && index < count end |
.validate(take) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/markdown_composer/take.rb', line 79 def validate(take) errors = [] take.each do |key, value| unless Registries.default.take.key?(key.to_sym) errors << "Unknown take operator #{key.inspect}" next end case key when "first", "last", "skip", "skip_last", "every", "top_percent", "bottom_percent", "middle", "middle_percent", "alternate", "random" errors << "#{key} must be positive" if value.to_i <= 0 when "position", "except" Array(value).each { |position| errors << "#{key} cannot include 0" if position.to_i.zero? } when "ranges" Array(value).each do |range| errors << "range is missing from/to" unless range.key?("from") && range.key?("to") errors << "range cannot include 0" if range["from"].to_i.zero? || range["to"].to_i.zero? end end end errors end |