Class: Tableling::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/tableling/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Field

TODO: extract active record functionality



8
9
10
11
12
13
14
15
16
17
# File 'lib/tableling/field.rb', line 8

def initialize name, options = {}, &block
  @name = name.to_s
  @order_column = options[:order].try :to_s
  @value_column = options[:value].try :to_s
  @model = options[:model]
  (options[:modules] || []).each do |mod|
    extend mod
  end
  instance_eval &block if block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/tableling/field.rb', line 5

def name
  @name
end

Instance Method Details

#extract(object) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/tableling/field.rb', line 35

def extract object
  if @value_block
    @value_block.call object
  else
    object.send(@value_column || @name)
  end
end

#order(&block) ⇒ Object



19
20
21
# File 'lib/tableling/field.rb', line 19

def order &block
  @order_block = block
end

#value(&block) ⇒ Object



23
24
25
# File 'lib/tableling/field.rb', line 23

def value &block
  @value_block = block
end

#with_order(query, direction) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/tableling/field.rb', line 27

def with_order query, direction
  if @order_block
    @order_block.call query, direction
  else
    query.order "#{@model.table_name}.#{@order_column || @name} #{direction}"
  end
end