Skip to content

Instantly share code, notes, and snippets.

@amixpal
Created July 2, 2015 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amixpal/920cc6de2195c50273ad to your computer and use it in GitHub Desktop.
Save amixpal/920cc6de2195c50273ad to your computer and use it in GitHub Desktop.
#Simulation Model class
require 'simulator_utility'
require 'randomize_utility'
class Simulation < ActiveRecord::Base
@@data_with_opinion = %w(:soft :none :hard)
@@data_without_opinion = %w(:none)
belongs_to :user
before_create :fill_matrix
validates_presence_of :name, :message => 'Name field cannot be empty..'
validates :row, :inclusion => { :in => 1..10, :message => 'The row must be between 1 and 10' }
validates :col, :inclusion => { :in => 1..10, :message => 'The row must be between 1 and 10' }
def fill_matrix
self.is_deleted = false
self.matrix = generate_matrix_with_random(self.row, self.col, self.generation_type)
self.verdict = ::SimulatorUtility.new(self.matrix).verdict
end
private
def generate_matrix_with_random(row, col, type)
if type == 2
return @@data_with_opinion.repeated_permutation(col).to_a.slice(0, row).shuffle
elsif type == 3
return @@data_without_opinion.repeated_permutation(col).to_a.slice(0, row).shuffle
elsif type == 1
return ::RandomizeMatrix.new(row, col).generate_customized_matrix
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment