Class: Gcloud::Datastore::Properties

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/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.



27
28
29
30
31
32
33
34
# File 'lib/gcloud/datastore/properties.rb', line 27

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



78
79
80
81
82
83
# File 'lib/gcloud/datastore/properties.rb', line 78

def self.from_grpc grpc_map
  # For some reason Google::Protobuf::Map#map isn't returning the value.
  # It returns nil every time. COnvert to Hash to get actual objects.
  grpc_hash = GRPCUtils.map_to_hash grpc_map
  new Hash[grpc_hash.map { |(k, v)| [k.to_s, GRPCUtils.from_value(v)] }]
end

Instance Method Details

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



36
37
38
39
# File 'lib/gcloud/datastore/properties.rb', line 36

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

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



42
43
44
45
46
# File 'lib/gcloud/datastore/properties.rb', line 42

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

#delete(key, &block) ⇒ Object



64
65
66
67
# File 'lib/gcloud/datastore/properties.rb', line 64

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

#each(&block) ⇒ Object



60
61
62
# File 'lib/gcloud/datastore/properties.rb', line 60

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

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/gcloud/datastore/properties.rb', line 49

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

#fetch(key, &_block) ⇒ Object



54
55
56
57
58
# File 'lib/gcloud/datastore/properties.rb', line 54

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

#to_grpcObject



74
75
76
# File 'lib/gcloud/datastore/properties.rb', line 74

def to_grpc
  Hash[@hash.map { |(k, v)| [k.to_s, GRPCUtils.to_value(v)] }]
end

#to_hObject Also known as: to_hash



69
70
71
# File 'lib/gcloud/datastore/properties.rb', line 69

def to_h
  @hash.dup
end