mumukit-randomizer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0fd1aa38765b85dbc2615df2164a5541f512c813
4
+ data.tar.gz: b3da26c578ae66ff2e677baa48f6f74dd99c211c
5
+ SHA512:
6
+ metadata.gz: 5e35d10845d56a4bb60a841f716dbcbf9f59ac718ac04f77ed31ca3a1164993bc93297cae07f4e908072b9a538b6276ffe983bc53fb29eb8e7371cb5ec0be0fe
7
+ data.tar.gz: b34175f2a5317f9d05792e834581b8f774d6e743f57235e3cb97c22822e4d95fc3ef19a2aae900afe0ee313ce36f1c3ff8bdda6337064bdd836f6770bd146446
@@ -0,0 +1,11 @@
1
+ class Mumukit::Randomizer::Randomization::Base
2
+ attr_accessor :choices
3
+
4
+ def size
5
+ choices.size
6
+ end
7
+
8
+ def modulo(value, range)
9
+ Random.new(value).rand(range)
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class Mumukit::Randomizer::Randomization::OneOf < Mumukit::Randomizer::Randomization::Base
2
+ def initialize(choices)
3
+ @choices = choices
4
+ end
5
+
6
+ def get(value)
7
+ choices[modulo(value, 0..choices.size - 1)]
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module Mumukit::Randomizer::Randomization
2
+ attr_accessor :randomizations
3
+
4
+ def initialize(randomizations)
5
+ @randomizations = randomizations
6
+ end
7
+
8
+ def self.parse(randomization)
9
+ case randomization[:type].to_sym
10
+ when :oneOf then Mumukit::Randomizer::Randomization::OneOf.new randomization[:value]
11
+ when :range then Mumukit::Randomizer::Randomization::Range.new(*randomization[:value])
12
+ else raise 'Unsupported randomization kind'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ class Mumukit::Randomizer::Randomization::Range < Mumukit::Randomizer::Randomization::Base
2
+ def initialize(lower_bound, upper_bound)
3
+ @choices = Range.new lower_bound, upper_bound
4
+ end
5
+
6
+ def get(value)
7
+ modulo(value, choices.first..choices.last)
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ module Mumukit
2
+ class Randomizer
3
+ def initialize(randomizations)
4
+ @randomizations = randomizations
5
+ end
6
+
7
+ def with_seed(seed)
8
+ @randomizations.each_with_index.map { |(key, value), index| [key, value.get(seed + index)] }
9
+ end
10
+
11
+ def randomize!(field, seed)
12
+ with_seed(seed).inject(field) { |result, (replacee, replacer)| result.gsub "$#{replacee}", replacer.to_s }
13
+ end
14
+
15
+ def self.parse(randomizations)
16
+ new randomizations.with_indifferent_access.transform_values { |it| Mumukit::Randomizer::Randomization.parse it }
17
+ end
18
+ end
19
+ end
20
+
21
+ require_relative 'randomizations/randomization'
22
+ require_relative 'randomizations/base'
23
+ require_relative 'randomizations/one_of'
24
+ require_relative 'randomizations/range'
25
+ require_relative 'randomizer/version'
26
+ require_relative 'randomizer/with_randomizations'
@@ -0,0 +1,5 @@
1
+ module Mumukit
2
+ class Randomizer
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ module WithRandomizations
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ serialize :randomizations, Hash
6
+ end
7
+
8
+ def seed
9
+ @seed || 0
10
+ end
11
+
12
+ def seed_with!(seed)
13
+ @seed = seed
14
+ end
15
+
16
+ def randomizer
17
+ @randomizer ||= Mumukit::Randomizer.parse(randomizations)
18
+ end
19
+
20
+ module ClassMethods
21
+ def randomize(*selectors)
22
+ selectors.each { |selector| randomize_field selector }
23
+ end
24
+
25
+ private
26
+
27
+ def randomize_field(selector)
28
+ define_method(selector) do |*args|
29
+ return unless super(*args)
30
+ randomizer.randomize!(super(*args), seed)
31
+ end
32
+ end
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mumukit-randomizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Julián Berbel Alt
8
+ - Franco Bulgarelli
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-07-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.16'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.16'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: activesupport
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description:
71
+ email:
72
+ - julian@mumuki.org
73
+ - franco@mumuki.org
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - lib/mumukit/randomizations/base.rb
79
+ - lib/mumukit/randomizations/one_of.rb
80
+ - lib/mumukit/randomizations/randomization.rb
81
+ - lib/mumukit/randomizations/range.rb
82
+ - lib/mumukit/randomizer.rb
83
+ - lib/mumukit/randomizer/version.rb
84
+ - lib/mumukit/randomizer/with_randomizations.rb
85
+ homepage: http://github.com/mumuki/mumukit-randomizer
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.5.1
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Library for randomizing parts of mumuki exercises
109
+ test_files: []