Sha256: 7f98d3c7b2c87cd898e75e66864bae2de01572adf254c101b1c31f3637bd29a0

Contents?: true

Size: 1.59 KB

Versions: 13

Compression:

Stored size: 1.59 KB

Contents

require 'xot/bit_flag'
require 'xot/block_util'


module Xot


  module BitFlagAccessor

    def bit_flag_accessor(name, bit_flag = nil, **flags, &block)
      bf = define_bit_flag name, bit_flag, flags, block
      define_bit_flag_writer name, bf
      define_bit_flag_reader name, bf
    end

    def bit_flag_writer(name, bit_flag = nil, **flags, &block)
      define_bit_flag_writer name, define_bit_flag(name, bit_flag, flags, block)
    end

    def bit_flag_reader(name, bit_flag = nil, **flags, &block)
      define_bit_flag_reader name, define_bit_flag(name, bit_flag, flags, block)
    end

    private

      def define_bit_flag(name, bit_flag, flags, block)
        bit_flag ||= Xot::BitFlag.new(**flags)
        Xot::BlockUtil.instance_eval_or_block_call bit_flag, &block if block

        define_singleton_method "#{name}_flag".intern do
          bit_flag
        end

        bit_flag
      end

      def define_bit_flag_writer(name, bit_flag)
        writer = "#{name}=".intern
        setter = "bf_set_#{name}__".intern

        alias_method setter, writer
        private setter

        define_method writer do |*symbols|
          __send__ setter, bit_flag.symbols2bits(symbols)
        end

        name
      end

      def define_bit_flag_reader(name, bit_flag)
        reader = name.intern
        getter = "bf_get_#{name}__".intern

        alias_method getter, reader
        private getter

        define_method reader do
          bit_flag.bits2symbols __send__(getter)
        end

        name
      end

  end# BitFlagAccessor


end# Xot


class Module

  include Xot::BitFlagAccessor

end# Module

Version data entries

13 entries across 13 versions & 1 rubygems

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