Sha256: cfadb80107b0ca874827603a5f41ef103569f5c2b1cd17c34e75690ae7320c56
Contents?: true
Size: 1.54 KB
Versions: 6
Compression:
Stored size: 1.54 KB
Contents
# frozen_string_literal: true # Copied out of WCC::Data::EnumeratedType so that we don't have to depend on the wcc-data gem module WCC::Media class EnumeratedType def initialize(args = {}) only_attributes(args).each do |key, value| instance_variable_set("@#{key}", value) end end def self.[](value) all.find { |record| record.matches?(value) } end 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 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 def self.all @all ||= db.collect { |data| new(data) } end def self.reset @all = nil end def self.attributes(*attrs) attrs.each do |attr| defined_attributes << attr define_method(attr) do instance_variable_get("@#{attr}") end end end def self.defined_attributes @attributes ||= [] # rubocop:disable Naming/MemoizedInstanceVariableName end def self.inherited(subclass) super subclass.send( :instance_variable_set, :@attributes, defined_attributes.dup ) end private def only_attributes(args) args.select do |key, _value| self.class.defined_attributes.include?(key) end end end end
Version data entries
6 entries across 6 versions & 1 rubygems