Class: SequenceGenerator

Inherits:
Object
  • Object
show all
Defined in:
frontend/app/models/sequence_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to, prefix, suffix, limit) ⇒ SequenceGenerator

Returns a new instance of SequenceGenerator.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'frontend/app/models/sequence_generator.rb', line 3

def initialize(from, to, prefix, suffix, limit)
  @errors = []

  @from = from
  @to = to
  @prefix = prefix
  @suffix = suffix

  # limit the range to 1000 entries, unless the number of rows is provided
  @limit = (limit || 1000).to_i

  generate_range
end

Class Method Details

.from_params(params) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'frontend/app/models/sequence_generator.rb', line 33

def self.from_params(params)
  errors = []
  errors.push(I18n.t("rde.fill_column.sequence_from_required")) if params[:from].blank?
  errors.push(I18n.t("rde.fill_column.sequence_to_required")) if params[:to].blank?

  return {"errors" => errors} if errors.length > 0

  self.new(params[:from], params[:to], params[:prefix], params[:suffix], params[:limit]).generate_range
end

Instance Method Details

#generate_rangeObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'frontend/app/models/sequence_generator.rb', line 18

def generate_range
  range = (@from..@to)
  values = range.take(@limit).map {|i| "#{@prefix}#{i}#{@suffix}"}

  {
    "size" => values.length,
    "limit" => @limit,
    "values" => values,
    "summary" => @limit ?
      I18n.t("rde.fill_column.sequence_summary_with_maxsize", :limit => @limit, :count => values.length) :
      I18n.t("rde.fill_column.sequence_summary", :count => values.length)
  }
end