Sha256: 6c44efe2aefd3915a933bd7795779c30740909d584069b7ee8a60b2d126e72cb

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

require 'gir_ffi/builders/registered_type_builder'
require 'gir_ffi/builders/with_methods'
require 'gir_ffi/enum_base'

module GirFFI
  module Builders
    # Implements the creation of an enum or flags type. The type will be
    # attached to the appropriate namespace module, and will be defined
    # as an enum for FFI.
    class EnumBuilder < RegisteredTypeBuilder
      include WithMethods

      private

      def enum_sym
        @classname.to_sym
      end

      def value_spec
        return info.values.map {|vinfo|
          val = GirFFI::ArgHelper.cast_uint32_to_int32(vinfo.value)
          [vinfo.name.to_sym, val]
        }.flatten
      end

      def instantiate_class
        @enum = optionally_define_constant klass, :Enum do
          lib.enum(enum_sym, value_spec)
        end
        setup_class unless already_set_up
      end

      def setup_class
        klass.extend superclass
        setup_constants
        setup_gtype_getter
        stub_methods
        setup_inspect
      end

      def klass
        @klass ||= get_or_define_module namespace_module, @classname
      end

      def setup_inspect
        klass.instance_eval <<-EOS
          def self.inspect
            "#{@namespace}::#{@classname}"
          end
        EOS
      end

      def already_set_up
        klass.respond_to? :get_gtype
      end

      def superclass
        @superclass ||= EnumBase
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gir_ffi-0.7.1 lib/gir_ffi/builders/enum_builder.rb
gir_ffi-0.7.0 lib/gir_ffi/builders/enum_builder.rb