Sha256: 8be50dae96a8ae21bc162bc567095fa67f99fb10499c7e414107b96739beec8b

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module RMExtensions

  module ObjectExtensions

    module Accessors

      # creates an +attr_accessor+ like behavior, but the objects are stored with
      # a weak reference (OBJC_ASSOCIATION_ASSIGN).  useful to avoid retain cycles
      # when you want to have access to an object in a place that isnt responsible
      # for that object's lifecycle.
      # does not conform to KVO like attr_accessor does.
      def rmext_weak_attr_accessor(*attrs)
        attrs.each do |attr|
          define_method(attr) do
            rmext_associatedValueForKey(attr.to_sym)
          end
          define_method("#{attr}=") do |val|
            rmext_weaklyAssociateValue(val, withKey: attr.to_sym)
            val
          end
        end
      end

      # creates an +attr_accessor+ like behavior, but the objects are stored with
      # OBJC_ASSOCIATION_COPY.
      # does not conform to KVO like attr_accessor does.
      def rmext_copy_attr_accessor(*attrs)
        attrs.each do |attr|
          define_method(attr) do
            rmext_associatedValueForKey(attr.to_sym)
          end
          define_method("#{attr}=") do |val|
            rmext_atomicallyAssociateCopyOfValue(val, withKey: attr.to_sym)
            val
          end
        end
      end

    end

  end

end
Object.send(:include, ::RMExtensions::ObjectExtensions::Accessors)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rm-extensions-0.0.6 lib/motion/accessors.rb