module UnderOs::UI::Wrap

The raw -> abstraction wrapping code for UI::Views

Constants

INSTANCES_CACHE
RAW_WRAPS_MAP
WRAPS_TAGS_MAP

Public Class Methods

find_raw_class_for(wrap) click to toggle source
# File lib/under_os/ui/utils/wrap.rb, line 64
def self.find_raw_class_for(wrap)
  while wrap
    return RAW_WRAPS_MAP[wrap] if RAW_WRAPS_MAP[wrap]
    wrap = wrap.superclass
  end
end
find_wrap_for(raw_class) click to toggle source
# File lib/under_os/ui/utils/wrap.rb, line 56
def self.find_wrap_for(raw_class)
  RAW_WRAPS_MAP.each do |wrap, raw|
    return wrap if raw == raw_class
  end

  return nil
end
included(base) click to toggle source
# File lib/under_os/ui/utils/wrap.rb, line 10
def self.included(base)
  base.instance_eval do
    attr_accessor :_

    def self.wraps(raw_class, options={})
      RAW_WRAPS_MAP[self] = raw_class
      tag(options[:tag]) if options[:tag]
    end

    def self.tag(name)
      WRAPS_TAGS_MAP[name.to_s] = self
    end

    def self.new(options={}, *args, &block)
      return INSTANCES_CACHE[options] if INSTANCES_CACHE[options]

      if options.is_a?(UIView)
        klass = find_wrap_for(options.class)
        view  = options; options = args.shift || {}
      else
        klass = self
        view  = find_raw_class_for(self).alloc
        if view.class == UICollectionView
          view.initWithFrame([[0, 0], [0, 0]], collectionViewLayout: UICollectionViewFlowLayout.alloc.init)
        else
          view.initWithFrame([[0, 0], [0, 0]])
        end
      end

      return nil if ! klass

      klass.alloc.tap do |inst|
        INSTANCES_CACHE[inst._ = view] = inst
        inst.__send__ :initialize, options, *args, &block
      end
    end

    def self.rewrap(view, *args, &block)
      view = view._ if view.is_a?(UnderOs::UI::View)

      alloc.tap do |inst|
        INSTANCES_CACHE[inst._ = view] = inst
        inst.__send__ :initialize, *args, &block
      end
    end

    def self.find_wrap_for(raw_class)
      RAW_WRAPS_MAP.each do |wrap, raw|
        return wrap if raw == raw_class
      end

      return nil
    end

    def self.find_raw_class_for(wrap)
      while wrap
        return RAW_WRAPS_MAP[wrap] if RAW_WRAPS_MAP[wrap]
        wrap = wrap.superclass
      end
    end

    def self.wrap_for(raw_view)
      INSTANCES_CACHE[raw_view]
    end
  end
end
new(options={}, *args, &block) click to toggle source
# File lib/under_os/ui/utils/wrap.rb, line 23
def self.new(options={}, *args, &block)
  return INSTANCES_CACHE[options] if INSTANCES_CACHE[options]

  if options.is_a?(UIView)
    klass = find_wrap_for(options.class)
    view  = options; options = args.shift || {}
  else
    klass = self
    view  = find_raw_class_for(self).alloc
    if view.class == UICollectionView
      view.initWithFrame([[0, 0], [0, 0]], collectionViewLayout: UICollectionViewFlowLayout.alloc.init)
    else
      view.initWithFrame([[0, 0], [0, 0]])
    end
  end

  return nil if ! klass

  klass.alloc.tap do |inst|
    INSTANCES_CACHE[inst._ = view] = inst
    inst.__send__ :initialize, options, *args, &block
  end
end
rewrap(view, *args, &block) click to toggle source
# File lib/under_os/ui/utils/wrap.rb, line 47
def self.rewrap(view, *args, &block)
  view = view._ if view.is_a?(UnderOs::UI::View)

  alloc.tap do |inst|
    INSTANCES_CACHE[inst._ = view] = inst
    inst.__send__ :initialize, *args, &block
  end
end
tag(name) click to toggle source
# File lib/under_os/ui/utils/wrap.rb, line 19
def self.tag(name)
  WRAPS_TAGS_MAP[name.to_s] = self
end
wrap_for(raw_view) click to toggle source
# File lib/under_os/ui/utils/wrap.rb, line 71
def self.wrap_for(raw_view)
  INSTANCES_CACHE[raw_view]
end
wraps(raw_class, options={}) click to toggle source
# File lib/under_os/ui/utils/wrap.rb, line 14
def self.wraps(raw_class, options={})
  RAW_WRAPS_MAP[self] = raw_class
  tag(options[:tag]) if options[:tag]
end