Class: RepoManager::AssetConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/repo_manager/assets/asset_configuration.rb

Overview

asset_configuration saves just the user data by subtracting out the global hash and writing just the result

An asset requires a global template but doesn’t require a user datafile.

LOADING:

Load up the master template YAML file first, evaluate the ERB, save this hash for later use.

Copy the results to the asset attributes and then load the user data. The user data doesn’t contain ERB.

SAVING:

Compare existing asset attributes to the saved and eval’d master hash and write just the change assets to the user file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(asset) ⇒ AssetConfiguration

Returns a new instance of AssetConfiguration.



36
37
38
39
# File 'lib/repo_manager/assets/asset_configuration.rb', line 36

def initialize(asset)
  #logger.debug "initializing new AssetConfiguration with asset class: #{asset.class.to_s}"
  @asset = asset
end

Instance Attribute Details

#assetObject (readonly)

Returns the value of attribute asset.



34
35
36
# File 'lib/repo_manager/assets/asset_configuration.rb', line 34

def asset
  @asset
end

#folderObject

user datastore folder, can override parent datastore



29
30
31
# File 'lib/repo_manager/assets/asset_configuration.rb', line 29

def folder
  @folder
end

#parentObject

parent datastore defaults folder, read asset from here first if exists



32
33
34
# File 'lib/repo_manager/assets/asset_configuration.rb', line 32

def parent
  @parent
end

Instance Method Details

#load(ds = nil) ⇒ Object

load an asset from a configuration folder



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/repo_manager/assets/asset_configuration.rb', line 65

def load(ds=nil)
  @folder ||= ds

  contents = load_contents(folder)

  # if a global parent folder is defined, load it first
  parent = contents.delete(:parent) || parent
  if parent
    parent_folder = File.join(parent)
    unless Pathname.new(parent_folder).absolute?
      base_folder = File.dirname(folder)
      parent_folder = File.join(base_folder, parent_folder)
    end

    logger.debug "AssetConfiguration loading parent: #{parent_folder}"
    parent_configuration = RepoManager::AssetConfiguration.new(asset)

    begin
      parent_configuration.load(parent_folder)
    rescue Exception => e
      logger.warn "AssetConfiguration parent configuration load failed on: '#{parent_folder}' with: '#{e.message}'"
    end
  end

  # Load all attributes as hash 'attributes' so that merging
  # and adding new attributes doesn't require code changes. Note
  # that the 'parent' setting is not merged to attributes
  @asset.attributes.merge!(contents)
  @asset.create_accessors(@asset.attributes[:user_attributes])
  @asset
end

#save(attrs = nil) ⇒ Object

Save specific attributes to an asset configuration file. Only the param ‘attrs’ and the current contents of the config file are saved. Parent asset configurations are not saved.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/repo_manager/assets/asset_configuration.rb', line 45

def save(attrs=nil)
  raise "a Hash of attributes to save must be specified" unless attrs && attrs.is_a?(Hash)
  raise "folder must be set prior to saving attributes" unless folder

  # merge attributes to asset that contains parent attributes
  @asset.attributes.merge!(attrs)

  # load contents of the user folder and merge in attributes passed to save
  # so that we don't save parent attributes
  contents = {}
  if File.exists?(folder)
    contents = load_contents(folder)
    raise "expected contents to be a hash" unless contents.is_a?(Hash)
  end

  contents = contents.merge!(attrs)
  write_contents(folder, contents)
end

#to_hashObject



97
98
99
100
101
102
# File 'lib/repo_manager/assets/asset_configuration.rb', line 97

def to_hash
  result = {}
  result.merge!(:parent => parent.folder) if parent
  result.merge!(:attributes => @asset.attributes)
  result
end