Class: CookbookBumper::EnvFile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cookbook_bumper/envfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ EnvFile

Returns a new instance of EnvFile.



11
12
13
14
15
# File 'lib/cookbook_bumper/envfile.rb', line 11

def initialize(path)
  @path = path
  @env = parse(File.read(path))
  @log = []
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



8
9
10
# File 'lib/cookbook_bumper/envfile.rb', line 8

def log
  @log
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/cookbook_bumper/envfile.rb', line 8

def path
  @path
end

Instance Method Details

#[](cookbook) ⇒ Object



17
18
19
# File 'lib/cookbook_bumper/envfile.rb', line 17

def [](cookbook)
  cookbook_versions[cookbook]
end

#[]=(cookbook, version) ⇒ Object



21
22
23
# File 'lib/cookbook_bumper/envfile.rb', line 21

def []=(cookbook, version)
  cookbook_versions[cookbook] = version
end

#cleanObject



59
60
61
62
63
64
65
66
67
# File 'lib/cookbook_bumper/envfile.rb', line 59

def clean
  each do |cookbook_name, version|
    # metadata wasn't found or metadata was found using a different name
    if CookbookBumper.cookbooks[cookbook_name].nil? || CookbookBumper.cookbooks[cookbook_name].name != cookbook_name
      log_change(cookbook_name, version, nil)
      delete(cookbook_name)
    end
  end
end

#cookbook_versionsObject



29
30
31
# File 'lib/cookbook_bumper/envfile.rb', line 29

def cookbook_versions
  @env['cookbook_versions']
end

#deep_sort(obj = @env) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/cookbook_bumper/envfile.rb', line 69

def deep_sort(obj = @env)
  if obj.is_a?(Hash)
    obj.sort.map { |k, v| [k, deep_sort(v)] }.to_h
  else
    obj
  end
end

#log_change(cookbook_name, old_ver, new_ver) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cookbook_bumper/envfile.rb', line 37

def log_change(cookbook_name, old_ver, new_ver)
  action = if old_ver.nil?
             'Added'
           elsif new_ver.nil?
             'Deleted'
           elsif CookbookBumper.cookbooks[cookbook_name].bumped?
             'Bumped'
           else
             'Updated'
           end
  @log << [cookbook_name, action, old_ver, new_ver]
end

#nameObject



25
26
27
# File 'lib/cookbook_bumper/envfile.rb', line 25

def name
  @env['name']
end

#saveObject



77
78
79
# File 'lib/cookbook_bumper/envfile.rb', line 77

def save
  File.write(path, self)
end

#to_sObject



33
34
35
# File 'lib/cookbook_bumper/envfile.rb', line 33

def to_s
  JSON.pretty_generate(deep_sort)
end

#updateObject



50
51
52
53
54
55
56
57
# File 'lib/cookbook_bumper/envfile.rb', line 50

def update
  CookbookBumper.cookbooks.each do |cookbook|
    if cookbook.version != self[cookbook.name]
      log_change(cookbook.name, self[cookbook.name], cookbook.version)
      self[cookbook.name] = cookbook.version.exact
    end
  end
end