Class: Super::Schema::Guesser

Inherits:
Object
  • Object
show all
Defined in:
lib/super/schema/guesser.rb

Instance Method Summary collapse

Constructor Details

#initialize(model:, fields:, type:) ⇒ Guesser

Returns a new instance of Guesser.



6
7
8
9
10
11
# File 'lib/super/schema/guesser.rb', line 6

def initialize(model:, fields:, type:)
  @model = model
  @fields = fields
  @type = type
  @rejects = []
end

Instance Method Details

#assign_type(&block) ⇒ Object



28
29
30
31
# File 'lib/super/schema/guesser.rb', line 28

def assign_type(&block)
  @assign_type = block
  self
end

#callObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/super/schema/guesser.rb', line 33

def call
  result = sorted_attribute_names
  result = @rejects.reduce(result) do |intermediary_result, rejection_proc|
    intermediary_result.reject(&rejection_proc)
  end

  result = result.first(@limit) if @limit && @limit != Float::INFINITY

  result.each do |name|
    @fields[name] = @assign_type.call(name)
  end
end

#ignore_foreign_keysObject



23
24
25
26
# File 'lib/super/schema/guesser.rb', line 23

def ignore_foreign_keys
  @rejects.push(-> (attribute_name) { is_foreign_key[attribute_name] })
  self
end

#limitObject



13
14
15
16
# File 'lib/super/schema/guesser.rb', line 13

def limit
  @limit = yield
  self
end

#reject(&block) ⇒ Object



18
19
20
21
# File 'lib/super/schema/guesser.rb', line 18

def reject(&block)
  @rejects.push(block)
  self
end