Class: Cardigan::FilteredRepository

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/cardigan/filtered_repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, user, configuration) ⇒ FilteredRepository

Returns a new instance of FilteredRepository.



13
14
15
16
17
18
# File 'lib/cardigan/filtered_repository.rb', line 13

def initialize repository, user, configuration
  @repository, @user, @configuration = repository, user, configuration
  @sort_columns = multi('sort') || ['name']
  @display_columns = multi('display') || ['name']
  @filter = @configuration['filter']
end

Instance Attribute Details

#display_columnsObject

Returns the value of attribute display_columns.



8
9
10
# File 'lib/cardigan/filtered_repository.rb', line 8

def display_columns
  @display_columns
end

#filterObject

Returns the value of attribute filter.



8
9
10
# File 'lib/cardigan/filtered_repository.rb', line 8

def filter
  @filter
end

#sort_columnsObject

Returns the value of attribute sort_columns.



8
9
10
# File 'lib/cardigan/filtered_repository.rb', line 8

def sort_columns
  @sort_columns
end

Instance Method Details

#cardsObject



40
41
42
43
44
45
46
47
48
# File 'lib/cardigan/filtered_repository.rb', line 40

def cards
  me = @user
  cards = @filter ? @repository.select {|card| eval @filter } : @repository.to_a
  cards.sort do |a,b|
    a_values = sort_columns.map {|col| a[col] ? a[col] : '' }
    b_values = sort_columns.map {|col| b[col] ? b[col] : '' }
    a_values <=> b_values
  end
end

#columnsObject



50
51
52
53
54
# File 'lib/cardigan/filtered_repository.rb', line 50

def columns
  columns = Set.new
  @repository.each {|card| columns += card.keys }
  columns.to_a
end

#each_card_from_indices(numbers) ⇒ Object



60
61
62
63
64
65
# File 'lib/cardigan/filtered_repository.rb', line 60

def each_card_from_indices numbers
  c = cards
  numbers.scan(/\d+/).each do |n|
    yield c[n.to_i - 1]
  end
end

#max_field_length(name) ⇒ Object



56
57
58
# File 'lib/cardigan/filtered_repository.rb', line 56

def max_field_length name
  cards.map {|card| card[name] ? card[name].length : 0 }.max
end

#multi(key) ⇒ Object



20
21
22
23
# File 'lib/cardigan/filtered_repository.rb', line 20

def multi key
  return nil unless @configuration[key]
  @configuration[key].split "\n"
end