Class: Norman::AbstractKeySet Abstract

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/norman/abstract_key_set.rb

Overview

This class is abstract.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys = nil, mapper = nil) ⇒ AbstractKeySet

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/norman/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

#keysObject

Returns the value of attribute keys.



8
9
10
# File 'lib/norman/abstract_key_set.rb', line 8

def keys
  @keys
end

#mapperObject

Returns the value of attribute mapper.



8
9
10
# File 'lib/norman/abstract_key_set.rb', line 8

def mapper
  @mapper
end

Instance Method Details

#&(key_set) ⇒ Object



34
35
36
# File 'lib/norman/abstract_key_set.rb', line 34

def &(key_set)
  self.class.new((keys & key_set.keys).compact.freeze, mapper)
end

#+(key_set) ⇒ Object Also known as: |



25
26
27
# File 'lib/norman/abstract_key_set.rb', line 25

def +(key_set)
  self.class.new(keys + key_set.keys, mapper)
end

#-(key_set) ⇒ Object



30
31
32
# File 'lib/norman/abstract_key_set.rb', line 30

def -(key_set)
  self.class.new((keys - key_set.keys).freeze, mapper)
end

#count(&block) ⇒ Object

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/norman/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

#find(id = nil, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/norman/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

#find_by_key(&block) ⇒ Object



69
70
71
72
73
74
# File 'lib/norman/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

#first(&block) ⇒ Object

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/norman/abstract_key_set.rb', line 40

def first(&block)
  block_given? ? all.detect(&block) : all.first
end

#limit(length) ⇒ Object



87
88
89
# File 'lib/norman/abstract_key_set.rb', line 87

def limit(length)
  self.class.new(@keys.first(length).freeze, mapper)
end

#sort(&block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/norman/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

#to_enumObject Also known as: all



64
65
66
# File 'lib/norman/abstract_key_set.rb', line 64

def to_enum
  KeyIterator.new(keys) {|k| @mapper.get(k)}
end