Class: Filtrum::Field
- Inherits:
-
Object
- Object
- Filtrum::Field
- Defined in:
- lib/filtrum/field.rb
Instance Attribute Summary collapse
-
#alias ⇒ Object
Returns the value of attribute alias.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #as(name) ⇒ Object
- #extract(object) ⇒ Object
- #includes(&block) ⇒ Object
-
#initialize(name, view, options = {}, &block) ⇒ Field
constructor
A new instance of Field.
- #no_order ⇒ Object
- #order(&block) ⇒ Object
- #value(&block) ⇒ Object
- #with_includes(query) ⇒ Object
- #with_order(query, direction) ⇒ Object
- #working_name ⇒ Object
Constructor Details
#initialize(name, view, options = {}, &block) ⇒ Field
Returns a new instance of Field.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/filtrum/field.rb', line 6 def initialize name, view, = {}, &block @name, @view = name.to_s, view @alias = [:as].try :to_s @value_column = [:value].try :to_s @includes = [:includes] if [:order] == false @no_order = true elsif [:order] @order_column = [:order].to_s end instance_eval &block if block end |
Instance Attribute Details
#alias ⇒ Object
Returns the value of attribute alias.
4 5 6 |
# File 'lib/filtrum/field.rb', line 4 def alias @alias end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/filtrum/field.rb', line 4 def name @name end |
Instance Method Details
#as(name) ⇒ Object
26 27 28 |
# File 'lib/filtrum/field.rb', line 26 def as name @alias = name.to_s end |
#extract(object) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/filtrum/field.rb', line 65 def extract object if @value_block @value_block.call object else serialize object.send(@value_column || @name) end end |
#includes(&block) ⇒ Object
42 43 44 |
# File 'lib/filtrum/field.rb', line 42 def includes &block @includes_block = block end |
#no_order ⇒ Object
34 35 36 |
# File 'lib/filtrum/field.rb', line 34 def no_order @no_order = true end |
#order(&block) ⇒ Object
30 31 32 |
# File 'lib/filtrum/field.rb', line 30 def order &block @order_block = block end |
#value(&block) ⇒ Object
38 39 40 |
# File 'lib/filtrum/field.rb', line 38 def value &block @value_block = block end |
#with_includes(query) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/filtrum/field.rb', line 55 def with_includes query if @includes_block @includes_block.call query elsif @includes query.includes @includes else query end end |
#with_order(query, direction) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/filtrum/field.rb', line 46 def with_order query, direction return if @no_order if @order_block @order_block.call query, direction else query.order "#{model.table_name}.#{@order_column || @name} #{direction}" end end |
#working_name ⇒ Object
22 23 24 |
# File 'lib/filtrum/field.rb', line 22 def working_name @alias || @name end |