Class: Cardigan::FilteredRepository
- Inherits:
-
Object
- Object
- Cardigan::FilteredRepository
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/cardigan/filtered_repository.rb
Instance Attribute Summary collapse
-
#display_columns ⇒ Object
Returns the value of attribute display_columns.
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#sort_columns ⇒ Object
Returns the value of attribute sort_columns.
Instance Method Summary collapse
- #cards ⇒ Object
- #columns ⇒ Object
- #each_card_from_indices(numbers) ⇒ Object
-
#initialize(repository, user, configuration) ⇒ FilteredRepository
constructor
A new instance of FilteredRepository.
- #max_field_length(name) ⇒ Object
- #multi(key) ⇒ Object
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_columns ⇒ Object
Returns the value of attribute display_columns.
8 9 10 |
# File 'lib/cardigan/filtered_repository.rb', line 8 def display_columns @display_columns end |
#filter ⇒ Object
Returns the value of attribute filter.
8 9 10 |
# File 'lib/cardigan/filtered_repository.rb', line 8 def filter @filter end |
#sort_columns ⇒ Object
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
#cards ⇒ Object
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 |
#columns ⇒ Object
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 |