Sha256: b57b267a34bcf2c9446d814948d3abd074fc1bd70d263790c541000c4e27f66f

Contents?: true

Size: 1.91 KB

Versions: 52

Compression:

Stored size: 1.91 KB

Contents

module ShadowClassesSupport
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    alias_method :real_to_s, :to_s
    # NOTE: this workaround is neccessary, because
    # in many places scrivito_sdk assumes that
    # class.name == obj_class_name which isn't
    # true for shadow classes
    # it breaks the bijection that scrivito_sdk
    # expects, but injection is mostly ok
    def to_s
      self.name.sub(/\AShadowClasses::/, '')
    end

    def real_to_s
      self.name
    end
  end
end

module ShadowClasses
  def self.fetch(name,type)
    # false skips inheritance chain, it checks only this module
    if self.const_defined?(name, false)
      self.const_get(name, false)
    else
      self.define(name,type)
    end
  end

  def self.define(name,type)
    if name =~ /::/
      raise "Invalid name: #{name}, nesting of classes not allowed"
    end

    if type == 'Obj'
      if name == 'Obj'
        self.const_set(name, scrivito_obj_class)
      else 
        self.const_set(
          name,
          Class.new(scrivito_obj_class) do
            include ::ShadowClassesSupport
          end
        ).tap do |klass|
          klass.register_attribute_definitions(name)
        end
      end
    elsif type == 'Widget'
      parent_class = 'Widget'.safe_constantize || Scrivito::BasicWidget
      self.const_set(
        name,
        Class.new(parent_class) do
          include ::ShadowClassesSupport
        end
      ).tap do |klass|
        klass.register_attribute_definitions(name)
      end
    end

  end

  def self.scrivito_obj_class
    if !::Scrivito.const_defined?('Obj', false)
      if Fiona7.mode == :standalone
        ::Scrivito.const_set('Obj', ::Obj)
      elsif Fiona7.mode == :legacy
        ::Scrivito.const_set('Obj', Class.new(::Scrivito::BasicObj))
      else
        raise "Invalid Fiona7.mode = #{Fiona7.mode}"
      end
    end
    ::Scrivito.const_get('Obj', false)
  end
end

Version data entries

52 entries across 52 versions & 1 rubygems

Version Path
infopark_fiona7-0.71.1.12 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.1.11 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.1.10 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.1.1 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.1.0 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.0.7 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.0.6 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.0.4 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.0.3 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.0.2 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.0.1 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.0.0 lib/fiona7/shadow_classes.rb