astrolabe 0.5.1 → 0.5.2

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
  SHA1:
3
- metadata.gz: 1306bd1680b44a33bad44374f71f0254be88dd30
4
- data.tar.gz: f49d728a8cde1724e8c312475bcd8dad607c4eb6
3
+ metadata.gz: 85dd0cdbc2f8c839e157b64b9115222a2b1b3a44
4
+ data.tar.gz: e7aeecb9e7db9c47722ba291b594a0e69adc399d
5
5
  SHA512:
6
- metadata.gz: 47f5e3637d1d87069147a9d1f574ae36d853ce942fa1f3a44330430184519639490aa28c0e6c5c81e8a126ec3dbef1152eb754209c1bc2bbf35c0a2cff69f234
7
- data.tar.gz: 10468278e65896eb7d8a641fbe57789823a7a11bb91e89b8b34f976037b0ae3f7e6c9066e1bf583223123c23430f9143c6eb874ba95e01621b1f5e8ab4bc9d18
6
+ metadata.gz: 504173457388326f8fe074f1fe091ab6db9d92c28d95797a5fff0d6c6a4eb7f7888e760b5417bcd9268aaae2f2714245b0549bc1d0ac49efe50150b63ec02369
7
+ data.tar.gz: efa3d1e8edc678eb99ba91daa43b139b0398e3aa0ba1a787264a4c34fe39d09dcc8cdd08a0575808e160a29527163eaeb51d1eb581bcec1c9fcaccdfe0749843
data/astrolabe.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.test_files = spec.files.grep(/^spec\//)
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.add_runtime_dependency 'parser', '~> 2.1'
22
+ spec.add_runtime_dependency 'parser', '>= 2.1', '< 3.0'
23
23
 
24
24
  spec.add_development_dependency 'bundler', '~> 1.6'
25
25
  spec.add_development_dependency 'rake', '~> 10.3'
@@ -4,10 +4,14 @@ class Benchmarking
4
4
  class << self
5
5
  attr_accessor :warm_up
6
6
  alias_method :warm_up?, :warm_up
7
- attr_writer :loop_count
7
+ attr_writer :trial_count, :loop_count_in_trial
8
8
 
9
- def loop_count
10
- @loop_count ||= 100
9
+ def trial_count
10
+ @trial_count ||= 3
11
+ end
12
+
13
+ def loop_count_in_trial
14
+ @loop_count_in_trial ||= 100
11
15
  end
12
16
 
13
17
  def pretty_time(time)
@@ -22,32 +26,48 @@ class Benchmarking
22
26
  @process = block
23
27
  end
24
28
 
25
- def run
26
- @process.call
29
+ def average_time
30
+ measure if times.empty?
31
+ times.reduce(:+) / self.class.trial_count
27
32
  end
28
33
 
29
- def time
30
- return @time if @time
31
-
32
- self.class.loop_count.times { run } if self.class.warm_up?
33
- GC.start # https://github.com/ruby/ruby/blob/v2_1_2/lib/benchmark.rb#L265
34
-
35
- beginning = Time.now
36
- self.class.loop_count.times { run }
37
- ending = Time.now
38
-
39
- @time = ending - beginning
40
- end
34
+ alias_method :time, :average_time
41
35
 
42
36
  def pretty_time
43
37
  self.class.pretty_time(time)
44
38
  end
45
39
 
40
+ def times
41
+ @times ||= []
42
+ end
43
+
46
44
  def inspect
47
45
  "#{name} (#{pretty_time})"
48
46
  end
49
47
 
50
48
  alias_method :to_s, :inspect
49
+
50
+ private
51
+
52
+ def measure
53
+ fail 'Already measured!' unless times.empty?
54
+
55
+ self.class.loop_count.times { run } if self.class.warm_up?
56
+
57
+ self.class.trial_count.times do
58
+ GC.start # https://github.com/ruby/ruby/blob/v2_1_2/lib/benchmark.rb#L265
59
+
60
+ beginning = Time.now
61
+ self.class.loop_count_in_trial.times { run }
62
+ ending = Time.now
63
+
64
+ times << (ending - beginning)
65
+ end
66
+ end
67
+
68
+ def run
69
+ @process.call
70
+ end
51
71
  end
52
72
 
53
73
  RSpec::Matchers.define :be_faster_than do |other|
@@ -59,17 +79,12 @@ RSpec::Matchers.define :be_faster_than do |other|
59
79
  end
60
80
  end
61
81
 
62
- {
63
- twice: 2,
64
- three_times: 3,
65
- four_times: 4,
66
- five_times: 5,
67
- six_times: 6,
68
- seven_times: 7
69
- }.each do |name, value|
70
- chain name do
71
- @times = value
72
- end
82
+ chain :at_least do |times|
83
+ @times = times
84
+ end
85
+
86
+ # Just a syntax sugar.
87
+ chain :times do
73
88
  end
74
89
 
75
90
  failure_message do |subject|
@@ -6,7 +6,8 @@ require 'parser/current'
6
6
 
7
7
  describe 'performance' do
8
8
  Benchmarking.warm_up = true
9
- Benchmarking.loop_count = 1000
9
+ Benchmarking.trial_count = 3
10
+ Benchmarking.loop_count_in_trial = 1000
10
11
 
11
12
  def generate_source(max, current_nest_level = 1)
12
13
  <<-END
@@ -113,8 +114,8 @@ describe 'performance' do
113
114
  EachDescendantBenchmark.new('proc-pass', ast_with_node_class(node_class))
114
115
  end
115
116
 
116
- specify 'block-pass is obviously (at least 5 times) faster than nested-yield' do
117
- expect(block_pass).to be_faster_than(nested_yield).five_times
117
+ specify 'block-pass is obviously (at least 4 times) faster than nested-yield' do
118
+ expect(block_pass).to be_faster_than(nested_yield).at_least(4).times
118
119
  end
119
120
 
120
121
  specify 'block-pass is a bit faster than proc-pass' do
@@ -81,15 +81,15 @@ module Astrolabe
81
81
  # @yieldparam [Node] node each ancestor node
82
82
  # @return [self] if a block is given
83
83
  # @return [Enumerator] if no block is given
84
- def each_ancestor(*types)
84
+ def each_ancestor(*types, &block)
85
85
  return to_enum(__method__, *types) unless block_given?
86
86
 
87
87
  types.flatten!
88
- last_node = self
89
88
 
90
- while (current_node = last_node.parent)
91
- yield current_node if types.empty? || types.include?(current_node.type)
92
- last_node = current_node
89
+ if types.empty?
90
+ visit_ancestors(&block)
91
+ else
92
+ visit_ancestors_with_types(types, &block)
93
93
  end
94
94
 
95
95
  self
@@ -149,8 +149,15 @@ module Astrolabe
149
149
  # @return [Enumerator] if no block is given
150
150
  def each_descendant(*types, &block)
151
151
  return to_enum(__method__, *types) unless block_given?
152
+
152
153
  types.flatten!
153
- visit_descendants(types, &block)
154
+
155
+ if types.empty?
156
+ visit_descendants(&block)
157
+ else
158
+ visit_descendants_with_types(types, &block)
159
+ end
160
+
154
161
  self
155
162
  end
156
163
 
@@ -177,19 +184,55 @@ module Astrolabe
177
184
  # @return [Enumerator] if no block is given
178
185
  def each_node(*types, &block)
179
186
  return to_enum(__method__, *types) unless block_given?
187
+
180
188
  types.flatten!
189
+
181
190
  yield self if types.empty? || types.include?(type)
182
- visit_descendants(types, &block)
191
+
192
+ if types.empty?
193
+ visit_descendants(&block)
194
+ else
195
+ visit_descendants_with_types(types, &block)
196
+ end
197
+
183
198
  self
184
199
  end
185
200
 
186
201
  protected
187
202
 
188
- def visit_descendants(types, &block)
203
+ def visit_descendants(&block)
189
204
  children.each do |child|
190
205
  next unless child.is_a?(Node)
191
- yield child if types.empty? || types.include?(child.type)
192
- child.visit_descendants(types, &block)
206
+ yield child
207
+ child.visit_descendants(&block)
208
+ end
209
+ end
210
+
211
+ def visit_descendants_with_types(types, &block)
212
+ children.each do |child|
213
+ next unless child.is_a?(Node)
214
+ yield child if types.include?(child.type)
215
+ child.visit_descendants_with_types(types, &block)
216
+ end
217
+ end
218
+
219
+ private
220
+
221
+ def visit_ancestors
222
+ last_node = self
223
+
224
+ while (current_node = last_node.parent)
225
+ yield current_node
226
+ last_node = current_node
227
+ end
228
+ end
229
+
230
+ def visit_ancestors_with_types(types)
231
+ last_node = self
232
+
233
+ while (current_node = last_node.parent)
234
+ yield current_node if types.include?(current_node.type)
235
+ last_node = current_node
193
236
  end
194
237
  end
195
238
  end
@@ -6,7 +6,7 @@ module Astrolabe
6
6
  module Version
7
7
  MAJOR = 0
8
8
  MINOR = 5
9
- PATCH = 1
9
+ PATCH = 2
10
10
 
11
11
  def self.to_s
12
12
  [MAJOR, MINOR, PATCH].join('.')
data/spec/spec_helper.rb CHANGED
@@ -90,6 +90,7 @@ if ENV['TRAVIS']
90
90
 
91
91
  SimpleCov.start do
92
92
  add_filter '/spec/'
93
+ add_filter '/benchmark/'
93
94
  add_filter '/vendor/'
94
95
  end
95
96
  end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: astrolabe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuji Nakayama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-06 00:00:00.000000000 Z
11
+ date: 2014-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '2.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '2.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: bundler
29
35
  requirement: !ruby/object:Gem::Requirement