Class: SimpleEnum::Persistence::Hasher

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/simple_enum/persistence/hasher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Hasher

Returns a new instance of Hasher.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/simple_enum/persistence/hasher.rb', line 8

def initialize options
  @enum_klass = options[0]
  options = options[1] || Hash.new
  @enum_key = options[:key] || :name
  @enum_value = options[:value] || :id

  unless enum_klass.method_defined? :update_enum_hashers
    enum_klass.instance_eval do
      def enum_hashers
        @enum_hashers ||= []
      end

      def update_enum_hashers
        records = all.to_a
        enum_hashers.each do |hasher|
          hasher.enum_replace records
        end
      end
    end

    enum_klass.class_eval do
      def update_enum_hashers
        self.class.update_enum_hashers
      end

      after_commit :update_enum_hashers
    end
  end

  enum_klass.enum_hashers.push self
  enum_replace enum_klass.all
end

Instance Attribute Details

#enum_keyObject (readonly)

Returns the value of attribute enum_key.



6
7
8
# File 'lib/simple_enum/persistence/hasher.rb', line 6

def enum_key
  @enum_key
end

#enum_klassObject (readonly)

Returns the value of attribute enum_klass.



6
7
8
# File 'lib/simple_enum/persistence/hasher.rb', line 6

def enum_klass
  @enum_klass
end

#enum_mappingObject (readonly)

Returns the value of attribute enum_mapping.



6
7
8
# File 'lib/simple_enum/persistence/hasher.rb', line 6

def enum_mapping
  @enum_mapping
end

#enum_valueObject (readonly)

Returns the value of attribute enum_value.



6
7
8
# File 'lib/simple_enum/persistence/hasher.rb', line 6

def enum_value
  @enum_value
end

Instance Method Details

#each_pair(&block) ⇒ Object Also known as: each



50
51
52
# File 'lib/simple_enum/persistence/hasher.rb', line 50

def each_pair &block
  @enum_mapping.each &block
end

#enum_replace(records) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/simple_enum/persistence/hasher.rb', line 41

def enum_replace records
  @enum_mapping = Array.new
  records.each do |record|
    key = record.send(enum_key)
    value =  record.send(enum_value)
    @enum_mapping.push([key, value])
  end
end

#freezeObject

Do nothing when freeze, cause we have to motify the hash later



87
88
89
# File 'lib/simple_enum/persistence/hasher.rb', line 87

def freeze
  self
end

#key(value) ⇒ Object



63
64
65
66
# File 'lib/simple_enum/persistence/hasher.rb', line 63

def key value
  matched = @enum_mapping.find { |k, v| v == value }
  matched[0] if matched
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/simple_enum/persistence/hasher.rb', line 55

def key? key
  @enum_mapping.any? { |k, v| k == key }
end

#keysObject



74
75
76
# File 'lib/simple_enum/persistence/hasher.rb', line 74

def keys
  @enum_mapping.map { |k, v| k }
end

#value(key) ⇒ Object Also known as: []



68
69
70
71
# File 'lib/simple_enum/persistence/hasher.rb', line 68

def value key
  matched = @enum_mapping.find { |k, v| k == key }
  matched[1] if matched
end

#value?(value) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/simple_enum/persistence/hasher.rb', line 59

def value? value
  @enum_mapping.any? { |k, v| v == value }
end

#valuesObject



78
79
80
# File 'lib/simple_enum/persistence/hasher.rb', line 78

def values
  @enum_mapping.map { |k, v| v }
end

#values_at(*keys) ⇒ Object



82
83
84
# File 'lib/simple_enum/persistence/hasher.rb', line 82

def values_at *keys
  @enum_mapping.select { |k, v| keys.include? k }.map { |k, v| v }
end