combination.rb revision e17f3b48ced85fb8b25c7ca5b76cf093d089e264
294N/Aclass Combination < FakeRecord
294N/A DEFAULT_NAME = 'combinations.dol'
787N/A
789N/A attr_reader :nodes
789N/A attr_reader :target_repository, :error
789N/A
1336N/A def initialize(target_repository, combination_hash)
789N/A @target_repository = target_repository
789N/A from_combination_hash(combination_hash)
873N/A end
789N/A
877N/A def save!
789N/A ontology
294N/A rescue StandardError => error
873N/A @error = error
873N/A raise RecordNotSavedError, "Couldn't create combination!"
789N/A end
789N/A
789N/A def file_name
873N/A @file_name ||= DEFAULT_NAME
789N/A end
789N/A
789N/A def commit_message
789N/A @commit_message ||= commit_message_erb.result(binding)
789N/A end
789N/A
789N/A def combination_name
789N/A "combination"
789N/A end
1336N/A
1336N/A def ontology
789N/A @ontology ||= create_ontology!
873N/A end
1336N/A
1336N/A private
1336N/A def from_combination_hash(hash)
1336N/A @nodes = hash.fetch(:nodes, [])
1336N/A @file_name = hash[:file_name]
1336N/A @commit_message = hash[:commit_message]
1336N/A end
1336N/A
1336N/A def create_ontology!
1336N/A repository_file = build_repository_file
1336N/A repository_file.save!
1336N/A target_repository.ontologies.
1336N/A with_path(repository_file.target_path).
1336N/A without_parent.first
1336N/A end
1336N/A
1336N/A def build_repository_file
1336N/A target_directory = File.dirname(file_name)
873N/A target_directory = nil if target_directory == '.'
873N/A target_filename = File.basename(file_name)
873N/A file = RepositoryFile.new \
789N/A temp_file: dol_tempfile,
787N/A target_directory: target_directory,
294N/A target_filename: target_filename,
1336N/A repository_id: target_repository.path,
1336N/A repository_file: {repository_id: target_repository.path},
1336N/A user: User.first,
1336N/A message: commit_message
1337N/A end
294N/A
294N/A def named_nodes
294N/A nodes.map do |node|
1336N/A [node.split('/').last.split('?').first, node]
1336N/A end
1337N/A end
1337N/A
1337N/A def dol_tempfile
1337N/A file = Tempfile.new(file_name)
1337N/A file.write(dol_representation_erb.result(binding))
1337N/A file.rewind
1337N/A file
1337N/A end
1337N/A
1336N/A def dol_representation_erb
1337N/A template_file = Rails.root.join('lib/combinations/dol.erb')
1337N/A ERB.new(template_file.read, nil, '<>')
1337N/A end
294N/A
294N/A def commit_message_erb
294N/A template_file = Rails.root.join('lib/combinations/commit_message.erb')
294N/A ERB.new(template_file.read, nil, '>')
789N/A end
294N/Aend
1109N/A