Module: Adjective::Storable
- Defined in:
- lib/adjective/concerns/storable.rb
Instance Method Summary collapse
- #can_store?(items) ⇒ Boolean
-
#dump ⇒ Object
(also: #clear)
Dump - Delete all.
-
#dump_by(attribute, value) ⇒ Object
(also: #clear_by)
Dump selection - Selective delete.
-
#empty? ⇒ Boolean
Utility Methods.
- #empty_slots ⇒ Object
- #full? ⇒ Boolean
- #initialize_storage_data(items = [], opts = {}) ⇒ Object
-
#query(term, scope = :all) ⇒ Object
(also: #search)
Simple Search options for scope are: :all, :attributes, :values.
-
#retrieve_by(attribute, value) ⇒ Object
(also: #get_by, #find_by)
retrieve_by - Get.
-
#sort ⇒ Object
Sorting.
- #sort! ⇒ Object
- #sort_by(attribute, order = :asc) ⇒ Object
- #sort_by!(attribute, order = :asc) ⇒ Object
-
#store(items) ⇒ Object
(also: #deposit, #put)
Store - Put.
Instance Method Details
#can_store?(items) ⇒ Boolean
23 24 25 26 27 |
# File 'lib/adjective/concerns/storable.rb', line 23 def can_store?(items) return true if @max_size == :unlimited return true if (Array(items).length + @items.length) <= @max_size return false end |
#dump ⇒ Object Also known as: clear
Dump - Delete all
69 70 71 72 73 |
# File 'lib/adjective/concerns/storable.rb', line 69 def dump outbound_items = @items @items = [] return outbound_items end |
#dump_by(attribute, value) ⇒ Object Also known as: clear_by
Dump selection - Selective delete
76 77 78 79 80 |
# File 'lib/adjective/concerns/storable.rb', line 76 def dump_by(attribute, value) outbound_items = retrieve_by(attribute, value) @items = @items.select {|item| !outbound_items.include?(item) } return outbound_items end |
#empty? ⇒ Boolean
Utility Methods
15 16 17 |
# File 'lib/adjective/concerns/storable.rb', line 15 def empty? @items.length === 0 end |
#empty_slots ⇒ Object
29 30 31 |
# File 'lib/adjective/concerns/storable.rb', line 29 def empty_slots @max_size == :unlimited ? :unlimited : @max_size - @items.length end |
#full? ⇒ Boolean
19 20 21 |
# File 'lib/adjective/concerns/storable.rb', line 19 def full? @max_size == :unlimited ? false : @items.length < @max_size end |
#initialize_storage_data(items = [], opts = {}) ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/adjective/concerns/storable.rb', line 5 def initialize_storage_data(items = [], opts = {}) @items = items @initialized_at = Time.now @max_size = opts[:max_size] ||= :unlimited @default_sort_method = opts[:default_sort_method] ||= :object_id validate_inventory_capacity [:items, :initialized_at, :max_size, :default_sort_method].each {|attribute| self.class.send(:attr_accessor, attribute) } end |
#query(term, scope = :all) ⇒ Object Also known as: search
Simple Search options for scope are: :all, :attributes, :values
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/adjective/concerns/storable.rb', line 35 def query(term, scope = :all) validate_query_scope(scope) matches = [] @items.each do |item| chunks = [] attributes = item.instance_variables.map {|ivar| ivar.to_s.gsub("@", "").to_sym} attributes.each do |attribute| chunks.push(construct_query_data(attribute.to_s, item.send(attribute).to_s)[scope]) end matches << item if chunks.join.include?(term) end return matches end |
#retrieve_by(attribute, value) ⇒ Object Also known as: get_by, find_by
retrieve_by - Get
62 63 64 65 66 |
# File 'lib/adjective/concerns/storable.rb', line 62 def retrieve_by(attribute, value) @items.select do |item| item if item.respond_to?(attribute) && item.send(attribute) === value end end |
#sort ⇒ Object
Sorting
83 84 85 86 |
# File 'lib/adjective/concerns/storable.rb', line 83 def sort @items.sort_by { |item| item.send(@default_sort_method) } return @items end |
#sort! ⇒ Object
88 89 90 91 |
# File 'lib/adjective/concerns/storable.rb', line 88 def sort! @items = @items.sort_by { |item| item.send(@default_sort_method) } return @items end |
#sort_by(attribute, order = :asc) ⇒ Object
93 94 95 96 |
# File 'lib/adjective/concerns/storable.rb', line 93 def sort_by(attribute, order = :asc) sorted = @items.sort_by(&attribute) return order == :asc ? sorted : sorted.reverse end |
#sort_by!(attribute, order = :asc) ⇒ Object
98 99 100 101 |
# File 'lib/adjective/concerns/storable.rb', line 98 def sort_by!(attribute, order = :asc) sorted = @items.sort_by(&attribute) return order == :asc ? @items = sorted : @items = sorted.reverse end |
#store(items) ⇒ Object Also known as: deposit, put
Store - Put
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/adjective/concerns/storable.rb', line 50 def store(items) if can_store?(items) Array(items).each do |item| @items << item end return items else return false end end |