Class: Ambry::AbstractKeySet Abstract
- Inherits:
-
Object
- Object
- Ambry::AbstractKeySet
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/ambry/abstract_key_set.rb
Overview
This class is abstract.
Instance Attribute Summary (collapse)
-
- (Object) keys
Returns the value of attribute keys.
-
- (Object) mapper
Returns the value of attribute mapper.
Instance Method Summary (collapse)
- - (Object) &(key_set)
- - (Object) +(key_set) (also: #|)
- - (Object) -(key_set)
-
- (Object) count(&block)
With no block, returns the number of keys.
- - (Object) find(id = nil, &block)
- - (Object) find_by_key(&block)
-
- (Object) first(&block)
With no block, returns an instance for the first key.
-
- (AbstractKeySet) initialize(keys = nil, mapper = nil)
constructor
Create a new KeySet from an array of keys and a mapper.
- - (Object) limit(length)
- - (Object) sort(&block)
- - (Object) to_enum (also: #all)
Constructor Details
- (AbstractKeySet) initialize(keys = nil, mapper = nil)
Create a new KeySet from an array of keys and a mapper.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ambry/abstract_key_set.rb', line 13 def initialize(keys = nil, mapper = nil) @keys = keys || [].freeze # Assume that if a frozen array is passed in, it's already been compacted # and uniqued in order to improve performance. unless @keys.frozen? @keys.uniq! @keys.compact! @keys.freeze end @mapper = mapper end |
Instance Attribute Details
- (Object) keys
Returns the value of attribute keys
8 9 10 |
# File 'lib/ambry/abstract_key_set.rb', line 8 def keys @keys end |
- (Object) mapper
Returns the value of attribute mapper
8 9 10 |
# File 'lib/ambry/abstract_key_set.rb', line 8 def mapper @mapper end |
Instance Method Details
- (Object) &(key_set)
34 35 36 |
# File 'lib/ambry/abstract_key_set.rb', line 34 def &(key_set) self.class.new((keys & key_set.keys).compact.freeze, mapper) end |
- (Object) +(key_set) Also known as: |
25 26 27 |
# File 'lib/ambry/abstract_key_set.rb', line 25 def +(key_set) self.class.new(keys + key_set.keys, mapper) end |
- (Object) -(key_set)
30 31 32 |
# File 'lib/ambry/abstract_key_set.rb', line 30 def -(key_set) self.class.new((keys - key_set.keys).freeze, mapper) end |
- (Object) count(&block)
With no block, returns the number of keys. If a block is given, counts the number of elements yielding a true value.
46 47 48 49 50 51 52 |
# File 'lib/ambry/abstract_key_set.rb', line 46 def count(&block) return keys.count unless block_given? proxy = HashProxy.new keys.inject(0) do |count, key| proxy.with(mapper[key], &block) ? count.succ : count end end |
- (Object) find(id = nil, &block)
54 55 56 57 58 59 60 61 62 |
# File 'lib/ambry/abstract_key_set.rb', line 54 def find(id = nil, &block) return mapper.get(id) if id return self unless block_given? proxy = HashProxy.new self.class.new(keys.inject([]) do |found, key| found << key if proxy.with(mapper[key], &block) found end, mapper) end |
- (Object) find_by_key(&block)
69 70 71 72 73 74 |
# File 'lib/ambry/abstract_key_set.rb', line 69 def find_by_key(&block) return self unless block_given? self.class.new(keys.inject([]) do |set, key| set << key if yield(key); set end, mapper) end |
- (Object) first(&block)
With no block, returns an instance for the first key. If a block is given, it returns the first instance yielding a true value.
40 41 42 |
# File 'lib/ambry/abstract_key_set.rb', line 40 def first(&block) block_given? ? all.detect(&block) : all.first end |
- (Object) limit(length)
87 88 89 |
# File 'lib/ambry/abstract_key_set.rb', line 87 def limit(length) self.class.new(@keys.first(length).freeze, mapper) end |
- (Object) sort(&block)
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ambry/abstract_key_set.rb', line 76 def sort(&block) proxies = HashProxySet.new self.class.new(@keys.sort do |a, b| begin yield(*proxies.using(mapper[a], mapper[b])) ensure proxies.clear end end, mapper) end |
- (Object) to_enum Also known as: all
64 65 66 |
# File 'lib/ambry/abstract_key_set.rb', line 64 def to_enum KeyIterator.new(keys) {|k| @mapper.get(k)} end |