Module: AttrImmutable

Defined in:
lib/attr_immutable.rb,
lib/attr_immutable/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"1.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/attr_immutable.rb', line 4

def self.included (base)
  base.extend(ClassMethods)
end

Instance Method Details

#attr_immutable(arg) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/attr_immutable.rb', line 27

def attr_immutable (arg)
  attribute_set = false
  attribute = nil
  
  singleton_class = class << self
    self
  end
  
  singleton_class.send(:define_method, arg) do
    attribute
  end
  
  singleton_class.send(:define_method, "#{arg.to_s}=") do |value|
    raise "ERROR: Attempt to modify an immutable attribute" if attribute_set
    attribute_set = true
    attribute = value
  end
end