psychgus 1.2.0 → 1.3.4

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