Sha256: c598d82324eefd6f6afaa2d6ce46bd2d9ff1147a2e1921c149784f4abe6770eb

Contents?: true

Size: 1.62 KB

Versions: 7

Compression:

Stored size: 1.62 KB

Contents

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


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

7 entries across 7 versions & 1 rubygems

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