maroon 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,119 @@
1
+ require './lib/maroon'
2
+ require './lib/maroon/kernel'
3
+
4
+ context :Meter, :current_total do
5
+ role :price_per_sec
6
+
7
+ role :route do
8
+ price do
9
+ route.calculate_price clock.start
10
+ end
11
+ end
12
+
13
+ role :clock do
14
+ price do
15
+ self.duration * price_per_sec
16
+ end
17
+ end
18
+
19
+ current_total do |current_position|
20
+ route.update_position current_position
21
+ clock.price + route.price
22
+ end
23
+ print_route do
24
+ route.each_point {|x,y|
25
+ p "#{x},#{y}"
26
+ }
27
+ end
28
+ end
29
+
30
+ class Meter
31
+ def initialize(start,start_pos)
32
+ @clock = Clock.new start
33
+ @route = Route.new({0=>{1=>1.25}},Road_types.new)
34
+ route.update_position start_pos
35
+ @price_per_sec = 0.05
36
+ end
37
+ end
38
+
39
+ class Clock
40
+ attr_reader :start
41
+ def initialize(start)
42
+ @start = start
43
+ end
44
+ def duration
45
+ (Time::now - @start)
46
+ end
47
+ end
48
+
49
+ context :Route do
50
+ role :prices do end
51
+ role :payable_position do
52
+ price_from do |prev|
53
+ return 0 unless prev
54
+ delta = Math.sqrt((prev.x-self.x)**2 + (prev.y-self.y)**2 + (prev.z-self.z)**2)
55
+ road_type = @road_types[self]
56
+ price = prices[road_type]
57
+ delta * price
58
+ end
59
+ end
60
+
61
+ role :positions do
62
+ price_for_route do |price_table|
63
+ prev = nil
64
+ sum = 0
65
+ positions.each do |pos|
66
+ bind pos=>:payable_position, price_table => :prices
67
+ sum += pos.price_from prev
68
+ prev = pos
69
+ end
70
+ sum
71
+ end
72
+ end
73
+ update_position do |new_position|
74
+ positions << new_position
75
+ end
76
+ calculate_price do |start_time|
77
+ price_table = prices[start_time.hour / (24/prices.length)]
78
+ positions.price_for_route price_table
79
+ end
80
+ end
81
+
82
+ class Route
83
+ def initialize(prices, road_types)
84
+ @positions = []
85
+ @prices = prices
86
+ @road_types = road_types
87
+ end
88
+ end
89
+
90
+ class Road_types
91
+ def initialize
92
+ @road_types = {}
93
+ end
94
+ def []=(pos,road_type)
95
+ t = @road_types[pos.x] ||= {}
96
+ t = t[pos.y] ||= {}
97
+ t[pos.z] = road_type
98
+ end
99
+ def [](pos)
100
+ t = @road_types[pos.x]
101
+ return 1 unless t
102
+ t = t[pos.y]
103
+ return 1 unless t
104
+ t = t[pos.z]
105
+ return 1 unless t
106
+ t
107
+ end
108
+ end
109
+
110
+ class Position
111
+ attr_reader :x
112
+ attr_reader :y
113
+ attr_reader :z
114
+ def initialize(x,y,z)
115
+ @x = x
116
+ @y = y
117
+ @z = z
118
+ end
119
+ end
data/Gemfile CHANGED
File without changes
data/README.md CHANGED
File without changes
@@ -3,6 +3,7 @@ require './lib/maroon.rb'
3
3
  require './lib/maroon/kernel.rb'
4
4
  require 'ripper'
5
5
  require './Test/source_assertions.rb'
6
+ require './Examples/meter.rb'
6
7
 
7
8
 
8
9
  class BasicTests < Test::Unit::TestCase
@@ -185,6 +186,14 @@ class TestExamples < Test::Unit::TestCase
185
186
  end
186
187
  end
187
188
 
189
+ class TestExamples < Test::Unit::TestCase
190
+ def test_meter_example
191
+ meter = Meter.new Time::now, Position.new(1,2,0)
192
+ result = meter.call Position.new(2,4,1)
193
+ assert_equal(3,result.to_i)
194
+ end
195
+ end
196
+
188
197
 
189
198
 
190
199
 
File without changes
File without changes
File without changes
File without changes
@@ -1,3 +1,3 @@
1
1
  module Maroon
2
- VERSION = '0.6.0'
2
+ VERSION = '0.6.1'
3
3
  end
@@ -61,6 +61,7 @@ module Rewriter
61
61
  #Calls rewrite_block if needed and will return true if the AST was changed otherwise false
62
62
  ##
63
63
  def rewrite_bind?(block, expr)
64
+ changed = false
64
65
  #check if the first call is a bind call
65
66
  if expr && expr.length && (expr[0] == :call && expr[1] == nil && expr[2] == :bind)
66
67
  argument_list = expr[3]
@@ -84,20 +85,20 @@ module Rewriter
84
85
  raise "#{aliased_role} used in binding is an unknown role #{roles}" unless aliased_role.instance_of? Symbol and @roles.has_key? aliased_role
85
86
  add_alias local, aliased_role
86
87
  #replace bind call with assignment of iteration variable to role field
87
- rewrite_bind(aliased_role, local, block)
88
- return true
88
+ do_rewrite_bind(aliased_role, local, block)
89
+ changed = true
89
90
  end
90
91
  end
91
92
  end
92
93
  end
93
- false
94
+ changed
94
95
  end
95
96
 
96
97
  ##
97
98
  #removes call to bind in a block
98
99
  #and replaces it with assignment to the proper role player local variables
99
100
  #in the end of the block the local variables have their original values reassigned
100
- def rewrite_bind(aliased_role, local, block)
101
+ def do_rewrite_bind(aliased_role, local, block)
101
102
  raise 'aliased_role must be a Symbol' unless aliased_role.instance_of? Symbol
102
103
  raise 'local must be a Symbol' unless local.instance_of? Symbol
103
104
  aliased_field = "@#{aliased_role}".to_sym
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maroon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-26 00:00:00.000000000 Z
12
+ date: 2013-02-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sourcify
@@ -73,6 +73,7 @@ files:
73
73
  - Examples/Dijkstra/dijkstra.rb
74
74
  - Examples/MoneyTransfer.rb
75
75
  - Examples/greeter.rb
76
+ - Examples/meter.rb
76
77
  - Gemfile
77
78
  - LICENSE.txt
78
79
  - README.md
@@ -105,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
106
  version: '0'
106
107
  requirements: []
107
108
  rubyforge_project:
108
- rubygems_version: 1.8.23
109
+ rubygems_version: 1.8.24
109
110
  signing_key:
110
111
  specification_version: 3
111
112
  summary: maroon