<commit_before>require 'qml/qt_classes'

module QML
  class Context

    def self.new
      Plugins.core.createContext(Engine.instance)
    end

    def initialize
      super()
      @extension = Plugins.core.createContextExtension(self)
      @context_properties = {}
    end

    def eval(obj, str)
      @extension.evaluate(obj, str)
    end

    def []=(key, value)
      value.prefer_managed true if value.is_a? Wrapper
      # just hold referenece
      @context_properties[key] = value
      @extension.setContextProperty(key, value)
      value
    end

    def [](key)
      @extension.contextProperty(key)
    end

    def self.for_object(obj)
      Plugins.core.contextForObject(obj).tap do |context|
        context.managed = false if context
      end
    end
  end
end<commit_msg>Write Context documentation<commit_after>require 'qml/qt_classes'

module QML

  # {Context} represents QML contexts and used to expose Ruby values to QML.
  # Each context has values that is called "context properties" that can be accessed in QML by their names.
  #
  # This class is automatically created from Qt class QQmlContext.
  # Signals, slots and properties of QQmlContext can also be used in {Context}.
  # (See {http://qt-project.org/doc/qt-5/qqmlcontext.html Qt C++ documentation} for details)
  #
  # @example
  #   QML.application do |app|
  #     app.context[:foo] = 'foo'
  #     app.context[:bar] = 'bar'
  #     ...
  #   end
  class Context

    # Creates a new instance of {Context}.
    def self.new
      Plugins.core.createContext(Engine.instance)
    end

    def initialize
      super()
      @extension = Plugins.core.createContextExtension(self)
      @context_properties = {}
    end

    # Evaluates an JavaScript expression on the object in this context.
    # @param obj The object the expression is evaluated on
    # @param str The JavaScript expression string
    # @see Wrapper#qml_eval
    def eval(obj, str)
      @extension.evaluate(obj, str)
    end

    # Sets a context property.
    # @param key [String|Symbol] The property key
    # @param value The value
    # @return The value
    def []=(key, value)
      value.prefer_managed true if value.is_a? Wrapper
      # just hold referenece
      @context_properties[key] = value
      @extension.setContextProperty(key, value)
      value
    end

    # Gets a context property.
    # @param key [String|Symbol] The property key
    # @return The value
    def [](key)
      @extension.contextProperty(key)
    end

    # Gets a context that an object belongs to. If the object belongs to no context, returns nil.
    # @param obj
    # @return [Context|nil]
    def self.for_object(obj)
      Plugins.core.contextForObject(obj).tap do |context|
        context.managed = false if context
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08802565930149679, 'fraction_numerical': 0.0003563791874554526, 'mean_word_length': 2.6885676741130093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4357537', 'n_tokens_mistral': 847, 'n_tokens_neox': 827, 'n_words': 266}