Class: Omniboard::Column

Inherits:
Object
  • Object
show all
Extended by:
Property
Includes:
Property
Defined in:
lib/omniboard/column.rb

Overview

The column. Each column represents either:

  • A grouped column, if Column#group_by or Column.group_by are set

  • An ungrouped column, if they aren’t

Constant Summary collapse

INHERITED_PROPERTIES =
%i(sort mark_when dim_when icon group_by sort_groups group_name hide_dimmed display_project_counts)
ALLOWED_PROJECT_COUNTS =
%i(all active marked inherit)
ALLOWED_PROJECT_DISPLAYS =
%i(full compact)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Property

included

Constructor Details

#initialize(name, &blck) ⇒ Column

Intializer. Provide name and block for instance evaluation (optional)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/omniboard/column.rb', line 56

def initialize(name, &blck)
	# Set defaults
	self.name = name
	self.projects = []
	self.order(0)
	self.width(1)
	self.columns(1)
	self.display(:full)
	self.display_project_counts(nil)
	self.project_limit(nil)
	self.filter_button(false)

	INHERITED_PROPERTIES.each{ |s| self.send(s, :inherit) }

	instance_exec(&blck) if blck
	Omniboard::Column.add(self)
end

Class Attribute Details

.columnsObject (readonly)

Returns the value of attribute columns.



253
254
255
# File 'lib/omniboard/column.rb', line 253

def columns
  @columns
end

Instance Attribute Details

#nameObject

Column name, used to display



8
9
10
# File 'lib/omniboard/column.rb', line 8

def name
  @name
end

#projects(group = nil) ⇒ Object

Return an array of projects, sorted according to the sort block (or not, if no sort block supplied). If group string is provided, only fetched projects for that group



12
13
14
# File 'lib/omniboard/column.rb', line 12

def projects
  @projects
end

Class Method Details

.add(c) ⇒ Object

Add a column to the global columns register



256
257
258
259
# File 'lib/omniboard/column.rb', line 256

def add(c)
	@columns << c
	@columns = @columns.sort_by(&:order)
end

.clear_config(config) ⇒ Object

Clear configuration option. You can always pass :all to clear all configuration options



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/omniboard/column.rb', line 325

def clear_config config
	case config
	when :conditions
		@conditions = nil
	when :sort
		@sort = nil
	when :group_by
		@group_by = nil
	when :mark_when
		@mark_when = nil
	when :dim_when
		@dim_when = nil
	when :icon
		@icon = nil
	when :sort_groups
		@sort_groups = nil
	when :group_name
		@group_name = nil
	when :hide_dimmed
		@hide_dimmed = false
	when :display_project_counts
		@display_project_counts = nil
	when :all
		@conditions = nil
		@sort = nil
		@group_by = nil
		@mark_when = nil
		@dim_when = nil
		@icon = nil
		@sort_groups = nil
		@group_name = nil
		@hide_dimmed = false
		@display_project_counts = nil
	else
		raise ArgumentError, "Do not know how to clear config: #{config}"
	end
end

.colour_for_group(group) ⇒ Object

Returns the appropriate hue for a given group, if it matches any of the colour groups provided by @colour_groups



314
315
316
317
# File 'lib/omniboard/column.rb', line 314

def colour_for_group(group)
	colour_group = @colour_groups.find{ |cgp| cgp[:block][group] }
	return colour_group && colour_group[:hue]
end

.colour_group(hue, &blck) ⇒ Object

Assign a colour to a group, given it fits a block



309
310
311
# File 'lib/omniboard/column.rb', line 309

def colour_group(hue, &blck)
	@colour_groups << {hue: hue, block: blck}
end

.config(&blck) ⇒ Object

Config method



320
321
322
# File 'lib/omniboard/column.rb', line 320

def config &blck
	self.instance_exec(&blck)
end

.reset_columnsObject

Wipe the global register of columns. Useful when resetting Omniboard to a default state



263
264
265
# File 'lib/omniboard/column.rb', line 263

def reset_columns
	@columns = []
end

Instance Method Details

#<<Object

Add an array of projects to the column. If conditions is set, each project will be run through it. Only projects that return true will be allowed in.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/omniboard/column.rb', line 119

def add(arr)
	# Reset cache
	@grouped_projects = nil

	# Make sure it's an array
	arr = [arr] unless arr.is_a?(Array) || arr.is_a?(Rubyfocus::SearchableArray)

	# Run through individual conditions block
	arr = arr.select{ |p| self.conditions[p] } if self.conditions

	# Run through global conditions block
	arr = arr.select{ |p| self.class.conditions[p] } if self.class.conditions

	# Wrap in ProjectWrappers
	arr = arr.map{ |p| p.is_a?(Omniboard::ProjectWrapper) ? p : Omniboard::ProjectWrapper.new(p, column: self) }

	# Tasks performed upon adding project to column. Add to group, mark & dim appropriately
	arr.each do |pw|
		pw.column = self

		pw.group = self.group_for(pw)
		pw.marked = self.should_mark(pw)
		pw.dimmed = self.should_dim(pw)

		# Icon methods
		icon_attrs = self.icon_for(pw)
		if icon_attrs.is_a?(Array)
			pw.icon, pw.icon_alt = *icon_attrs
		else
			pw.icon = icon_attrs
		end
	end

	@projects += arr
end

#add(arr) ⇒ Object

Add an array of projects to the column. If conditions is set, each project will be run through it. Only projects that return true will be allowed in.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/omniboard/column.rb', line 84

def add(arr)
	# Reset cache
	@grouped_projects = nil

	# Make sure it's an array
	arr = [arr] unless arr.is_a?(Array) || arr.is_a?(Rubyfocus::SearchableArray)

	# Run through individual conditions block
	arr = arr.select{ |p| self.conditions[p] } if self.conditions

	# Run through global conditions block
	arr = arr.select{ |p| self.class.conditions[p] } if self.class.conditions

	# Wrap in ProjectWrappers
	arr = arr.map{ |p| p.is_a?(Omniboard::ProjectWrapper) ? p : Omniboard::ProjectWrapper.new(p, column: self) }

	# Tasks performed upon adding project to column. Add to group, mark & dim appropriately
	arr.each do |pw|
		pw.column = self

		pw.group = self.group_for(pw)
		pw.marked = self.should_mark(pw)
		pw.dimmed = self.should_dim(pw)

		# Icon methods
		icon_attrs = self.icon_for(pw)
		if icon_attrs.is_a?(Array)
			pw.icon, pw.icon_alt = *icon_attrs
		else
			pw.icon = icon_attrs
		end
	end

	@projects += arr
end

#can_be_grouped?Boolean

Returns true if column or global group_by supplied

Returns:

  • (Boolean)


146
147
148
# File 'lib/omniboard/column.rb', line 146

def can_be_grouped?
	!!property(:group_by)
end

#count_divObject


Presentation methods



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/omniboard/column.rb', line 221

def count_div
	total = case property(:display_project_counts)
	when :all
		self.projects.count
	when :active
		self.projects.select{ |p| !p.dimmed? }.count
	when :marked
		self.projects.select{ |p| p.marked? }.count
	else
		0
	end
	css_class = "column-total"
	css_class << " limit-breached" if project_limit && project_limit < total
	%|<div class="#{css_class}">#{total}</div>|
end

#group_for(project) ⇒ Object

Return the group a project should fall into



174
175
176
177
178
179
180
181
# File 'lib/omniboard/column.rb', line 174

def group_for(project)
	gby = property(:group_by)
	if gby
		Omniboard::Group[gby[project]]
	else
		nil
	end
end

#group_name_for(group) ⇒ Object

Return the group name for a given group



184
185
186
187
# File 'lib/omniboard/column.rb', line 184

def group_name_for(group)
	gname = property(:group_name)
	gname ? gname[group] : group.to_s
end

#grouped_projectsObject

Return a hash of arrays of sorted projects, grouped using the group_by lambda. Note: Unsorted

Raises:

  • (RuntimeError)


168
169
170
171
# File 'lib/omniboard/column.rb', line 168

def grouped_projects
	raise(RuntimeError, "Attempted to return grouped projects from column #{self.name}, but no group_by method defined.") unless can_be_grouped?
	@grouped_projects ||= self.projects.group_by(&:group)
end

#groupsObject

Returns a sorted array of groups. Returned as strings



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/omniboard/column.rb', line 151

def groups
	keys = grouped_projects.keys.map(&:identifier)

	group_sort_block = property(:sort_groups)
	if group_sort_block.nil?
		keys.sort
	elsif group_sort_block.arity == 1
		keys.sort_by(&group_sort_block)
	elsif group_sort_block.arity == 2
		keys.sort(&group_sort_block)
	else
		raise ArgumentError, "Omniboard::Column.group_sort has an arity of #{group_sort_block.arity}, must take either 1 or 2 arguments."
	end
end

#icon_for(project) ⇒ Object

Return the icon for a given project, based on icon blocks



210
211
212
213
214
215
216
217
# File 'lib/omniboard/column.rb', line 210

def icon_for(project)
	ic = property(:icon)
	if ic
		ic[project]
	else
		nil
	end
end

#property(sym) ⇒ Object

Fetch the appropriate property value. If the value is :inherit, will fetch the appropriate value from Omniboard::Column

Raises:

  • (ArgumentError)


75
76
77
78
79
80
# File 'lib/omniboard/column.rb', line 75

def property(sym)
	raise(ArgumentError, "Unrecognised property #{sym}: allowed values are #{INHERITED_PROPERTIES.join(", ")}.") unless INHERITED_PROPERTIES.include?(sym)
	v = self.send(sym)
	v = Omniboard::Column.send(sym) if v == :inherit
	v
end

#should_dim(project) ⇒ Object

Return the dimmed status of a given project, based on mark_when blocks



200
201
202
203
204
205
206
207
# File 'lib/omniboard/column.rb', line 200

def should_dim(project)
	dim = property(:dim_when)
	if dim
		dim[project]
	else
		false
	end
end

#should_mark(project) ⇒ Object

Return the marked status of a given project, based on mark_when blocks



190
191
192
193
194
195
196
197
# File 'lib/omniboard/column.rb', line 190

def should_mark(project)
	mark = property(:mark_when)
	if mark
		mark[project]
	else
		false
	end
end

#to_sObject



9
# File 'lib/omniboard/column.rb', line 9

def to_s; @name; end