Class: Tableling::Field
- Inherits:
-
Object
- Object
- Tableling::Field
- Defined in:
- lib/tableling/field.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #extract(object) ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Field
constructor
TODO: extract active record functionality.
- #order(&block) ⇒ Object
- #value(&block) ⇒ Object
- #with_order(query, direction) ⇒ Object
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, = {}, &block @name = name.to_s @order_column = [:order].try :to_s @value_column = [:value].try :to_s @model = [:model] ([:modules] || []).each do |mod| extend mod end instance_eval &block if block end |
Instance Attribute Details
#name ⇒ Object (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 |