Class: WCC::Media::EnumeratedType

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/media/enumerated_type.rb

Direct Known Subclasses

TagGroup

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ EnumeratedType

Returns a new instance of EnumeratedType.



6
7
8
9
10
# File 'lib/wcc/media/enumerated_type.rb', line 6

def initialize(args = {})
  only_attributes(args).each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Class Method Details

.[](value) ⇒ Object



12
13
14
# File 'lib/wcc/media/enumerated_type.rb', line 12

def self.[](value)
  all.find { |record| record.matches?(value) }
end

.allObject



28
29
30
# File 'lib/wcc/media/enumerated_type.rb', line 28

def self.all
  @all ||= db.collect { |data| new(data) }
end

.attributes(*attrs) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/wcc/media/enumerated_type.rb', line 36

def self.attributes(*attrs)
  attrs.each do |attr|
    defined_attributes << attr
    define_method(attr) do
      instance_variable_get("@#{attr}")
    end
  end
end

.dbObject

Raises:

  • (NotImplementedError)


22
23
24
25
26
# File 'lib/wcc/media/enumerated_type.rb', line 22

def self.db
  raise NotImplementedError,
        'The ::db class method should be defined in subclasses as an array ' \
        'of hashes containing data for each record.'
end

.defined_attributesObject



45
46
47
# File 'lib/wcc/media/enumerated_type.rb', line 45

def self.defined_attributes
  @attributes ||= [] # rubocop:disable Naming/MemoizedInstanceVariableName
end

.inherited(subclass) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/wcc/media/enumerated_type.rb', line 49

def self.inherited(subclass)
  super
  subclass.send(
    :instance_variable_set,
    :@attributes,
    defined_attributes.dup
  )
end

.resetObject



32
33
34
# File 'lib/wcc/media/enumerated_type.rb', line 32

def self.reset
  @all = nil
end

Instance Method Details

#matches?(value) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


16
17
18
19
20
# File 'lib/wcc/media/enumerated_type.rb', line 16

def matches?(value)
  raise NotImplementedError,
        'The #matches? method should be defined in subclasses and return ' \
        'true if the argument matches this record and false otherwise.'
end