mumukit-randomizer 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e93122f713ba49c9b158ce97cd16d1f7c8bdc0d8625f0db2c77e466b0516a98c
4
- data.tar.gz: 3f9cb16111ec473e70573646add6039b99da3c1b110fbacd584cb7b6825c2b15
3
+ metadata.gz: 52ed83471259c6d36ec21acdbd96fcd1962b065ff8400ea106f6b284d5b698a0
4
+ data.tar.gz: fcefa5da889b330e0eeb5f3d60b239d09849faae09eb25d407bf9f990e90723b
5
5
  SHA512:
6
- metadata.gz: 07d7b2c6ef4e30255e9a908c06b4aa3e40a09067e79b6a3f34c070803ecd6922880598d58dbd93352914f86c6ca4e9a0a222c7891078cde9a6202eb1f13c0fa7
7
- data.tar.gz: 749247a7c4ab9a09585310cb9aa8edc72e05a94441162e4a22f3c7b32616fa6f3161930b437cb15bb692593755359998b0ba6eb74059d23d3d989038b5450eeb
6
+ metadata.gz: 33d9d7996dba267891953d64f1e54294fbd3f5a03fdc624a7876c25d8045dfe48297b553fdbd960c512c5f46f9a695e3c1934bceb3b6eb8853867882950e5fcb
7
+ data.tar.gz: a9b529943b5cf3b01653265074fd46c782a436fe70195bcb00a50a5577e2afc537df8c32311fee5e1ae72596436ecd61fec438239ca2e79330c171adcf96f4eb
@@ -8,4 +8,8 @@ class Mumukit::Randomizer::Randomization::Base
8
8
  def modulo(value, range)
9
9
  Random.new(value).rand(range)
10
10
  end
11
+
12
+ def evaluate(seed, values)
13
+ get seed
14
+ end
11
15
  end
@@ -0,0 +1,22 @@
1
+ class Mumukit::Randomizer::Randomization::Expression < Mumukit::Randomizer::Randomization::Base
2
+
3
+ def initialize(keisan_code)
4
+ Keisan::AST.parse keisan_code
5
+
6
+ @keisan_code = keisan_code
7
+ @calculator = new_calculator
8
+ rescue Keisan::Exceptions::BaseError => e
9
+ raise Mumukit::Randomizer::RandomizationFormatError, "Malformed randomization expression: #{e.message}. See docs here https://github.com/project-eutopia/keisan"
10
+ end
11
+
12
+ def new_calculator
13
+ Keisan::Calculator.new.tap do |calculator|
14
+ calculator.define_function! "to_s", proc {|x| x.to_s }
15
+ calculator.define_function! "to_i", proc {|x| x.to_i }
16
+ end
17
+ end
18
+
19
+ def evaluate(seed, values)
20
+ @calculator.evaluate(@keisan_code, {seed: seed}.merge(values.to_h))
21
+ end
22
+ end
@@ -9,7 +9,8 @@ module Mumukit::Randomizer::Randomization
9
9
  case randomization[:type].to_sym
10
10
  when :one_of then Mumukit::Randomizer::Randomization::OneOf.new randomization[:value]
11
11
  when :range then Mumukit::Randomizer::Randomization::Range.new(*randomization[:value])
12
- else raise 'Unsupported randomization kind'
12
+ when :expression then Mumukit::Randomizer::Randomization::Expression.new(*randomization[:value])
13
+ else raise Mumukit::Randomizer::RandomizationFormatError, 'Unsupported randomization kind'
13
14
  end
14
15
  end
15
16
  end
@@ -1,7 +1,7 @@
1
1
  class Mumukit::Randomizer::Randomization::Range < Mumukit::Randomizer::Randomization::Base
2
2
  def initialize(lower_bound, upper_bound)
3
- raise 'lower bound must be Numeric' unless lower_bound.is_a?(Numeric)
4
- raise 'upper bound must be Numeric' unless upper_bound.is_a?(Numeric)
3
+ raise Mumukit::Randomizer::RandomizationFormatError, 'lower bound must be Numeric' unless lower_bound.is_a?(Numeric)
4
+ raise Mumukit::Randomizer::RandomizationFormatError, 'upper bound must be Numeric' unless upper_bound.is_a?(Numeric)
5
5
  @choices = Range.new lower_bound, upper_bound
6
6
  end
7
7
 
@@ -1,5 +1,5 @@
1
1
  module Mumukit
2
2
  class Randomizer
3
- VERSION = '1.1.0'
3
+ VERSION = '1.2.0'
4
4
  end
5
5
  end
@@ -5,21 +5,38 @@ module Mumukit
5
5
  end
6
6
 
7
7
  def with_seed(seed)
8
- @randomizations.each_with_index.map { |(key, value), index| [key, value.get(seed + index)] }
8
+ values = []
9
+ @randomizations.each_with_index do |(key, value), index|
10
+ values.push([key, value.evaluate(seed + index, values)])
11
+ end
12
+ values
13
+ end
14
+
15
+ def randomized_values(seed)
16
+ with_seed(seed).to_h
9
17
  end
10
18
 
11
19
  def randomize!(field, seed)
12
- with_seed(seed).inject(field) { |result, (replacee, replacer)| result.gsub "$#{replacee}", replacer.to_s }
20
+ with_seed(seed).inject(field) do |result, (replacee, replacer)|
21
+ result.gsub "$#{replacee}", replacer.to_s
22
+ end
13
23
  end
14
24
 
25
+ alias randomize randomize!
26
+
15
27
  def self.parse(randomizations)
16
28
  new randomizations.with_indifferent_access.transform_values { |it| Mumukit::Randomizer::Randomization.parse it }
17
29
  end
30
+
31
+ class RandomizationFormatError < StandardError
32
+ end
18
33
  end
19
34
  end
20
35
 
36
+ require 'keisan'
21
37
  require_relative 'randomizations/randomization'
22
38
  require_relative 'randomizations/base'
23
39
  require_relative 'randomizations/one_of'
24
40
  require_relative 'randomizations/range'
41
+ require_relative 'randomizations/expression'
25
42
  require_relative 'randomizer/version'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mumukit-randomizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julián Berbel Alt
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-08-10 00:00:00.000000000 Z
12
+ date: 2021-09-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -17,28 +17,28 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '1.16'
20
+ version: '2.0'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '1.16'
27
+ version: '2.0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '10.0'
34
+ version: '12.3'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '10.0'
41
+ version: '12.3'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rspec
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -67,6 +67,20 @@ dependencies:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: keisan
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.9'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0.9'
70
84
  description:
71
85
  email:
72
86
  - julian@mumuki.org
@@ -76,6 +90,7 @@ extensions: []
76
90
  extra_rdoc_files: []
77
91
  files:
78
92
  - lib/mumukit/randomizations/base.rb
93
+ - lib/mumukit/randomizations/expression.rb
79
94
  - lib/mumukit/randomizations/one_of.rb
80
95
  - lib/mumukit/randomizations/randomization.rb
81
96
  - lib/mumukit/randomizations/range.rb
@@ -100,8 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
115
  - !ruby/object:Gem::Version
101
116
  version: '0'
102
117
  requirements: []
103
- rubyforge_project:
104
- rubygems_version: 2.7.7
118
+ rubygems_version: 3.0.3
105
119
  signing_key:
106
120
  specification_version: 4
107
121
  summary: Library for randomizing parts of mumuki exercises