Sha256: 1ee30a8c5cd65fef722d95e082bf80f8c2c8d79250510b4d11817359e4312e31

Contents?: true

Size: 847 Bytes

Versions: 11

Compression:

Stored size: 847 Bytes

Contents

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

11 entries across 11 versions & 1 rubygems

Version Path
xot-0.3 lib/xot/universal_accessor.rb
xot-0.2.1 lib/xot/universal_accessor.rb
xot-0.2 lib/xot/universal_accessor.rb
xot-0.1.42 lib/xot/universal_accessor.rb
xot-0.1.41 lib/xot/universal_accessor.rb
xot-0.1.40 lib/xot/universal_accessor.rb
xot-0.1.39 lib/xot/universal_accessor.rb
xot-0.1.38 lib/xot/universal_accessor.rb
xot-0.1.37 lib/xot/universal_accessor.rb
xot-0.1.36 lib/xot/universal_accessor.rb
xot-0.1.35 lib/xot/universal_accessor.rb