adt 0.0.3 → 0.0.4

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.
@@ -1,3 +1,8 @@
1
+ 0.0.4
2
+ =====
3
+
4
+ * BUG: Methods defined using the 'operation' DSL could not access other methods in the class.
5
+
1
6
  0.0.3
2
7
  =====
3
8
 
data/lib/adt.rb CHANGED
@@ -220,16 +220,23 @@ module ADT
220
220
  # @param [Proc] The definitions of the implementations for each case.
221
221
  def operation(sym, &definitions)
222
222
  define_method(sym) do |*args|
223
+ the_instance = self
223
224
  dsl = CaseRecorder.new
224
- dsl_cls = class <<dsl; self; end
225
- # This is hax so that we can 'instance_eval' the definitions block on a recorder,
226
- # but in this case the definitions block could have arguments (which are arguments
227
- # to the operation)
228
- dsl_cls.send(:define_method, :_defs, &definitions)
229
- dsl._defs(*args)
225
+ # The definitions block needs to be executed in the context of the recorder, to
226
+ # read the impls.
227
+ dsl.__instance_exec(*args, &definitions)
230
228
  # Now we just turn the [(case_name, impl)] structure into an argument for fold and
231
229
  # are done. Fold with a hash will check that all keys are defined.
232
- fold(dsl._implementations.inject({}) { |memo, (c, impl)| memo[c] = impl; memo })
230
+ fold(dsl._implementations.inject({}) { |memo, (c, impl)|
231
+ # Fucker. if 'impl' is used directly, because of the 'define_method' from earlier,
232
+ # it is evaluated in the context of the recorder, which is bad. So instead. We
233
+ # instance_exec it back on the instance.
234
+ # TODO: use the proc builder like in the `cases` method, which will let us tie
235
+ # down the arity
236
+ some_impl = proc { |*args| the_instance.instance_exec(*args, &impl) }
237
+ memo[c] = some_impl
238
+ memo
239
+ })
233
240
  end
234
241
  end
235
242
  end
@@ -2,6 +2,7 @@ module ADT
2
2
  # @private
3
3
  class CaseRecorder
4
4
  alias :__instance_eval :instance_eval
5
+ alias :__instance_exec :instance_exec
5
6
  instance_methods.each { |m| undef_method m unless m =~ /(^__|object_id)/ }
6
7
 
7
8
  def initialize
metadata CHANGED
@@ -1,8 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adt
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.3
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 4
9
+ version: 0.0.4
6
10
  platform: ruby
7
11
  authors:
8
12
  - Nick Partridge
@@ -10,7 +14,7 @@ autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
16
 
13
- date: 2011-08-10 00:00:00 +10:00
17
+ date: 2011-09-06 00:00:00 +10:00
14
18
  default_executable:
15
19
  dependencies: []
16
20
 
@@ -37,21 +41,23 @@ rdoc_options: []
37
41
  require_paths:
38
42
  - lib
39
43
  required_ruby_version: !ruby/object:Gem::Requirement
40
- none: false
41
44
  requirements:
42
45
  - - ">="
43
46
  - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
44
49
  version: "0"
45
50
  required_rubygems_version: !ruby/object:Gem::Requirement
46
- none: false
47
51
  requirements:
48
52
  - - ">="
49
53
  - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
50
56
  version: "0"
51
57
  requirements: []
52
58
 
53
59
  rubyforge_project:
54
- rubygems_version: 1.6.1
60
+ rubygems_version: 1.3.6
55
61
  signing_key:
56
62
  specification_version: 3
57
63
  summary: Algebraic data type DSL