ppy 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.
Files changed (4) hide show
  1. data/bin/ppy +3 -6
  2. data/lib/ppy.rb +19 -6
  3. data/lib/ppy/nested_access.rb +22 -0
  4. metadata +3 -2
data/bin/ppy CHANGED
@@ -14,11 +14,8 @@ yaml = YAML.load_file(file)
14
14
 
15
15
  # If the vine gem is installed, only output a nested value if one has been
16
16
  # provided as the second argument.
17
- if Gem::Specification.find_by_name('vine')
18
- unless ARGV[1].nil?
19
- require 'vine'
20
- yaml = yaml.vine(ARGV[1])
21
- end
17
+ unless ARGV[1].nil?
18
+ yaml = yaml.nested_access(ARGV[1])
22
19
  end
23
20
 
24
- PrettyYaml.print(yaml)
21
+ PrettyYaml.print(yaml)
data/lib/ppy.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rainbow'
2
+ require 'ppy/nested_access'
2
3
 
3
4
  module PrettyYaml
4
5
  extend self
@@ -12,7 +13,7 @@ module PrettyYaml
12
13
  string = "#{indent_string(level)}#{format_key(key)}:"
13
14
 
14
15
  if value.empty?
15
- puts string + ' ' + format_value('{ }')
16
+ puts string + ' ' + empty_value(value)
16
17
  else
17
18
  puts string
18
19
  print(value, level + 1)
@@ -26,7 +27,7 @@ module PrettyYaml
26
27
  if value.is_a? Enumerable
27
28
  string = format_key "#{indent_string(level)}-"
28
29
  if value.empty?
29
- puts string + ' ' + format_value('[ ]')
30
+ puts string + ' ' + empty_value(value)
30
31
  else
31
32
  puts string
32
33
  print_yaml(value, level + 1)
@@ -40,17 +41,29 @@ module PrettyYaml
40
41
  end
41
42
  end
42
43
 
43
- # creates space indentation for a string, based on a level int.
44
- def indent_string(level)
44
+ # Creates space indentation for a string, based on a level int.
45
+ def indent_string(level = 0)
45
46
  " " * level
46
47
  end
47
48
 
48
- # Formats the data key.
49
+ # Formats an empty_value.
50
+ def empty_value(value)
51
+ case value
52
+ when Array
53
+ format_value('[ ]')
54
+ when Hash
55
+ format_value('{ }')
56
+ else
57
+ value
58
+ end
59
+ end
60
+
61
+ # Formats a data key.
49
62
  def format_key(key)
50
63
  key.to_s.color(:yellow)
51
64
  end
52
65
 
53
- # Formats the data value.
66
+ # Formats a data value.
54
67
  def format_value(value)
55
68
  case value
56
69
  when Numeric
@@ -0,0 +1,22 @@
1
+
2
+ class Hash
3
+
4
+ def nested_access(path)
5
+ ret = self
6
+ path.to_s.split('.').each do |p|
7
+ if p.to_i.to_s == p
8
+ ret = ret[p.to_i]
9
+ elsif !ret[p.to_s].nil?
10
+ ret = ret[p.to_s]
11
+ elsif !ret[p.to_sym].nil?
12
+ ret = ret[p.to_sym]
13
+ else
14
+ ret = nil
15
+ end
16
+ break unless ret
17
+ end
18
+
19
+ ret
20
+ end
21
+
22
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ppy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -35,8 +35,9 @@ extensions: []
35
35
  extra_rdoc_files: []
36
36
  files:
37
37
  - lib/ppy.rb
38
+ - lib/ppy/nested_access.rb
38
39
  - bin/ppy
39
- homepage: http://rubygems.org/gems/ppy
40
+ homepage: https://github.com/damiankloip/ppy
40
41
  licenses: []
41
42
  post_install_message:
42
43
  rdoc_options: []