Class: Sherlock::Collection::Base
- Defined in:
- lib/sherlock/collection/base.rb
Instance Method Summary collapse
- #&(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
-
#[](value, *args) ⇒ Object
Filters the collection, if the first argument is an Array, Regexp, String or Hash.
-
#first(*value) ⇒ Object
Returns the first value of the collection (matching the value, if given).
-
#initialize(arr = [], opts = {}) ⇒ Base
constructor
A new instance of Base.
-
#select_items_matching(*pattern) ⇒ Object
(also: #filter)
Returns a collection with all files matching the given pattern.
- #|(other) ⇒ Object
Constructor Details
#initialize(arr = [], opts = {}) ⇒ Base
Returns a new instance of Base.
6 7 8 9 |
# File 'lib/sherlock/collection/base.rb', line 6 def initialize(arr = [], opts = {}) super(0) self.concat (arr, opts) end |
Instance Method Details
#&(other) ⇒ Object
50 51 52 |
# File 'lib/sherlock/collection/base.rb', line 50 def &(other) new(super) end |
#+(other) ⇒ Object
46 47 48 |
# File 'lib/sherlock/collection/base.rb', line 46 def +(other) new(super.uniq) end |
#-(other) ⇒ Object
42 43 44 |
# File 'lib/sherlock/collection/base.rb', line 42 def -(other) new(super) end |
#[](value, *args) ⇒ Object
Filters the collection, if the first argument is an Array, Regexp, String or Hash.
33 34 35 36 37 38 39 40 |
# File 'lib/sherlock/collection/base.rb', line 33 def [](value, *args) case value when String, Regexp, Array, Hash filter(value, *args) else super(value) end end |
#first(*value) ⇒ Object
Returns the first value of the collection (matching the value, if given).
12 13 14 15 16 17 18 19 |
# File 'lib/sherlock/collection/base.rb', line 12 def first(*value) item = if value.empty? super else filter(value)[0] end new([item]) end |
#select_items_matching(*pattern) ⇒ Object Also known as: filter
Returns a collection with all files matching the given pattern.
23 24 25 26 27 28 29 |
# File 'lib/sherlock/collection/base.rb', line 23 def select_items_matching(*pattern) opts = pattern.last.is_a?(Hash) ? pattern.pop : {} pattern = [pattern].flatten arr = select { |f| pattern.empty? || matching?(f, pattern) } arr = (arr, opts) new(arr, opts) end |
#|(other) ⇒ Object
54 55 56 |
# File 'lib/sherlock/collection/base.rb', line 54 def |(other) new(super) end |