Class: RR::Collection
- Inherits:
-
Object
- Object
- RR::Collection
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/refined-refinements/collection.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key, value) ⇒ Object
flashcards[:expressions, ‘hacer’] flashcards[:translations, :silent_translations, ‘to be’].
- #active_items ⇒ Object
- #back_up_path ⇒ Object
- #each(&block) ⇒ Object
- #filter(filter_name, &block) ⇒ Object
- #filter_out(filter_name, &block) ⇒ Object
- #filtered_out_items(filter_name) ⇒ Object
- #filters ⇒ Object
- #has_filter?(filter_name) ⇒ Boolean
-
#initialize(basename) ⇒ Collection
constructor
def initialize(item_class, basename) @path = self.class.data_file_dir.join(“#basename.yml”) @item_class, @activity_filters = item_class, Hash.new end.
- #items(&block) ⇒ Object
- #replace(original_item, new_item) ⇒ Object
- #run_filter(filter_name, items) ⇒ Object
- #save ⇒ Object
- #save_to(path) ⇒ Object
Constructor Details
#initialize(basename) ⇒ Collection
def initialize(item_class, basename)
@path = self.class.data_file_dir.join("#{basename}.yml")
@item_class, @activity_filters = item_class, Hash.new
end
18 19 20 21 |
# File 'lib/refined-refinements/collection.rb', line 18 def initialize(basename) @path = self.class.data_file_dir.join(basename) @activity_filters = Hash.new end |
Class Method Details
.data_file_dir ⇒ Object
10 11 12 |
# File 'lib/refined-refinements/collection.rb', line 10 def self.data_file_dir Pathname.new("~/Dropbox/Data/Data/Flashcards"). end |
Instance Method Details
#[](key, value) ⇒ Object
flashcards[:expressions, ‘hacer’] flashcards[:translations, :silent_translations, ‘to be’]
69 70 71 72 73 |
# File 'lib/refined-refinements/collection.rb', line 69 def [](key, value) self.items.select do |item| [item.send(key)].flatten.include?(value) end end |
#active_items ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/refined-refinements/collection.rb', line 33 def active_items return self.items if @activity_filters.empty? @activity_filters.keys.reduce(self.items) do |items, filter_name| self.run_filter(filter_name, items) end end |
#back_up_path ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/refined-refinements/collection.rb', line 112 def back_up_path chunks = @path.basename.to_s.split('.') = Time.now.strftime('%Y-%m-%d-%H-%M') basename = chunks.insert(-2, ).join('.') self.class.data_file_dir.join('Backups', basename) end |
#each(&block) ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/refined-refinements/collection.rb', line 124 def each(&block) if block self.active_items.each(&block) else self.active_items.to_enum end end |
#filter(filter_name, &block) ⇒ Object
45 46 47 48 |
# File 'lib/refined-refinements/collection.rb', line 45 def filter(filter_name, &block) @activity_filters[filter_name] = block if block self end |
#filter_out(filter_name, &block) ⇒ Object
50 51 52 53 |
# File 'lib/refined-refinements/collection.rb', line 50 def filter_out(filter_name, &block) @activity_filters[filter_name] = Proc.new { |item| ! block.call(item) } if block self end |
#filtered_out_items(filter_name) ⇒ Object
63 64 65 |
# File 'lib/refined-refinements/collection.rb', line 63 def filtered_out_items(filter_name) self.items - self.run_filter(filter_name, self.items) end |
#filters ⇒ Object
55 56 57 |
# File 'lib/refined-refinements/collection.rb', line 55 def filters @activity_filters.keys end |
#has_filter?(filter_name) ⇒ Boolean
59 60 61 |
# File 'lib/refined-refinements/collection.rb', line 59 def has_filter?(filter_name) @activity_filters.has_key?(filter_name) end |
#items(&block) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/refined-refinements/collection.rb', line 23 def items(&block) @items ||= self.load_raw_collection.map do |data| begin block.call(data) rescue => error abort "Loading item #{data.inspect} failed: #{error.}.\n\n#{error.backtrace}" end end end |
#replace(original_item, new_item) ⇒ Object
81 82 83 84 85 |
# File 'lib/refined-refinements/collection.rb', line 81 def replace(original_item, new_item) index = self.items.index(original_item) self.items.delete(original_item) self.items.insert(index, new_item) end |
#run_filter(filter_name, items) ⇒ Object
41 42 43 |
# File 'lib/refined-refinements/collection.rb', line 41 def run_filter(filter_name, items) items.select { |item| @activity_filters[filter_name].call(item) } end |
#save ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/refined-refinements/collection.rb', line 87 def save return if self.items.empty? if File.mtime(@path.to_s) > @loaded_at raise "Cannot be saved #{File.mtime(@path.to_s).inspect} vs. #{@loaded_at.inspect}" end self.save_to(@path) && self.save_to(self.back_up_path) @loaded_at = Time.now # Otherwise the next save call with fail. true end |
#save_to(path) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/refined-refinements/collection.rb', line 100 def save_to(path) updated_data = self.serialise if (! File.exist?(path)) || File.read(path) != updated_data path.open('w') do |file| file.write(updated_data) end return true else return false end end |