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-1.2.0.0.0 lib/fiona7/shadow_classes.rb
infopark_fiona7-1.1.0.0.9 lib/fiona7/shadow_classes.rb
infopark_fiona7-1.1.0.0.8 lib/fiona7/shadow_classes.rb
infopark_fiona7-1.1.0.0.7 lib/fiona7/shadow_classes.rb
infopark_fiona7-1.1.0.0.6 lib/fiona7/shadow_classes.rb
infopark_fiona7-1.1.0.0.5 lib/fiona7/shadow_classes.rb
infopark_fiona7-1.1.0.0.4 lib/fiona7/shadow_classes.rb
infopark_fiona7-1.1.0.0.3 lib/fiona7/shadow_classes.rb
infopark_fiona7-1.1.0.0.2 lib/fiona7/shadow_classes.rb
infopark_fiona7-1.1.0.0.10 lib/fiona7/shadow_classes.rb
infopark_fiona7-1.1.0.0.1 lib/fiona7/shadow_classes.rb
infopark_fiona7-1.1.0.0.0 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.1.9 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.1.8 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.1.7 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.1.6 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.1.5 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.1.4 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.1.3 lib/fiona7/shadow_classes.rb
infopark_fiona7-0.71.1.2 lib/fiona7/shadow_classes.rb