Module: RailsData::Record

Defined in:
lib/rails_data/record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columnsObject (readonly)

extend RailsData::Record config do

object -> { Order.find order_id }
column :amount, header: 'My name', field: -> {}
column :email, header: 'Email', field: -> {}

end



9
10
11
# File 'lib/rails_data/record.rb', line 9

def columns
  @columns
end

#recordObject (readonly)

extend RailsData::Record config do

object -> { Order.find order_id }
column :amount, header: 'My name', field: -> {}
column :email, header: 'Email', field: -> {}

end



9
10
11
# File 'lib/rails_data/record.rb', line 9

def record
  @record
end

Instance Method Details

#column(name, field: nil, as: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rails_data/record.rb', line 23

def column(name, field: nil, as: nil)
  @columns ||= {}
  name = name.to_sym

  @columns[name] = {}

  if field.respond_to?(:call)
    @columns[name][:field] = field
  end

  if as
    @columns[name][:as] = as
  else
    @columns[name][:as] = 'string'
  end

  @columns
end

#config(*args, &block) ⇒ Object



11
12
13
# File 'lib/rails_data/record.rb', line 11

def config(*args, &block)
  block.call(*args) if block_given?
end

#object(object) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/rails_data/record.rb', line 15

def object(object)
  if object.respond_to?(:call)
    @record = object
  else
    raise 'The Record must be callable'
  end
end