psychgus 1.3.3 → 1.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,107 +1,92 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  #--
5
5
  # This file is part of Psychgus.
6
- # Copyright (c) 2017-2019 Jonathan Bradley Whited (@esotericpig)
7
- #
8
- # Psychgus is free software: you can redistribute it and/or modify
9
- # it under the terms of the GNU Lesser General Public License as published by
10
- # the Free Software Foundation, either version 3 of the License, or
11
- # (at your option) any later version.
12
- #
13
- # Psychgus is distributed in the hope that it will be useful,
14
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- # GNU Lesser General Public License for more details.
17
- #
18
- # You should have received a copy of the GNU Lesser General Public License
19
- # along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2017 Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
20
9
  #++
21
10
 
22
-
23
11
  module Psychgus
24
12
  ###
25
13
  # This is the OOP way to style your classes/modules/etc.
26
- #
14
+ #
27
15
  # Even though it's unnecessary to mix in (include) this module, it's recommended because new methods may be
28
16
  # added in the future, so this pseudo-guarantees your class won't break in a new version.
29
- #
17
+ #
30
18
  # A complete example:
31
19
  # require 'psychgus'
32
- #
20
+ #
33
21
  # class MyClass
34
22
  # include Psychgus::Blueberry
35
- #
23
+ #
36
24
  # attr_reader :my_hash
37
- #
25
+ #
38
26
  # def initialize()
39
27
  # @my_hash = {:key1=>'val1',:key2=>'val2'}
40
28
  # end
41
- #
29
+ #
42
30
  # def psychgus_stylers(sniffer)
43
31
  # return MyClassStyler.new(sniffer)
44
32
  # end
45
33
  # end
46
- #
34
+ #
47
35
  # class MyClassStyler
48
36
  # include Psychgus::Styler
49
- #
37
+ #
50
38
  # def initialize(sniffer)
51
39
  # @level = sniffer.level
52
40
  # end
53
- #
41
+ #
54
42
  # def style_mapping(sniffer,node)
55
43
  # node.style = Psychgus::MAPPING_FLOW
56
- #
44
+ #
57
45
  # relative_level = sniffer.level - @level
58
46
  # end
59
47
  # end
60
- #
48
+ #
61
49
  # my_class = MyClass.new()
62
50
  # puts my_class.to_yaml()
63
- #
51
+ #
64
52
  # Alternatively, MyClass could have been the {Blueberry} and the {Styler}, without the need for
65
53
  # MyClassStyler:
66
54
  # class MyClass
67
55
  # include Psychgus::Blueberry
68
56
  # include Psychgus::Styler
69
- #
57
+ #
70
58
  # # ...
71
- #
59
+ #
72
60
  # def psychgus_stylers(sniffer)
73
61
  # @level = sniffer.level # This will be included in the output of to_yaml()
74
- #
62
+ #
75
63
  # return self
76
64
  # end
77
- #
65
+ #
78
66
  # def style_mapping(sniffer,node)
79
67
  # # ...
80
68
  # end
81
69
  # end
82
- #
70
+ #
83
71
  # However, it's best to put the styling logic inside of a separate class (or inner class) away from the main
84
72
  # logic. This also prevents extra helper vars, like @level, from showing up in the output.
85
- #
73
+ #
86
74
  # After your class and its children have been processed, the styler(s) will be removed from the logic for
87
75
  # the next sibling object(s). Therefore, you can safely do class-specific checks on level, etc. without it
88
76
  # affecting the sibling object(s). See {Ext::YAMLTreeExt} and {Ext::YAMLTreeExt#accept} for details.
89
- #
77
+ #
90
78
  # "The Blueberry" is the name of Gus's car from the TV show Psych.
91
- #
92
- # @author Jonathan Bradley Whited (@esotericpig)
93
- # @since 1.0.0
94
- #
79
+ #
95
80
  # @see Ext::YAMLTreeExt
96
81
  # @see Ext::YAMLTreeExt#accept
97
82
  ###
98
83
  module Blueberry
99
84
  # Duck Type this method to return the {Styler}(s) for your class/module/etc.
100
- #
85
+ #
101
86
  # @param sniffer [SuperSniffer] passed in from {StyledTreeBuilder}; use this for storing the level,
102
87
  # position, etc. for styling your instance variables later relative to your
103
88
  # class/module/etc.
104
- #
89
+ #
105
90
  # @return [Styler,Array<Styler>,nil] {Styler}(s) for this class/module/etc.
106
91
  def psychgus_stylers(sniffer)
107
92
  return nil
@@ -1,72 +1,60 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  #--
5
5
  # This file is part of Psychgus.
6
- # Copyright (c) 2019 Jonathan Bradley Whited (@esotericpig)
7
- #
8
- # Psychgus is free software: you can redistribute it and/or modify
9
- # it under the terms of the GNU Lesser General Public License as published by
10
- # the Free Software Foundation, either version 3 of the License, or
11
- # (at your option) any later version.
12
- #
13
- # Psychgus is distributed in the hope that it will be useful,
14
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- # GNU Lesser General Public License for more details.
17
- #
18
- # You should have received a copy of the GNU Lesser General Public License
19
- # along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2019 Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
20
9
  #++
21
10
 
22
-
23
11
  module Psychgus
24
12
  module Ext
25
13
  ###
26
14
  # Core extensions to Object.
27
- #
28
- # @author Jonathan Bradley Whited (@esotericpig)
29
- # @since 1.0.0
30
- #
15
+ #
31
16
  # @see https://github.com/ruby/psych/blob/master/lib/psych/core_ext.rb
32
17
  ###
33
18
  module ObjectExt
34
19
  # Convert an Object to YAML.
35
- #
20
+ #
36
21
  # +options+ can also be a Hash, so can be a drop-in-replacement for Psych.
37
- #
22
+ #
38
23
  # @example
39
24
  # class MyStyler
40
25
  # include Psychgus::Styler
41
- #
26
+ #
42
27
  # def style_sequence(sniffer,node)
43
28
  # node.style = Psychgus::SEQUENCE_FLOW
44
29
  # end
45
30
  # end
46
- #
31
+ #
47
32
  # my_obj = {
48
33
  # :Foods => {
49
34
  # :Fruits => %w(Apple Banana Blueberry Pear),
50
35
  # :Veggies => %w(Bean Carrot Celery Pea)
51
36
  # }}
52
- #
37
+ #
53
38
  # puts my_obj.to_yaml(indentation: 5,stylers: MyStyler.new)
54
- #
39
+ #
55
40
  # # Or, pass in a Hash:
56
41
  # #puts my_obj.to_yaml({:indentation=>5,:stylers=>MyStyler.new})
57
- #
42
+ #
58
43
  # # Output:
59
44
  # # ---
60
45
  # # :Foods:
61
46
  # # :Fruits: [Apple, Banana, Blueberry, Pear]
62
47
  # # :Veggies: [Bean, Carrot, Celery, Pea]
63
- #
48
+ #
64
49
  # @param options [Hash] the options (or keyword args) to pass to {Psychgus.dump}
65
- #
50
+ #
66
51
  # @return [String] the YAML generated from this Object
67
- #
52
+ #
68
53
  # @see Psychgus.dump
69
- def to_yaml(**options)
54
+ def to_yaml(options={})
55
+ # NOTE: This method signature must use old-style `options={}` instead of `**options`!
56
+ # Because some Gems, like `Moneta`, depend on this.
57
+
70
58
  # Do not use Psych.dump() if no Stylers, because a class might be a Blueberry
71
59
  return Psychgus.dump(self,**options)
72
60
  end
@@ -1,46 +1,31 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  #--
5
5
  # This file is part of Psychgus.
6
- # Copyright (c) 2019 Jonathan Bradley Whited (@esotericpig)
7
- #
8
- # Psychgus is free software: you can redistribute it and/or modify
9
- # it under the terms of the GNU Lesser General Public License as published by
10
- # the Free Software Foundation, either version 3 of the License, or
11
- # (at your option) any later version.
12
- #
13
- # Psychgus is distributed in the hope that it will be useful,
14
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- # GNU Lesser General Public License for more details.
17
- #
18
- # You should have received a copy of the GNU Lesser General Public License
19
- # along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2019 Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
20
9
  #++
21
10
 
22
-
23
11
  require 'psych'
24
12
 
25
13
  module Psychgus
26
14
  module Ext
27
15
  ###
28
16
  # Extensions to Psych::Nodes::Node.
29
- #
30
- # @author Jonathan Bradley Whited (@esotericpig)
31
- # @since 1.0.0
32
17
  ###
33
18
  module NodeExt
34
19
  # Check if this Node is of a certain type (Alias, Mapping, Scalar, Sequence, etc.).
35
- #
20
+ #
36
21
  # New versions of Psych have alias?(), mapping?(), etc., so this is for old versions.
37
- #
22
+ #
38
23
  # This is equivalent to the following (with less typing):
39
24
  # node.is_a?(Psych::Nodes::Alias)
40
25
  # node.is_a?(Psych::Nodes::Mapping)
41
26
  # node.is_a?(Psych::Nodes::Scalar)
42
27
  # node.is_a?(Psych::Nodes::Sequence)
43
- #
28
+ #
44
29
  # @example
45
30
  # node.node_of?(:alias)
46
31
  # node.node_of?(:mapping)
@@ -48,17 +33,17 @@ module Psychgus
48
33
  # node.node_of?(:sequence)
49
34
  # node.node_of?(:alias,:mapping,:scalar,:sequence) # OR
50
35
  # node.node_of?(:doc,:map,:seq) # OR
51
- #
36
+ #
52
37
  # @param names [Symbol,String] the type(s) to check using OR
53
- #
38
+ #
54
39
  # @return [true,false] true if this Node is one of the +names+ type, else false
55
- #
40
+ #
56
41
  # @see Psychgus.node_class
57
42
  def node_of?(*names)
58
43
  names.each do |name|
59
44
  return true if is_a?(Psychgus.node_class(name))
60
45
  end
61
-
46
+
62
47
  return false
63
48
  end
64
49
  end
@@ -1,68 +1,63 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  #--
5
5
  # This file is part of Psychgus.
6
- # Copyright (c) 2019 Jonathan Bradley Whited (@esotericpig)
7
- #
8
- # Psychgus is free software: you can redistribute it and/or modify
9
- # it under the terms of the GNU Lesser General Public License as published by
10
- # the Free Software Foundation, either version 3 of the License, or
11
- # (at your option) any later version.
12
- #
13
- # Psychgus is distributed in the hope that it will be useful,
14
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- # GNU Lesser General Public License for more details.
17
- #
18
- # You should have received a copy of the GNU Lesser General Public License
19
- # along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2019 Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
20
9
  #++
21
10
 
22
-
23
11
  require 'psych'
12
+ require 'rubygems/version'
24
13
 
25
14
  require 'psychgus/styled_tree_builder'
26
15
 
27
16
  module Psychgus
28
17
  module Ext
18
+ PSYCHGUS_PSYCH_VERSION = Gem::Version.create(Psych::VERSION)
19
+ PSYCHGUS_PSYCH_VERSION_5_1_2 = Gem::Version.create('5.1.2')
20
+
29
21
  ###
30
22
  # Extensions to Psych::Visitors::YAMLTree::Registrar.
31
- #
32
- # @author Jonathan Bradley Whited (@esotericpig)
33
- # @since 1.0.0
34
23
  ###
35
24
  module RegistrarExt
36
25
  # Remove +target+ from this Registrar to prevent it becoming an alias.
37
- #
26
+ #
38
27
  # @param target [Object] the Object to remove from this Registrar
39
- def remove_alias(target)
40
- @obj_to_node.delete(target.object_id)
28
+ def psychgus_unregister(target)
29
+ if PSYCHGUS_PSYCH_VERSION < PSYCHGUS_PSYCH_VERSION_5_1_2
30
+ return unless key?(target) && target.respond_to?(:object_id)
31
+
32
+ @obj_to_node.delete(target.object_id)
33
+ else # 5.1.2+
34
+ return unless key?(target)
35
+
36
+ @targets.delete(target)
37
+ @obj_to_node.delete(target)
38
+ end
41
39
  end
42
40
  end
43
-
41
+
44
42
  ###
45
43
  # Extensions to Psych::Visitors::YAMLTree.
46
- #
47
- # @author Jonathan Bradley Whited (@esotericpig)
48
- # @since 1.0.0
49
44
  ###
50
45
  module YAMLTreeExt
51
46
  # Accepts a new Object to convert to YAML.
52
- #
47
+ #
53
48
  # This is roughly the same place where Psych checks if +target+ responds to +:encode_with+.
54
- #
49
+ #
55
50
  # 1. Check if +@emitter+ is a {StyledTreeBuilder}.
56
51
  # 2. If #1 and +target+ is a {Blueberry}, get the {Styler}(s) from +target+ and add them to +@emitter+.
57
52
  # 3. If #1 and +@emitter.deref_aliases?+, prevent +target+ from becoming an alias.
58
53
  # 4. Call +super+ and store the result.
59
54
  # 5. If #2, remove the {Styler}(s) from +@emitter+.
60
55
  # 6. Return the result of +super+.
61
- #
56
+ #
62
57
  # @param target [Object] the Object to pass to super
63
- #
58
+ #
64
59
  # @return the result of super
65
- #
60
+ #
66
61
  # @see Psych::Visitors::YAMLTree
67
62
  # @see Blueberry
68
63
  # @see Blueberry#psychgus_stylers
@@ -70,29 +65,27 @@ module Psychgus
70
65
  # @see StyledTreeBuilder
71
66
  def accept(target)
72
67
  styler_count = 0
73
-
68
+
74
69
  if @emitter.is_a?(StyledTreeBuilder)
75
70
  # Blueberry?
76
71
  if target.respond_to?(:psychgus_stylers)
77
72
  stylers = target.psychgus_stylers(@emitter.sniffer)
78
73
  stylers_old_len = @emitter.stylers.length
79
-
74
+
80
75
  @emitter.add_styler(*stylers)
81
-
76
+
82
77
  styler_count = @emitter.stylers.length - stylers_old_len
83
78
  end
84
-
79
+
85
80
  # Dereference aliases?
86
- if @emitter.deref_aliases?()
87
- @st.remove_alias(target) if target.respond_to?(:object_id) && @st.key?(target)
88
- end
81
+ @st.psychgus_unregister(target) if @emitter.deref_aliases?
89
82
  end
90
-
83
+
91
84
  result = super(target)
92
-
85
+
93
86
  # Check styler_count because @emitter may not be a StyledTreeBuilder and target may not be a Blueberry
94
87
  @emitter.pop_styler(styler_count) if styler_count > 0
95
-
88
+
96
89
  return result
97
90
  end
98
91
  end