Class: Treet::Farm
- Inherits:
-
Object
- Object
- Treet::Farm
- Defined in:
- lib/treet/farm.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#repotype ⇒ Object
readonly
Returns the value of attribute repotype.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#xrefkey ⇒ Object
readonly
Returns the value of attribute xrefkey.
Class Method Summary collapse
-
.plant(opts) ⇒ Object
“plant” a new farm: given an array of hashes (in JSON), create a directory of Treet repositories, one per hash.
Instance Method Summary collapse
-
#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.
- #count ⇒ Object
-
#export ⇒ Object
export as an array, not as a hash the xref for each repo will be included under ‘xref.#xrefkey`.
-
#initialize(opts) ⇒ Farm
constructor
A new instance of Farm.
-
#patch(patches) ⇒ Object
apply patches to a farm of repos.
- #repo(id, opts = {}) ⇒ Object
- #repos(opts = {}) ⇒ Object
- #xrefs ⇒ Object
Constructor Details
Instance Attribute Details
#repotype ⇒ Object (readonly)
Returns the value of attribute repotype.
6 7 8 |
# File 'lib/treet/farm.rb', line 6 def repotype @repotype end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/treet/farm.rb', line 6 def root @root end |
#xrefkey ⇒ Object (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 |
#count ⇒ Object
76 77 78 |
# File 'lib/treet/farm.rb', line 76 def count xrefs.count end |
#export ⇒ Object
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 |
#xrefs ⇒ Object
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 |