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.

Defined Under Namespace

Classes: Row

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, columns = []) ⇒ DataView

Initializes the DataView.

Model

The DataView supports a model that contains an array of hashes. The keys in these hashes much be symbols of the field names. This is based off of the ActiveRecord interface.

Columns

An Array of Hashes that contain data about the columns and how the columns will be handled. The column order in the each method is the same as the array order. Each Hash 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 Hash of parameters and return the transformed value. The Hash contains the following keys:

  • :sender - The DataView sending the call

  • :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 Hash of parameters and return the transformed value. The Hash contains the following keys:

  • :sender - The DataView sending the call

  • :row_index - The Index of the current row.

  • :column_index - The Index of the current column.

  • :value - The current value.

  • :column - The column of the current value.



64
65
66
67
68
69
70
# File 'lib/data_view.rb', line 64

def initialize(model, columns = [])
	raise 'columns cannot be nil' if columns == nil
	@model = model.respond_to?(:each_index) ? model : [model]

	# @columns is an ordered array of the columns that will be shown
	@columns = columns
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



38
39
40
# File 'lib/data_view.rb', line 38

def columns
  @columns
end

#rowsObject (readonly)

Returns the value of attribute rows.



38
39
40
# File 'lib/data_view.rb', line 38

def rows
  @rows
end

Instance Method Details

#[](index) ⇒ Object

Gets a particular row of the view.



72
73
74
# File 'lib/data_view.rb', line 72

def [](index)
	get_view_row(index)
end

#eachObject

Iterates through each row of the view.



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

def each
	@model.each_index do |row_index|
		yield(get_view_row(row_index))
	end
end

#each_titleObject

Iterates through each title of the columns.



86
87
88
89
90
91
92
# File 'lib/data_view.rb', line 86

def each_title
	@columns.each do |column|
		column_title = column[:title]
		title = (column_title == nil) ? column[:field] : column[:title]
		yield(column, title)
	end
end

#each_title_with_indexObject

Iterates through each title of the columns with the index.



94
95
96
97
98
99
100
# File 'lib/data_view.rb', line 94

def each_title_with_index
	i = 0
	each_title do |f, t|
		yield(f, t, i)
		i = i + 1
	end
end

#lengthObject

The number of rows in the result.



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

def length
	@model.length
end