Class: DataView::DataView

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/data_view.rb

Overview

DataView is intended to reduce coupling between the Controller and View layers in a MVC application. The Controller can send a DataView to the View layer. The View layer does not need to have access to the Model.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, *columns) ⇒ DataView

The column order in the each method is the same as the array order. Each Struct contains the following keys:

:field

(Required) The database field should be a Symbol or String of the field that is called.

:title

The title of the column. If one is not given, the field value is used.

:value

The value of the particular cell. If a value is not given, the value of the database field in the model is used. If a Symbol or String is given and the model contains a field with that name, then the model value is returned. If the Symbol or string is not a field in the model, the value parameter is returned. If a Proc is given, the return value of the Proc is used. The Proc must accept a Struct of parameters and return the transformed value. The Struct contains the following keys:

  • :model - The Model of the DataView

  • :row_index - The Index of the current row.

  • :column_index - The Index of the current column.

  • :column - The column of the current value.

:value_transforms

An Array of Procs that act as event sinks to transform the value into the desired view. The transforms occur in the order of the Array. Each Proc must accept a Struct of parameters and return the transformed value. The Struct contains the following keys:

  • :value - The current value.

  • :model - The Model of the DataView

  • :row_index - The Index of the current row.

  • :column_index - The Index of the current column.

  • :column - The column of the current value.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/data_view.rb', line 62

def initialize(model, *columns)
# A new array object is created here.
  columns = columns.flatten
  @model = if model.respond_to?(:each_index) then model else [model] end

  # @columns is an ordered array of the columns that will be shown
  @columns = columns
class << @columns
	def find_by_field(field)
		self.find {|c| c.field == field}
	end
end
end

Class Method Details

.create_column(field = nil, title = nil, value = nil, *value_transforms) ⇒ Object

Creates a column definition struct.



9
10
11
12
13
14
15
16
17
18
# File 'lib/data_view.rb', line 9

def create_column(field=nil, title=nil, value=nil, *value_transforms)
  @@Column_Struct_Class = Struct.new(
    :field,
    :title,
    :value,
    :value_transforms
  ) unless defined?(@@Column_Struct_Class)
  
  @@Column_Struct_Class.new(field, title, value, value_transforms.flatten)
end

Instance Method Details

#[](index) ⇒ Object

Gets a particular row of the view.



92
93
94
95
# File 'lib/data_view.rb', line 92

def [](index)
  v_r_s = create_view_row_struct
  get_view_row(index, v_r_s)
end

#columnsObject

An array of the columns in the DataView. columns also has the find_by_field(field) method to quickly find a column by its field.



77
78
79
# File 'lib/data_view.rb', line 77

def columns
	@columns
end

#create_column(field = nil, title = nil, value = nil, *value_transforms) ⇒ Object

Creates a column definition struct.



87
88
89
# File 'lib/data_view.rb', line 87

def create_column(field=nil, title=nil, value=nil, *value_transforms)
  DataView.create_column(field, title, value, *value_transforms)
end

#eachObject

Iterates through each row of the view.



103
104
105
106
107
108
# File 'lib/data_view.rb', line 103

def each
  v_r_s = create_view_row_struct
  @model.each_index do |row_index|
    yield(get_view_row(row_index, v_r_s))
  end
end

#lengthObject

The number of rows in the result.



98
99
100
# File 'lib/data_view.rb', line 98

def length
  @model.length
end

#rowsObject

An array of the rows in the DataView.



82
83
84
# File 'lib/data_view.rb', line 82

def rows
	@rows
end