Class: Google::Cloud::Datastore::Properties

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/datastore/properties.rb

Overview

Properties

Hash-like data structure for Datastore properties.

See Entity#properties

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties = {}) ⇒ Properties

Returns a new instance of Properties



28
29
30
31
32
33
34
35
# File 'lib/google/cloud/datastore/properties.rb', line 28

def initialize properties = {}
  @hash = {}
  properties.each do |key, value|
    key   = ensure_key_type key
    value = ensure_value_type value
    @hash[key] = value
  end
end

Class Method Details

.from_grpc(grpc_map) ⇒ Object



80
81
82
83
# File 'lib/google/cloud/datastore/properties.rb', line 80

def self.from_grpc grpc_map
  # Convert to Hash of string keys and raw values.
  new Hash[grpc_map.map { |k, v| [k.to_s, Convert.from_value(v)] }]
end

Instance Method Details

#[](key) ⇒ Object Also known as: read



37
38
39
40
# File 'lib/google/cloud/datastore/properties.rb', line 37

def [] key
  key = ensure_key_type key
  @hash[key]
end

#[]=(key, value) ⇒ Object Also known as: write



43
44
45
46
47
# File 'lib/google/cloud/datastore/properties.rb', line 43

def []= key, value
  key   = ensure_key_type key
  value = ensure_value_type value
  @hash[key] = value
end

#delete(key, &block) ⇒ Object



65
66
67
68
# File 'lib/google/cloud/datastore/properties.rb', line 65

def delete key, &block
  key = ensure_key_type key
  @hash.delete key, &block
end

#each(&block) ⇒ Object



61
62
63
# File 'lib/google/cloud/datastore/properties.rb', line 61

def each &block
  @hash.each(&block)
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/google/cloud/datastore/properties.rb', line 50

def exist? key
  key = ensure_key_type key
  @hash.key? key
end

#fetch(key, &_block) ⇒ Object



55
56
57
58
59
# File 'lib/google/cloud/datastore/properties.rb', line 55

def fetch key, &_block
  key = ensure_key_type key
  @hash[key] = yield unless exist? key
  @hash[key]
end

#to_grpcObject



75
76
77
78
# File 'lib/google/cloud/datastore/properties.rb', line 75

def to_grpc
  # Convert to Hash with Google::Datastore::V1::Value values.
  Hash[@hash.map { |k, v| [k.to_s, Convert.to_value(v)] }]
end

#to_hObject Also known as: to_hash



70
71
72
# File 'lib/google/cloud/datastore/properties.rb', line 70

def to_h
  @hash.dup
end