Class: Treet::Farm

Inherits:
Object
  • Object
show all
Defined in:
lib/treet/farm.rb

Direct Known Subclasses

Gitfarm

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Farm

Returns a new instance of Farm.

Raises:

  • (Errno::ENOENT)


8
9
10
11
12
13
14
# File 'lib/treet/farm.rb', line 8

def initialize(opts)
  raise Errno::ENOENT unless File.directory?(opts[:root])

  @root = opts[:root]
  @xrefkey = opts[:xref]
  @repotype = opts[:repotype] || Treet::Repo
end

Instance Attribute Details

#repotypeObject (readonly)

Returns the value of attribute repotype.



6
7
8
# File 'lib/treet/farm.rb', line 6

def repotype
  @repotype
end

#rootObject (readonly)

Returns the value of attribute root.



6
7
8
# File 'lib/treet/farm.rb', line 6

def root
  @root
end

#xrefkeyObject (readonly)

Returns the value of attribute xrefkey.



6
7
8
# File 'lib/treet/farm.rb', line 6

def xrefkey
  @xrefkey
end

Class Method Details

.plant(opts) ⇒ Object

“plant” a new farm: given an array of hashes (in JSON), create a directory of Treet repositories, one per hash. Generate directory names for each repo.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/treet/farm.rb', line 38

def self.plant(opts)
  jsonfile = opts[:json]
  rootdir = opts[:root]

  array_of_hashes = JSON.load(File.open(jsonfile))
  Dir.chdir(rootdir) do
    array_of_hashes.each do |h|
      uuid = SecureRandom.uuid
      thash = Treet::Hash.new(h)
      thash.to_repo(uuid, opts)
    end
  end

  self.new(opts)
end

Instance Method Details

#add(hash, opts = {}) ⇒ Object

add a new repo, with data from an input hash if an :id is provided, then the new repo will be stored under that directory name, otherwise a unique id will be generated



64
65
66
67
68
# File 'lib/treet/farm.rb', line 64

def add(hash, opts = {})
  uuid = opts[:id] || SecureRandom.uuid
  thash = Treet::Hash.new(hash)
  repos[uuid] = thash.to_repo("#{root}/#{uuid}", opts.merge(:repotype => repotype))
end

#countObject



76
77
78
# File 'lib/treet/farm.rb', line 76

def count
  xrefs.count
end

#exportObject

export as an array, not as a hash the xref for each repo will be included under ‘xref.#xrefkey`



32
33
34
# File 'lib/treet/farm.rb', line 32

def export
  repos.map {|xref,repo| repo.to_hash}
end

#patch(patches) ⇒ Object

apply patches to a farm of repos



55
56
57
58
59
# File 'lib/treet/farm.rb', line 55

def patch(patches)
  patches.map do |k,diffs|
    repos[k].patch(diffs)
  end
end

#repo(id, opts = {}) ⇒ Object



23
24
25
26
27
28
# File 'lib/treet/farm.rb', line 23

def repo(id, opts = {})
  repotype.new("#{root}/#{id}", opts)
rescue Errno::ENOENT
  # no such repository exists
  nil
end

#repos(opts = {}) ⇒ Object



16
17
18
19
20
21
# File 'lib/treet/farm.rb', line 16

def repos(opts = {})
  xrefs.each_with_object({}) do |subdir,h|
    # in a Farm we are looking for repositories under the root
    h[subdir] = repo(subdir, opts)
  end
end

#xrefsObject



70
71
72
73
74
# File 'lib/treet/farm.rb', line 70

def xrefs
  Dir.chdir(root) do
    Dir.glob("*").select {|f| File.directory?(f)}
  end
end