Sha256: 6931d9886b79cf3974b088f1345b257af5c07aef0fc2cd9cc944a89926c22411

Contents?: true

Size: 840 Bytes

Versions: 7

Compression:

Stored size: 840 Bytes

Contents

# -*- coding: utf-8 -*-


module Xot


  module UniversalAccessor

    def universal_accessor (*names, **names_with_options)
      names.each              {|name|       define_universal_accessor name}
      names_with_options.each {|name, opts| define_universal_accessor name, opts}
    end

    private

      def define_universal_accessor (name, options = {})
        set = "ua_set_#{name}__"
        get = "ua_get_#{name}__"
        class_eval <<-END
          alias_method :#{set}, options[:writer] || :#{name}=
          alias_method :#{get}, options[:reader] || :#{name}
          private :#{set}, :#{get}
          def #{name} (*args)
            #{set} *args unless args.empty?
            #{get}
          end
        END
      end

  end# UniversalAccessor


end# Xot


class Module

  include Xot::UniversalAccessor

end# Module

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
xot-0.1.21 lib/xot/universal_accessor.rb
xot-0.1.20 lib/xot/universal_accessor.rb
xot-0.1.19 lib/xot/universal_accessor.rb
xot-0.1.16 lib/xot/universal_accessor.rb
xot-0.1.15 lib/xot/universal_accessor.rb
xot-0.1.14 lib/xot/universal_accessor.rb
xot-0.1.13 lib/xot/universal_accessor.rb