Sha256: d001ce4caf3047a7cbf5a6691c181d2347d5a2159326f3f43d1bfc64e2a6e7b7

Contents?: true

Size: 886 Bytes

Versions: 43

Compression:

Stored size: 886 Bytes

Contents

module WithRandomizations
  extend ActiveSupport::Concern

  included do
    serialize :randomizations, Hash
    validate :ensure_randomizations_format
  end

  def seed
    @seed || 0
  end

  def seed_with!(seed)
    @seed = seed
  end

  def randomizer
    #TODO remove this hack after removing seed state from here
    @randomizer ||= (Mumukit::Randomizer.parse(randomizations) rescue Mumukit::Randomizer.new([]))
  end

  private

  def ensure_randomizations_format
    errors.add :randomizations,
               :invalid_format unless Mumukit::Randomizer.valid? randomizations.to_h
  end

  module ClassMethods
    def randomize(*selectors)
      selectors.each { |selector| randomize_field selector }
    end

    private

    def randomize_field(selector)
      define_method(selector) do |*args|
        super(*args).randomize_with randomizer, seed
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
mumuki-domain-9.23.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.22.1 app/models/concerns/with_randomizations.rb
mumuki-domain-9.22.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.21.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.20.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.19.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.18.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.17.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.16.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.15.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.14.1 app/models/concerns/with_randomizations.rb
mumuki-domain-9.14.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.13.1 app/models/concerns/with_randomizations.rb
mumuki-domain-9.13.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.12.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.11.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.10.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.9.0 app/models/concerns/with_randomizations.rb
mumuki-domain-9.8.1 app/models/concerns/with_randomizations.rb
mumuki-domain-9.8.0 app/models/concerns/with_randomizations.rb