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

Returns a new instance of AbstractDummyModel.



363
364
365
366
367
368
# File 'lib/persistent_enum.rb', line 363

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



389
390
391
392
393
394
395
396
397
398
399
# File 'lib/persistent_enum.rb', line 389

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.



361
362
363
# File 'lib/persistent_enum.rb', line 361

def attributes
  @attributes
end

#enum_constantObject (readonly)

Returns the value of attribute enum_constant.



361
362
363
# File 'lib/persistent_enum.rb', line 361

def enum_constant
  @enum_constant
end

#ordinalObject (readonly) Also known as: id

Returns the value of attribute ordinal.



361
362
363
# File 'lib/persistent_enum.rb', line 361

def ordinal
  @ordinal
end

Class Method Details

.for_name(name_attr) ⇒ Object



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

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



405
406
407
408
409
410
411
412
413
414
# File 'lib/persistent_enum.rb', line 405

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: []



376
377
378
379
380
381
382
383
384
385
# File 'lib/persistent_enum.rb', line 376

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

Returns:

  • (Boolean)


401
402
403
# File 'lib/persistent_enum.rb', line 401

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

#to_symObject



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

def to_sym
  enum_constant.to_sym
end