Class: SampleModels::AttributeSequence

Inherits:
Object
  • Object
show all
Defined in:
lib/sample_models/attribute_sequence.rb

Defined Under Namespace

Classes: Builder

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, column, validation, input) ⇒ AttributeSequence

Returns a new instance of AttributeSequence.



7
8
9
10
# File 'lib/sample_models/attribute_sequence.rb', line 7

def initialize(model, column, validation, input)
  @model, @column, @validation, @input = model, column, validation, input
  @number = 0
end

Class Method Details

.build(*args) ⇒ Object



3
4
5
# File 'lib/sample_models/attribute_sequence.rb', line 3

def self.build(*args)
  Builder.new(*args).run
end

Instance Method Details

#belongs_to_associationObject



12
13
14
15
16
# File 'lib/sample_models/attribute_sequence.rb', line 12

def belongs_to_association
  @model.belongs_to_associations.detect { |a|
    a.foreign_key == @column.name
  }
end

#nextObject



18
19
20
21
22
# File 'lib/sample_models/attribute_sequence.rb', line 18

def next
  @number += 1
  @input.next if @input
  value
end

#valueObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sample_models/attribute_sequence.rb', line 24

def value
  case @column.type
    when :string, :text
      "#{@column.name} #{@number}"
    when :integer
      belongs_to_association ? belongs_to_assoc_foreign_key_value : @number
    when :datetime
      Time.now.utc - @number.minutes
    when :date
      Date.today - @number
    when :float
      @number.to_f
    end
end