<commit_before># Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


require "gcloud/datastore/key"
require "gcloud/datastore/properties"
require "gcloud/datastore/proto"

module Gcloud
  module Datastore
    ##
    # # Entity
    #
    # Entity represents a Datastore record.
    # Every Entity has a {Key}, and a list of properties.
    #
    # @example
    #   entity = Gcloud::Datastore::Entity.new
    #   entity.key = Gcloud::Datastore::Key.new "User", "<EMAIL>"
    #   entity["name"] = "<NAME>"
    #
    class Entity
      ##
      # The Key that identifies the entity.
      attr_reader :key

      ##
      # Create a new Entity object.
      def initialize
        @properties = Properties.new
        @key = Key.new
        @_exclude_indexes = {}
      end

      ##
      # Retrieve a property value by providing the name.
      #
      # @param [String, Symbol] prop_name The name of the property.
      #
      # @return [Object, nil] Returns `nil` if the property doesn't exist
      #
      # @example Properties can be retrieved with a string name:
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   dataset = gcloud.datastore
      #   user = dataset.find "User", "<EMAIL>"
      #   user["name"] #=> "<NAME>"
      #
      # @example Or with a symbol name:
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   dataset = gcloud.datastore
      #   user = dataset.find "User", "<EMAIL>"
      #   user[:name] #=> "<NAME>"
      #
      def [] prop_name
        @properties[prop_name]
      end

      ##
      # Set a property value by name.
      #
      # @param [String, Symbol] prop_name The name of the property.
      # @param [Object] prop_value The value of the property.
      #
      # @example Properties can be set with a string name:
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   dataset = gcloud.datastore
      #   user = dataset.find "User", "<EMAIL>"
      #   user["name"] = "<NAME>"
      #
      # @example Or with a symbol name:
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   dataset = gcloud.datastore
      #   user = dataset.find "User", "<EMAIL>"
      #   user[:name] = "<NAME>"
      #
      def []= prop_name, prop_value
        @properties[prop_name] = prop_value
      end

      ##
      # Retrieve properties in a hash-like structure.
      # Properties can be accessed or set by string or symbol.
      #
      # @return [Gcloud::Datastore::Properties]
      #
      # @example
      #   entity.properties[:name] = "<NAME>"<commit_msg>Add documentation for getting/setting IO/blob<commit_after># Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


require "gcloud/datastore/key"
require "gcloud/datastore/properties"
require "gcloud/datastore/proto"

module Gcloud
  module Datastore
    ##
    # # Entity
    #
    # Entity represents a Datastore record.
    # Every Entity has a {Key}, and a list of properties.
    #
    # @example
    #   entity = Gcloud::Datastore::Entity.new
    #   entity.key = Gcloud::Datastore::Key.new "User", "<EMAIL>"
    #   entity["name"] = "<NAME>"
    #
    class Entity
      ##
      # The Key that identifies the entity.
      attr_reader :key

      ##
      # Create a new Entity object.
      def initialize
        @properties = Properties.new
        @key = Key.new
        @_exclude_indexes = {}
      end

      ##
      # Retrieve a property value by providing the name.
      #
      # Property values are converted from the Datastore value type
      # automatically. Blob properties are returned as StringIO objects.
      #
      # @param [String, Symbol] prop_name The name of the property.
      #
      # @return [Object, nil] Returns `nil` if the property doesn't exist
      #
      # @example Properties can be retrieved with a string name:
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   dataset = gcloud.datastore
      #   user = dataset.find "User", "<EMAIL>"
      #   user["name"] #=> "<NAME>"
      #
      # @example Or with a symbol name:
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   dataset = gcloud.datastore
      #   user = dataset.find "User", "<EMAIL>"
      #   user[:name] #=> "<NAME>"
      #
      # @example Getting a blob value returns a StringIO object:
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   dataset = gcloud.datastore
      #   user = dataset.find "User", "<EMAIL>"
      #   user["avatar"] #=> StringIO("\x89PNG\r\n\x1A...")
      #
      def [] prop_name
        @properties[prop_name]
      end

      ##
      # Set a property value by name.
      #
      # Property values are converted to use the proper Datastore value type
      # automatically. Use an IO-compatible object (File, StringIO, Tempfile) to
      # indicate the property value should be stored as a Datastore `blob`.
      # IO-compatible objects are converted to StringIO objects when they are
      # set.
      #
      # @param [String, Symbol] prop_name The name of the property.
      # @param [Object] prop_value The value of the property.
      #
      # @example Properties can be set with a string name:
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   dataset = gcloud.datastore
      #   user = dataset.find "User", "<EMAIL>"
      #   user["name"] = "<NAME>"
      #
      # @example Or with a symbol name:
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   dataset = gcloud.datastore
      #   user = dataset.find "User", "<EMAIL>"
      #   user[:name] = "<NAME>"
      #
      # @example Setting a blob value using an IO:
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   dataset = gcloud.datastore
      #   user = dataset.find "User", "<EMAIL>"
      #   user["avatar"] = File.open "/avatars/heidi.png"
      #   user["avatar"] #=> StringIO("\x89PNG\r\n\x1A...")
      #
      def []= prop_name, prop_value
        @properties[prop_name] = prop_value
      end

      ##
      # Retrieve properties in a hash-like structure.
      # Properties can be accessed or set by string or symbol.
      #
      # @return [Gcloud::Datastore::Properties]
      #
      # @example
      #   entity.properties[:name] = "<NAME>"
{'fraction_non_alphanumeric': 0.10822510822510822, 'fraction_numerical': 0.002976190476190476, 'mean_word_length': 2.1594017094017093, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6621249', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2173, 'n_words': 712}