psych-simple 1.0.0 → 2.0.0

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 +4 -4
  2. data/lib/psych/simple.rb +26 -5
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95f2a708d81448dd3cf14c03a92562315ff9710d
4
- data.tar.gz: 24b00f2d1e0ba113cd22a1c6a179755f643c0eb1
3
+ metadata.gz: 46925684618841d0fedcff982e85391d3f117952
4
+ data.tar.gz: cde4c99de716a9e1fef0bcd1b45d8b8cbd65e628
5
5
  SHA512:
6
- metadata.gz: 2238f24b376985311123c0ba141022beadc9709cbd99e70f4413feeba013cf4142dcd59a98aca90e8eb39ca29bba68fa8074abaa2f9a1cdf69349b8abf77ccc7
7
- data.tar.gz: e1a6895151ab45dcab7eb1bc317a17d0e8da89a5ba65df37a66e047e9fd4ee6b8bb2cde77d1260f7f61304a9d9088dac76bc97494339c9869ef8621c35962a61
6
+ metadata.gz: 85c0c3ea612897fda59b767407e1544635d10db6b983a428fba0dc120b8dce4375f291448ddcee61f1ece3934949528a7e90298f52b07429b5e21b5d61d6205f
7
+ data.tar.gz: 7874febe0e2da19d8d5c21b8d7dfafd3a7000fb9924bb82d6b206481c85d9e67d4249bbf0fabe57ff7bb62429f13d207265065478a102d062f47a8b9d518d8e9
data/lib/psych/simple.rb CHANGED
@@ -1,15 +1,32 @@
1
1
  require 'psych'
2
2
 
3
3
  module Psych
4
- def self.simple_load(yaml, filename = nil)
5
- parser = Parser.new(SimpleHandler.new)
6
- parser.parse yaml, filename
4
+
5
+ # Load yaml in to a ruby data structure.
6
+ #
7
+ # This function is 2-5x faster as compared to Psych::load but supports a
8
+ # simplified (and safe) data format only. All values are deserialized as
9
+ # strings. Anchors and tags are not supported, and thus ruby objects are
10
+ # not supported either.
11
+ #
12
+ # Options can have the following keys,
13
+ # - filename: used in the exception message (defaults to nil)
14
+ # - symbolize_names: if true return symbols as keys in a yaml mapping,
15
+ # otherwise strings are returned (defaults to false)
16
+ #
17
+ def self.simple_load(yaml, options = nil)
18
+ parser = Parser.new(SimpleHandler.new(options))
19
+ parser.parse yaml, options && options[:filename]
7
20
  parser.handler.document
8
21
  end
9
22
 
10
23
  class SimpleHandler
11
24
  attr_reader :document
12
25
 
26
+ def initialize(options = nil)
27
+ @symbolize_names = options && options[:symbolize_names]
28
+ end
29
+
13
30
  def start_stream(encoding)
14
31
  @stack = Array.new
15
32
  end
@@ -18,7 +35,7 @@ module Psych
18
35
  raise unless @stack.empty?
19
36
  end
20
37
 
21
- def start_document version, tag_directives, implicit
38
+ def start_document(version, tag_directives, implicit)
22
39
  raise unless tag_directives.empty?
23
40
  @stack.push Array.new
24
41
  end
@@ -33,7 +50,11 @@ module Psych
33
50
  end
34
51
 
35
52
  def end_mapping
36
- mapping = Hash[*@stack.pop]
53
+ mapping = Hash.new
54
+ @stack.pop.each_slice(2) do |name, value|
55
+ name = name.to_sym if @symbolize_names
56
+ mapping[name] = value
57
+ end
37
58
  @stack.last << mapping
38
59
  end
39
60
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: psych-simple
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Kuhn