combination.rb revision 22607ad2617ae07db8df3478c1e3c5bede481c91
7cb128dc4cae2a03a742f63ba7afee23c78e3af0Phil Carmodyclass Combination < FakeRecord
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen DEFAULT_NAME = 'combinations.dol'
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen DEFAULT_COMBINATION_NAME = 'combination'
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen attr_reader :nodes
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen attr_reader :target_repository, :error, :user
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen validate :nodes_is_collection, :nodes_is_not_empty
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen def self.combined_ontology!(*args)
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen create!(*args).ontology
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen end
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen def self.create!(*args)
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen combination = new(*args)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen combination.save!
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen combination
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen end
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
22627da0fb77c1d0d9a8e8bc485ef5540b6f2e69Timo Sirainen def initialize(user, target_repository, combination_hash)
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen @user = user
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen @target_repository = Repository.find(target_repository)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen from_combination_hash(combination_hash.with_indifferent_access)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen end
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen def save!
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen raise RecordNotSavedError unless valid?
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ontology
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen end
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen def file_name
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen @file_name ||= DEFAULT_NAME
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen end
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen def commit_message
1d132fe27d010b73aacc605b4c6257b0079f9e97Timo Sirainen @commit_message ||= commit_message_erb.result(binding)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen end
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen def combination_name
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen @combination_name ||= DEFAULT_COMBINATION_NAME
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen end
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen def ontology
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen @ontology ||= create_ontology!
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen end
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen private
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen def from_combination_hash(hash)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen @nodes = hash.fetch(:nodes, [])
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen @file_name = hash[:file_name]
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen @commit_message = hash[:commit_message]
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen @combination_name = hash[:combination_name]
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen end
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen def create_ontology!
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen repository_file = build_repository_file
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen repository_file.save!
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen target_repository.ontologies.
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen with_path(repository_file.target_path).
68f0dfb4b2815ecbc1bd8d8a68adcfd577ec55aeTimo Sirainen without_parent.first
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen end
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen def build_repository_file
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen target_directory = File.dirname(file_name)
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen target_directory = nil if target_directory == '.'
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen target_filename = File.basename(file_name)
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen file = RepositoryFile.new \
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen temp_file: dol_tempfile,
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen target_directory: target_directory,
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen target_filename: target_filename,
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen repository_id: target_repository.path,
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen repository_file: {repository_id: target_repository.path},
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen user: user,
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen message: commit_message
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen end
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
0db5b158a00c08955bdacc99b1e2cd1ec07f4311Timo Sirainen def named_nodes
0db5b158a00c08955bdacc99b1e2cd1ec07f4311Timo Sirainen nodes.map do |node|
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen [node.split('/').last.split('?').first, node]
d16b506f5540e3407d256bda35624b38a5ecf88fTimo Sirainen end
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen end
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen def dol_tempfile
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen file = Tempfile.new(file_name)
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen file.write(dol_representation_erb.result(binding))
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen file.rewind
554c1c792dc6fce1e25c74555c2da786bffde75fTimo Sirainen file
554c1c792dc6fce1e25c74555c2da786bffde75fTimo Sirainen end
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen def dol_representation_erb
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen template_file = Rails.root.join('lib/combinations/dol.erb')
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen ERB.new(template_file.read, nil, '<>')
6df2db16b3920346ed07cefb86e8bdcb7e1faec5Timo Sirainen end
6df2db16b3920346ed07cefb86e8bdcb7e1faec5Timo Sirainen
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen def commit_message_erb
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen template_file = Rails.root.join('lib/combinations/commit_message.erb')
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen ERB.new(template_file.read, nil, '>')
35f3b7e05afecacd0332c210c6e253911c2813d8Timo Sirainen end
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen def nodes_is_collection
35f3b7e05afecacd0332c210c6e253911c2813d8Timo Sirainen unless nodes.is_a?(Enumerable)
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen errors.add(:nodes, 'is not an array/collection')
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen end
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen end
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen def nodes_is_not_empty
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen unless nodes.present?
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen errors.add(:nodes, 'should be set and contain URIs')
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen end
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen end
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainenend
e8db44d3d542a8d29500ddfc0e7b51004345af36Timo Sirainen