Class: Cartos::Cashbase::Collection
- Inherits:
-
Object
- Object
- Cartos::Cashbase::Collection
- Defined in:
- lib/cartos/cashbase.rb
Instance Attribute Summary collapse
-
#elements ⇒ Object
Returns the value of attribute elements.
Instance Method Summary collapse
- #categories ⇒ Object
- #each(&block) ⇒ Object
- #each_category(&block) ⇒ Object
- #each_month(&block) ⇒ Object
- #filter_by_month(month) ⇒ Object
- #filter_by_year(year) ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
Instance Attribute Details
#elements ⇒ Object
Returns the value of attribute elements.
31 32 33 |
# File 'lib/cartos/cashbase.rb', line 31 def elements @elements end |
Instance Method Details
#categories ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/cartos/cashbase.rb', line 33 def categories result = self.elements.inject([]) do |memo, element| memo << element.category end result.uniq end |
#each(&block) ⇒ Object
85 86 87 |
# File 'lib/cartos/cashbase.rb', line 85 def each(&block) self.elements.each &block if block_given? end |
#each_category(&block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cartos/cashbase.rb', line 65 def each_category(&block) categories_hash = self.elements.inject({}) do |memo, element| (memo[element.category] ||= []) << element memo end categories_hash.each_pair do |category, elements| collection = self.class.new collection.elements = elements yield category, elements end end |
#each_month(&block) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cartos/cashbase.rb', line 52 def each_month(&block) months_hash = self.elements.inject({}) do |memo, element| (memo[element.date.month] ||= []) << element memo end months_hash.each_pair do |month, elements| collection = self.class.new collection.elements = elements yield month, collection end end |
#filter_by_month(month) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/cartos/cashbase.rb', line 78 def filter_by_month(month) self.elements.select! do |element| element.date.month == month end self end |
#filter_by_year(year) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/cartos/cashbase.rb', line 45 def filter_by_year(year) self.elements.select! do |element| element.date.year == year end self end |
#size ⇒ Object
41 42 43 |
# File 'lib/cartos/cashbase.rb', line 41 def size self.elements.size end |
#to_a ⇒ Object
89 90 91 |
# File 'lib/cartos/cashbase.rb', line 89 def to_a self.elements end |