Class: DeadSimpleCMS::Util::Identifier::Dictionary

Inherits:
Hash
  • Object
show all
Defined in:
lib/dead_simple_cms/util/identifier/dictionary.rb

Overview

Public: a dictionary of objects along with their identifiers. Since we are indexing by the identifier, we extend from Hash since that is our primary data store for the objects. We also enforce the condition that the values must be of a target_class.

Defined Under Namespace

Classes: DuplciateItem, InvalidEntryClass

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_class, *args, &block) ⇒ Dictionary

Returns a new instance of Dictionary.



13
14
15
16
17
18
# File 'lib/dead_simple_cms/util/identifier/dictionary.rb', line 13

def initialize(target_class, *args, &block)
  options = args.extract_options!
  @identifier_method = options[:identifier_method] || :identifier
  @target_class = target_class
  super(*args, &block)
end

Instance Attribute Details

#identifier_methodObject (readonly)

Returns the value of attribute identifier_method.



11
12
13
# File 'lib/dead_simple_cms/util/identifier/dictionary.rb', line 11

def identifier_method
  @identifier_method
end

#target_classObject (readonly)

Returns the value of attribute target_class.



11
12
13
# File 'lib/dead_simple_cms/util/identifier/dictionary.rb', line 11

def target_class
  @target_class
end

Instance Method Details

#[](key) ⇒ Object



48
49
50
51
52
# File 'lib/dead_simple_cms/util/identifier/dictionary.rb', line 48

def [](key)
  result = super(to_identifier(key))
  maybe_build_block(result)
  result
end

#[]=(key, value) ⇒ Object

Public: set a record for the dictionary.

* If the value is not of type target_class, raise an error.
* If the record already exists, raise an error.

Raises:



41
42
43
44
45
46
# File 'lib/dead_simple_cms/util/identifier/dictionary.rb', line 41

def []=(key, value)
  raise InvalidEntryClass, "Only entries with class #{target_class} can be added" unless value.is_a?(target_class)
  identifier = to_identifier(key)
  raise DuplciateItem, "#{identifier} already present in dictionary." if key?(identifier)
  super(identifier, value)
end

#add(item) ⇒ Object

Public: Convenience method for adding item by it’s identifier.



25
26
27
# File 'lib/dead_simple_cms/util/identifier/dictionary.rb', line 25

def add(item)
  self[to_identifier(item)] = item
end

#each(&block) ⇒ Object



54
55
56
57
58
59
# File 'lib/dead_simple_cms/util/identifier/dictionary.rb', line 54

def each(&block)
  super do |k, v|
    maybe_build_block(v)
    block[k, v]
  end
end

#identifiersObject



20
21
22
# File 'lib/dead_simple_cms/util/identifier/dictionary.rb', line 20

def identifiers
  keys
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/dead_simple_cms/util/identifier/dictionary.rb', line 29

def include?(key)
  key?(to_identifier(key))
end

#to_hashObject



33
34
35
# File 'lib/dead_simple_cms/util/identifier/dictionary.rb', line 33

def to_hash
  Hash[self]
end

#valuesObject



61
62
63
64
65
66
67
# File 'lib/dead_simple_cms/util/identifier/dictionary.rb', line 61

def values
  results = super
  results.each do |result|
    maybe_build_block(result)
  end
  results
end