Class: PersistentEnum::AbstractDummyModel

Inherits:
Object
  • Object
show all
Defined in:
lib/persistent_enum.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, attributes = {}) ⇒ AbstractDummyModel



373
374
375
376
377
378
# File 'lib/persistent_enum.rb', line 373

def initialize(id, name, attributes = {})
  @ordinal       = id
  @enum_constant = name
  @attributes    = attributes.with_indifferent_access
  freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
# File 'lib/persistent_enum.rb', line 399

def method_missing(meth, *args)
  if attributes.has_key?(meth)
    unless args.empty?
      raise ArgumentError.new("wrong number of arguments (#{args.size} for 0)")
    end

    attributes[meth]
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



371
372
373
# File 'lib/persistent_enum.rb', line 371

def attributes
  @attributes
end

#enum_constantObject (readonly)

Returns the value of attribute enum_constant.



371
372
373
# File 'lib/persistent_enum.rb', line 371

def enum_constant
  @enum_constant
end

#ordinalObject (readonly) Also known as: id

Returns the value of attribute ordinal.



371
372
373
# File 'lib/persistent_enum.rb', line 371

def ordinal
  @ordinal
end

Class Method Details

.for_name(name_attr) ⇒ Object



426
427
428
429
430
431
# File 'lib/persistent_enum.rb', line 426

def self.for_name(name_attr)
  Class.new(self) do
    define_singleton_method(:name_attr, -> { name_attr })
    alias_method name_attr, :enum_constant
  end
end

Instance Method Details

#freezeObject



415
416
417
418
419
420
421
422
423
424
# File 'lib/persistent_enum.rb', line 415

def freeze
  @ordinal.freeze
  @enum_constant.freeze
  @attributes.freeze
  @attributes.each do |k, v|
    k.freeze
    v.freeze
  end
  super
end

#read_attribute(attr) ⇒ Object Also known as: []



386
387
388
389
390
391
392
393
394
395
# File 'lib/persistent_enum.rb', line 386

def read_attribute(attr)
  case attr.to_s
  when 'id'
    ordinal
  when self.class.name_attr.to_s
    enum_constant
  else
    @attributes[attr]
  end
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean



411
412
413
# File 'lib/persistent_enum.rb', line 411

def respond_to_missing?(meth, include_private = false)
  attributes.has_key?(meth) || super
end

#to_symObject



380
381
382
# File 'lib/persistent_enum.rb', line 380

def to_sym
  enum_constant.to_sym
end