Sha256: 91e2cb17918adabd6699d456d6ed655cde0f573cada02d1801dcdf68f43142a6

Contents?: true

Size: 947 Bytes

Versions: 12

Compression:

Stored size: 947 Bytes

Contents

module Celluloid
  module Internals
    # Properties define inheritable attributes of classes, somewhat similar to
    # Rails cattr_*/mattr_* or class_attribute
    module Properties
      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
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
celluloid-essentials-0.20.5 lib/celluloid/internals/properties.rb
celluloid-essentials-0.20.2.1 lib/celluloid/internals/properties.rb
celluloid-essentials-0.20.2 lib/celluloid/internals/properties.rb
celluloid-essentials-0.20.1.1 lib/celluloid/internals/properties.rb
celluloid-essentials-0.20.1 lib/celluloid/internals/properties.rb
celluloid-essentials-0.20.0 lib/celluloid/internals/properties.rb
celluloid-essentials-0.20.0.pre17 lib/celluloid/internals/properties.rb
celluloid-essentials-0.20.0.pre16 lib/celluloid/internals/properties.rb
celluloid-essentials-0.20.0.pre15 lib/celluloid/internals/properties.rb
celluloid-essentials-0.20.0.pre14 lib/celluloid/internals/properties.rb
celluloid-essentials-0.20.0.pre13 lib/celluloid/internals/properties.rb
celluloid-essentials-0.20.0.pre12 lib/celluloid/internals/properties.rb