pretty_ancestors 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/pretty_ancestors.rb +86 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3614e84fb8a3216e5b7851b869dc7514ee1a0af1
4
+ data.tar.gz: 5011138f8bbee8ac2f955172366207a35d858e78
5
+ SHA512:
6
+ metadata.gz: 1ab7ff2da30bd1a6b81e1157899c0737aa9746de8063ae56b759c6226981aaaa59729879460ed5d74217a8a0d1e5ae24db8dda197dc21949192cf555f9437c68
7
+ data.tar.gz: 30688e4d194339fa923af311d38e73263af18e2c06034dbc1c6dcbc357fe25f67b348a47575a3330d2c176e07edac2e753ce5859eedbb3c158f8ed2bafe6ba80
@@ -0,0 +1,86 @@
1
+ module PrettyAncestors
2
+ class Result < Array
3
+ def pretty_print(q)
4
+ q.group(1, '[', ']') {
5
+ breaker = ->{
6
+ q.text ','
7
+ q.text q.newline
8
+ q.text q.genspace.(q.indent)
9
+ }
10
+ q.seplist self, breaker, :each do |mod|
11
+ q.pp mod
12
+ end
13
+ }
14
+ end
15
+
16
+ end
17
+ end
18
+
19
+ class Module
20
+
21
+ def pretty_ancestors type = :simplified
22
+ case type
23
+ when :raw
24
+ root_ancestors = ancestors
25
+
26
+ recur = ->(mods, map, level) do
27
+ return [[], map] if mods.empty?
28
+ origin_map = map.dup
29
+ init, *tail = mods
30
+
31
+ if map[init]
32
+ if map[init] < level
33
+ return false, map
34
+ else
35
+ return recur.(tail, map, level + 1)
36
+ end
37
+ end
38
+ prepended = init.ancestors.take_while{|x| x != init} & root_ancestors
39
+ included = init.ancestors.drop_while{|x| x != init}.drop(1).select{|x| x.instance_of? Module} & root_ancestors
40
+
41
+ (in_front, map) = recur.(prepended, map.merge(init => level), level + 1)
42
+ (behind, map) = recur.(included, map, level + 1)
43
+
44
+ return [false, map] unless in_front && behind
45
+
46
+ (result, map) = recur.(tail, map, level + 1)
47
+ if result
48
+ [[[in_front, init, behind]].concat(result), map]
49
+ else
50
+ recur.(tail, origin_map, level)
51
+ end
52
+ end
53
+
54
+ if self.instance_of? Module
55
+ recur.([self], {}, 0)[0][0]
56
+ elsif self.instance_of? Class
57
+ ancestors.select{|x| x.instance_of? Class}.reverse.reduce([[], {}]) do |(arr, map), x|
58
+ (result, map) = recur.([x], map, 0)
59
+ arr << result[0]
60
+ [arr, map.keys.map{|k| [k, Float::INFINITY]}.to_h]
61
+ end[0].reverse
62
+ end
63
+ when :simplified
64
+ traverse = ->((included, mod, prepended)) do
65
+ if included.empty? && prepended.empty?
66
+ mod
67
+ else
68
+ result = []
69
+ result << included.map{|x| traverse.(x)} if !included.empty?
70
+ result << mod
71
+ result << prepended.map{|x| traverse.(x)} if !prepended.empty?
72
+ result
73
+ end
74
+ end
75
+
76
+ if self.instance_of? Module
77
+ result = traverse.(pretty_ancestors(:raw))
78
+ elsif self.instance_of? Class
79
+ result = pretty_ancestors(:raw).map{|x| traverse.(x)}
80
+ end
81
+ PrettyAncestors::Result.new(result.instance_of?(Array) ? result : [result])
82
+ end
83
+
84
+ end
85
+ end
86
+
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pretty_ancestors
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - CicholGricenchos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email: cichol@live.cn
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/pretty_ancestors.rb
20
+ homepage: https://github.com/CicholGricenchos/pretty_ancestors
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.6.7
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: better ancestor info
44
+ test_files: []