Sha256: 6edf1a28f39b1b836e0ef2161a04735993a7c34c8a51fa5bbaa085e5d05ba440

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

require 'gir_ffi/builder/type/base'
module GirFFI
  module Builder
    module Type

      # Implements the creation of a constant. Though semantically not a
      # type, its build method is like that of the types, in that it is
      # triggered by a missing constant in the parent namespace.  The
      # constant will be attached to the appropriate namespace module.
      class Constant < Base
        TYPE_TAG_TO_UNION_MEMBER = {
          :gint32 => :v_int32,
          :gdouble => :v_double,
          :utf8 => :v_string
        }

        def build_class
          unless defined? @klass
            instantiate_class
          end
          @klass
        end

        def instantiate_class
          @klass = optionally_define_constant namespace_module, @classname do
            tag = info.constant_type.tag
            val = info.value[TYPE_TAG_TO_UNION_MEMBER[tag]]
            if RUBY_VERSION >= "1.9" and tag == :utf8
              val.force_encoding("utf-8")
            else
              val
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gir_ffi-0.0.10 lib/gir_ffi/builder/type/constant.rb
gir_ffi-0.0.9 lib/gir_ffi/builder/type/constant.rb