Class: GvcsFx::DataStore::DefaultDataStore

Inherits:
Object
  • Object
show all
Includes:
Antrapol::ToolRack::ExceptionUtils
Defined in:
lib/store.rb

Constant Summary collapse

FILENAME =
"workspaces_tr.yml"
DEFPATH =
File.join(GvcsFx::GVCSFX_STORE_ROOT, FILENAME)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefaultDataStore

Returns a new instance of DefaultDataStore.



44
45
46
47
48
49
# File 'lib/store.rb', line 44

def initialize
  @workspaces = { }
  @wsRef = { }
  @paths = []
  @parent = []
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



43
44
45
# File 'lib/store.rb', line 43

def parent
  @parent
end

#pathsObject (readonly)

Returns the value of attribute paths.



43
44
45
# File 'lib/store.rb', line 43

def paths
  @paths
end

#workspacesObject (readonly)

Returns the value of attribute workspaces.



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

def workspaces
  @workspaces
end

#wsListObject (readonly)

Returns the value of attribute wsList.



43
44
45
# File 'lib/store.rb', line 43

def wsList
  @wsList
end

Class Method Details

.load(path = DEFPATH) ⇒ Object



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
# File 'lib/store.rb', line 65

def self.load(path = DEFPATH)
  if not (path.nil? or path.empty?)
    begin
      File.open(path,"r") do |f|
        @cont = f.read
      end

      obj = YAML.load(@cont)

      obj.parent.sort!

      # sort the workspace's path
      obj.workspaces.each do |k,v|
        obj.workspaces[k] = v.sort_by(&:path)
      end

      obj

    rescue Exception => ex
      Global.instance.logger.error ex.message
      Global.instance.logger.error "Failed to load store from '#{path}'. Returning new instance."
      DefaultDataStore.new
    end
  else
    DefaultDataStore.new
  end
end

Instance Method Details

#add_workspace(parent, path) ⇒ Object

Raises:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/store.rb', line 112

def add_workspace(parent, path)
  raise GvcsFxException, "Parent cannot be empty" if parent.nil? or parent.empty?
  raise GvcsFxException, "Path cannot be empty" if path.nil? or path.empty?


  if not @paths.include?(path) 
    if not @workspaces.keys.include?(parent)
      @workspaces[parent] = []
    end

    ws = Workspace.new(path, parent)
    @workspaces[parent] << ws 
    # todo sort the array of parent... but the array contains object
    # custom sorter is required
    @paths << path
    @wsRef[path] = ws
  end
end

#delete_project(proj) ⇒ Object



99
100
101
102
103
104
# File 'lib/store.rb', line 99

def delete_project(proj)
  if not_empty?(proj) and @workspaces.keys.include?(proj)
    @workspaces.delete(proj)
    @parent.delete(proj)
  end
end

#is_project_empty?(proj) ⇒ Boolean

is workspace_registered?

Returns:

  • (Boolean)


145
146
147
# File 'lib/store.rb', line 145

def is_project_empty?(proj)
  @workspaces[proj].length == 0
end

#is_project_registered?(proj) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
# File 'lib/store.rb', line 106

def is_project_registered?(proj)
  if not_empty?(proj)
    @workspaces.keys.include?(proj)
  end
end

#is_workspace_registered?(path) ⇒ Boolean

remove_workspace

Returns:

  • (Boolean)


141
142
143
# File 'lib/store.rb', line 141

def is_workspace_registered?(path)
  @paths.include?(path.strip)
end

#new_project(proj) ⇒ Object



93
94
95
96
97
# File 'lib/store.rb', line 93

def new_project(proj)
  if not_empty?(proj)
    @workspaces[proj] = []
  end
end

#remove_workspace(path) ⇒ Object

add_workspace

Raises:



131
132
133
134
135
136
137
138
139
# File 'lib/store.rb', line 131

def remove_workspace(path)
  raise GvcsFxException, "Path cannot be empty" if path.nil? or path.empty?

  ws = @wsRef[path]
  @workspaces[ws.parent].delete(ws)
  @paths.delete(path)
  @wsRef.delete(path)

end

#store(path = DEFPATH) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/store.rb', line 51

def store(path = DEFPATH)
  if not (path.nil? or path.empty?)
    if not File.exist?(File.dirname(path))
      FileUtils.mkdir_p File.dirname(path)
    end

    @parent = @workspaces.keys.sort

    File.open(path,"w") do |f|
      f.write YAML.dump(self)
    end
  end
end