Sha256: 9cfdf919c63ad96ee68b77ba1d1b351ec416ddd6f605fb3b9d5b4e81524ddf28

Contents?: true

Size: 873 Bytes

Versions: 7

Compression:

Stored size: 873 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, **kwargs)
            #{set}(*args, **kwargs) unless args.empty? && 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.34 lib/xot/universal_accessor.rb
xot-0.1.33 lib/xot/universal_accessor.rb
xot-0.1.32 lib/xot/universal_accessor.rb
xot-0.1.31 lib/xot/universal_accessor.rb
xot-0.1.30 lib/xot/universal_accessor.rb
xot-0.1.29 lib/xot/universal_accessor.rb
xot-0.1.28 lib/xot/universal_accessor.rb