Class: Kubert::FileAccess

Inherits:
Object
  • Object
show all
Defined in:
lib/kubert/file_access.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, key = nil) ⇒ FileAccess

Returns a new instance of FileAccess.



4
5
6
7
8
9
10
# File 'lib/kubert/file_access.rb', line 4

def initialize(type, key = nil)
  @type = type
  @key = key
  @s3_path = Kubert.public_send("s3_#{type}_path")
  @file_name = Kubert.public_send("#{type}_file_name")
  @data = read
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/kubert/file_access.rb', line 3

def data
  @data
end

Instance Method Details

#clean_localObject



46
47
48
# File 'lib/kubert/file_access.rb', line 46

def clean_local
  File.delete(local_path) unless local?
end

#foundObject



12
13
14
# File 'lib/kubert/file_access.rb', line 12

def found
  data[:data].select {|k, _v| k == key }
end

#getObject



29
30
31
# File 'lib/kubert/file_access.rb', line 29

def get
  data[:data][key]
end

#local?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/kubert/file_access.rb', line 33

def local?
  !s3_path
end

#readObject



16
17
18
# File 'lib/kubert/file_access.rb', line 16

def read
  YAML.load(file_read).with_indifferent_access
end

#set(value) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/kubert/file_access.rb', line 20

def set(value)
  if value.nil?
    data[:data].delete(key)
  else
    data[:data][key] = value
  end
  self
end

#writeObject



37
38
39
40
# File 'lib/kubert/file_access.rb', line 37

def write
  return write_local if local?
  s3_object.put(body: data.to_plain_yaml, content_encoding: 'application/octet-stream', content_type: "text/vnd.yaml", metadata: {author: `whoami`})
end

#write_localObject



42
43
44
# File 'lib/kubert/file_access.rb', line 42

def write_local
  File.write(local_path, data.to_plain_yaml)
end