print_r 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemspec +4 -0
- data/Rakefile +2 -0
- data/lib/print_r/generator.rb +55 -0
- data/lib/print_r/parser.rb +62 -0
- data/lib/print_r/version.rb +3 -0
- data/lib/print_r.rb +18 -0
- data/print_r.gemspec +18 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8f45e1fd95e1954161a1885401fb6a1b768352ed
|
4
|
+
data.tar.gz: f855a5b7127dd03b42d33d6dc1d455fe14a29393
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4d1459fb03af1865b54231ab2767f5a34d4a0cd83d0689efe5af1e92ef6bb20e3b3c4e53b9986b57580192b78cf0b38664e86b2fc47449c99ad95479da2ead28
|
7
|
+
data.tar.gz: 43346e7625479f9fa409fd51ecdf7edc216197b282194797d25e81a19a063f2ec68acd2b5e95d625b2a76f384929739b30714621618ae8b7fe633cadc4ab736a
|
data/Gemspec
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# encoding=utf-8
|
3
|
+
|
4
|
+
module PrintR
|
5
|
+
class Generator
|
6
|
+
attr_reader :object
|
7
|
+
|
8
|
+
def initialize(object)
|
9
|
+
@object = object
|
10
|
+
end
|
11
|
+
|
12
|
+
def generate
|
13
|
+
_to_str(object, 0)
|
14
|
+
end
|
15
|
+
|
16
|
+
def _to_str(obj, indent)
|
17
|
+
sp1 = " " * indent
|
18
|
+
sp2 = " " * (indent+1)
|
19
|
+
str = nil
|
20
|
+
type = "Array"
|
21
|
+
|
22
|
+
if obj.kind_of?(Hash)
|
23
|
+
type = "Array"
|
24
|
+
elsif obj.kind_of?(Array)
|
25
|
+
type = "Array"
|
26
|
+
obj2 = {}
|
27
|
+
obj.each_with_index do |value,i|
|
28
|
+
obj2[i] = value
|
29
|
+
end
|
30
|
+
obj = obj2
|
31
|
+
elsif obj.kind_of?(String)
|
32
|
+
elsif obj.respond_to?(:instance_variables)
|
33
|
+
type = "#{obj.class.name} Object"
|
34
|
+
obj = obj.instance_variables.inject({}) {|h,key|
|
35
|
+
key_str = key.to_s.sub("@", "")
|
36
|
+
h[key_str] = obj.instance_variable_get(key)
|
37
|
+
h
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
if obj.kind_of?(Hash)
|
42
|
+
str = "#{type}\n"
|
43
|
+
str += sp1 + "(\n"
|
44
|
+
obj.each_pair do |key,value|
|
45
|
+
str += sprintf("%s[%s] => %s\n", sp2, key, _to_str(value, indent+2))
|
46
|
+
end
|
47
|
+
str += sp1 + ")\n"
|
48
|
+
else
|
49
|
+
str = obj.to_s
|
50
|
+
end
|
51
|
+
|
52
|
+
str
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# encoding=utf-8
|
3
|
+
|
4
|
+
module PrintR
|
5
|
+
class Parser
|
6
|
+
attr_reader :text
|
7
|
+
|
8
|
+
def initialize(text)
|
9
|
+
@text = text
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse
|
13
|
+
_to_object(text, 0)
|
14
|
+
end
|
15
|
+
|
16
|
+
def _to_object(str, indent)
|
17
|
+
str = str.strip
|
18
|
+
sp = " " * indent
|
19
|
+
md = str.match(/^(?:Array|.*? Object)\n#{sp}\(\n(.+)#{sp}\)$/m)
|
20
|
+
if md
|
21
|
+
obj = _to_key_values(md[1].split(/(\r\n|\r|\n)/), indent+1)
|
22
|
+
|
23
|
+
obj.each do |key, value|
|
24
|
+
obj[key] = _to_object(value, indent+2)
|
25
|
+
end
|
26
|
+
else
|
27
|
+
obj = str
|
28
|
+
end
|
29
|
+
obj
|
30
|
+
end
|
31
|
+
|
32
|
+
def _to_key_values(lines, indent)
|
33
|
+
sp = " " * indent
|
34
|
+
h = {}
|
35
|
+
tmp_key = nil
|
36
|
+
tmp_val = ""
|
37
|
+
lines.each do |line|
|
38
|
+
md = line.match(/^#{sp}\[([^\]]+)\] => (.*)/)
|
39
|
+
if md
|
40
|
+
if tmp_key
|
41
|
+
h[tmp_key] = tmp_val
|
42
|
+
tmp_key = nil
|
43
|
+
tmp_val = ""
|
44
|
+
end
|
45
|
+
|
46
|
+
tmp_key = md[1]
|
47
|
+
tmp_val = md[2]
|
48
|
+
else
|
49
|
+
tmp_val += line
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
if tmp_key
|
54
|
+
h[tmp_key] = tmp_val
|
55
|
+
tmp_key = nil
|
56
|
+
tmp_val = ""
|
57
|
+
end
|
58
|
+
|
59
|
+
h
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/print_r.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# encoding=utf-8
|
3
|
+
|
4
|
+
require 'print_r/parser'
|
5
|
+
require 'print_r/generator'
|
6
|
+
require 'print_r/version'
|
7
|
+
|
8
|
+
module PrintR
|
9
|
+
class << self
|
10
|
+
def parse(text)
|
11
|
+
Parser.new(text).parse
|
12
|
+
end
|
13
|
+
|
14
|
+
def generate(object)
|
15
|
+
Generator.new(object).generate
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/print_r.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require './lib/print_r/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.authors = ["Yoshida Tetsuya"]
|
7
|
+
gem.email = ["yoshida.eth0@gmail.com"]
|
8
|
+
gem.description = %q{This is a print_r() implementation.}
|
9
|
+
gem.summary = %q{This is a print_r() implementation.}
|
10
|
+
gem.homepage = %q{https://github.com/yoshida-eth0/ruby-print_r}
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\).delete_if{|x| x.match(/^example/)}
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "print_r"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = PrintR::VERSION
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: print_r
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yoshida Tetsuya
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: This is a print_r() implementation.
|
14
|
+
email:
|
15
|
+
- yoshida.eth0@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- Gemspec
|
21
|
+
- Rakefile
|
22
|
+
- lib/print_r.rb
|
23
|
+
- lib/print_r/generator.rb
|
24
|
+
- lib/print_r/parser.rb
|
25
|
+
- lib/print_r/version.rb
|
26
|
+
- print_r.gemspec
|
27
|
+
homepage: https://github.com/yoshida-eth0/ruby-print_r
|
28
|
+
licenses: []
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.0.3
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: This is a print_r() implementation.
|
50
|
+
test_files: []
|