Class: Rooq::Table

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rooq/table.rb

Defined Under Namespace

Classes: TableBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ void

Parameters:

  • name (Symbol)
  • block (T.proc.params(builder: TableBuilder).void, nil)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rooq/table.rb', line 17

def initialize(name, &block)
  @name = name
  @fields = T.let({}, T::Hash[Symbol, Field])
  @field_accessors = T.let({}, T::Hash[Symbol, Field])

  if block_given?
    builder = TableBuilder.new(self)
    block.call(builder)
  end

  @fields.freeze
  freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Field

Parameters:

  • method_name (Symbol)
  • args (T.untyped)

Returns:



37
38
39
40
41
42
43
44
45
46
# File 'lib/rooq/table.rb', line 37

def method_missing(method_name, *args)
  field_name = method_name.to_s.downcase.to_sym

  if @fields.key?(field_name)
    T.must(@fields[field_name])
  else
    available = @fields.keys.join(", ")
    raise ValidationError, "Unknown field '#{field_name}' on table '#{@name}'. Available fields: #{available}"
  end
end

Instance Attribute Details

#fieldsHash{Symbol => Field} (readonly)

Returns:

  • (Hash{Symbol => Field})


14
15
16
# File 'lib/rooq/table.rb', line 14

def fields
  @fields
end

#nameSymbol (readonly)

Returns:

  • (Symbol)


11
12
13
# File 'lib/rooq/table.rb', line 11

def name
  @name
end

Instance Method Details

#asteriskArray<Field>

Returns:



32
33
34
# File 'lib/rooq/table.rb', line 32

def asterisk
  @fields.values
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Parameters:

  • method_name (Symbol)
  • include_private (Boolean) (defaults to: false)

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/rooq/table.rb', line 49

def respond_to_missing?(method_name, include_private = false)
  field_name = method_name.to_s.downcase.to_sym
  @fields.key?(field_name) || super
end