Module: Celluloid::Internals::Properties

Defined in:
lib/celluloid/internals/properties.rb

Overview

Properties define inheritable attributes of classes, somewhat similar to Rails cattr_/mattr_ or class_attribute

Instance Method Summary collapse

Instance Method Details

#property(name, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/celluloid/internals/properties.rb', line 6

def property(name, opts = {})
  default   = opts.fetch(:default, nil)
  multi     = opts.fetch(:multi, false)
  ivar_name = "@#{name}".to_sym

  singleton = class << ancestors.first; self; end
  singleton.send(:remove_method, name) rescue nil
  singleton.send(:define_method, name) do |value = nil, *extra|
    if value
      value = value ? [value, *send(name), *extra].uniq : [] if multi
      instance_variable_set(ivar_name, value)
    elsif instance_variables.include?(ivar_name)
      instance_variable_get(ivar_name)
    elsif superclass.respond_to? name
      superclass.send(name)
    else
      default
    end
  end
end