indented_io 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 912e798e34c1c61e53b1cdaf9743383085503671e3ba318995a434ff1ec2fac7
4
- data.tar.gz: 5ca6c729d02ff390720b3faf76adffe22a10aec20236adbafe5edcd21b364955
3
+ metadata.gz: ae77258c328dc98d5333e7954d3412556a1628a9ae2797885342f3efeff9c84a
4
+ data.tar.gz: 167ea53c4fd36d27a544c304ce72632c452641f3c2978a1c1cc15e1b844e58e7
5
5
  SHA512:
6
- metadata.gz: f9b98e7f028b6386b6b2caca76690fa53e87e6d06c9a28e6967147e54abca098ed11364a54f9bc1acaf3bd489baafcb46351acbef471527d7b53f4dddad470ea
7
- data.tar.gz: b2962b48817a7a5a5803484f0ff82a335d955e140b3a9b5003a91c538612842918cbaf24992e12d7ce630a1e1b866786a781290a75ccecf78af4f7fa8efd0505
6
+ metadata.gz: 843c6ca1105dca45e8ea3b53c47933db4e650451b29294855a80a7a84186804d7565f49309fdf2fcbdea30c43d605caea2bf1a5b65c0c6fc90501b896ee8bf23
7
+ data.tar.gz: 248f51b62caab9925452779161b2e910eb06aaa62b9c1e886d75bb040b1feb613d7cc9605ddad5aa0c2d2ab7fda5e92dbf0b95b398c278bd287098a351234f73
data/README.md CHANGED
@@ -20,13 +20,13 @@ indent(2, "* ").puts "Indented two levels"
20
20
 
21
21
  outputs
22
22
 
23
- ```ruby
23
+ ```
24
24
  Not indented
25
25
  Indented one level
26
26
  * * Indented two levels
27
27
  ```
28
28
 
29
- #### `#indent`
29
+ #### `Kernel#indent`, `IO#indent`, and `StringIO#indent`
30
30
 
31
31
  `#indent` without a block returns an `IndentedIO` object that acts as the
32
32
  receiver but redefine `#print`, `#printf`, `#puts`, and `#p` to print indented
@@ -201,7 +201,7 @@ git commits and tags, and push the `.gem` file to
201
201
  ## Contributing
202
202
 
203
203
  Bug reports and pull requests are welcome on GitHub at
204
- https://github.com/[USERNAME]/indented_io.
204
+ https://github.com/clrgit/indented_io.
205
205
 
206
206
  ## License
207
207
 
@@ -1,6 +1,13 @@
1
1
 
2
2
  module IndentedIO
3
- # Error class
3
+ # Error class. To rescue errors form IndentedIO do
4
+ #
5
+ # begin
6
+ # do_some_stuff()
7
+ # rescue IndentedIO::Error => ex
8
+ # handle_error()
9
+ # end
10
+ #
4
11
  class Error < RuntimeError; end
5
12
  end
6
13
 
@@ -8,26 +8,18 @@ module IndentedIO
8
8
  # levels rise or fall IndentedIO objects are moved on and off the stack
9
9
  #
10
10
  # Note that #new is private. The only way to create a IndentedIO object is to
11
- # call #indent on a object that supports it
11
+ # call #indent on an object that supports it
12
12
  class IndentedIO
13
13
  include IndentedIOInterface
14
14
 
15
- # :nodoc:
15
+ # @!visibility private
16
16
  alias :interface_indent :indent
17
- # :startdoc:
18
17
 
19
- # Return a IndentedIO object
20
- #
21
- # See IndentedIO::IndentedIOInterface#indent for documentation
18
+ # (see IndentedIO::IndentedIOInterface#indent)
22
19
  def indent(levels=1, string_ = self.this_indent, string: string_, bol: nil, &block)
23
20
  interface_indent(levels, string, bol: bol, &block)
24
21
  end
25
22
 
26
- # Current level
27
- def level
28
- @level ||= @levels + (parent.is_a?(::IndentedIO::IndentedIO) ? parent.level : 0)
29
- end
30
-
31
23
  # Indent and print args to the underlying device. #print has the same semantic
32
24
  # as Kernel#print
33
25
  def print(*args)
@@ -74,14 +66,14 @@ module IndentedIO
74
66
  args.size == 1 ? args.first : args
75
67
  end
76
68
 
77
- # :stopdoc:
78
-
79
69
  # Make IndentedIO behave like the underlying @device
70
+ # @!visibility private
80
71
  def respond_to?(method)
81
72
  [:indent, :level, :print, :puts, :p].include?(method) || device.respond_to?(method)
82
73
  end
83
74
 
84
75
  # Make IndentedIO behave like the underlying @device
76
+ # @!visibility private
85
77
  def method_missing(method, *args)
86
78
  device.send(method, *args)
87
79
  end
@@ -119,6 +111,11 @@ module IndentedIO
119
111
  @base.instance_variable_set(:@bol, bol) # @bol only exists in the #base object
120
112
  end
121
113
 
114
+ # Current level
115
+ def level
116
+ @level ||= @levels + (parent.is_a?(::IndentedIO::IndentedIO) ? parent.level : 0)
117
+ end
118
+
122
119
  # Hide new
123
120
  private_class_method :new
124
121
 
@@ -151,6 +148,7 @@ module IndentedIO
151
148
  end
152
149
 
153
150
  public
151
+ # @!visibility private
154
152
  def dump
155
153
  $stderr.puts "#{self.class} [#{self.object_id}]"
156
154
  $stderr.puts " device: #{device.class} [#{device.object_id}]"
@@ -1,25 +1,36 @@
1
1
  module IndentedIO
2
2
  # IndentedIO interface that provides the #indent method. Used by IO,
3
3
  # StringIO, and IndentedIO. It can be included in any class that define a
4
- # #print method
4
+ # #print method like this:
5
+ #
6
+ # require 'indented_io'
7
+ # class MyIO
8
+ # include IndentedIO::IndentedIOInterface
9
+ # def print(*args) ... end
10
+ # end
11
+ #
12
+ # my_io = MyIO.new
13
+ # my_io.print "Not indented\n"
14
+ # my_io.indent.puts "It works!"
15
+ #
16
+ # # Not indented
17
+ # # It works!
18
+ #
5
19
  module IndentedIOInterface
6
20
  # Returns a IndentedIO object that can be used for printing. The IO object
7
- # will pass-through all method calls except #print, #printf, #puts, and #p
8
- # to the enclosing object
21
+ # will pass-through all method to the underlying device except #print,
22
+ # #printf, #puts, and #p
9
23
  #
10
- # :call-seq:
11
- # indent(levels = 1)
12
- # indent(levels, string)
13
- # indent(levels, string: indent_string, bol: beginning_of_line)
14
- # indent(levels, string, bol: beginning_of_line)
24
+ # +level+ is the number of leves to indent and +string+ is the string used
25
+ # for indentation. The indentation string can also be given as the keyword
26
+ # parameter +:string+. Default is the indent string of the outer level or
27
+ # {::IndentedIO.default_indent} if this is the first level. +:bol+ control the
28
+ # beginning-of-line status: If true, #indent will begin writing with an
29
+ # indentation string as if it was at the beginning of the line. If false,
30
+ # it will only indent after the next newline. Default is true
31
+ #
32
+ # If +level+ is negative, #indent will outdent text instead
15
33
  #
16
- # +levels+:: Number of indentation levels. Default is one level
17
- # +string+:: The indentation string. Default is the indent string of the
18
- # outer level or ::IndentedIO.default_indent if this is the
19
- # first level
20
- # +bol+:: Beginning of line. If true, #indent will begin writing with an
21
- # indentation string. If false, it will only indent after the next
22
- # newline. Default true
23
34
  def indent(levels = 1, string_ = ::IndentedIO.default_indent, string: string_, bol: nil, &block)
24
35
  block.nil? || block.arity == 1 or raise ::IndentedIO::Error.new "Wrong number of parameters"
25
36
  obj = ::IndentedIO::IndentedIO.send(:new, self, levels, string, bol)
@@ -1,6 +1,6 @@
1
1
  require 'indented_io/indented_io_interface'
2
2
 
3
- # :nodoc:
3
+ # Includes the IndentedIOInterface that define the #indent method
4
4
  class IO
5
5
  include IndentedIO::IndentedIOInterface
6
6
  end
@@ -1,7 +1,22 @@
1
1
  require 'indented_io/indented_io_interface'
2
2
 
3
- # :nodoc:
4
3
  module Kernel
4
+ # Like {IndentedIO::IndentedIOInterface#indent} except the underlying device is
5
+ # not the receiver (Kernel) but $stdout. Kernel#indent also allows a block without
6
+ # and argument. In that case it manipulates $stdout to print indented:
7
+ #
8
+ # puts "Not indented
9
+ # indent {
10
+ # puts "Indented"
11
+ # indent {
12
+ # puts "Even more indented"
13
+ # }
14
+ # }
15
+ #
16
+ # # Not indented
17
+ # # Indented
18
+ # # Even more indented
19
+ #
5
20
  def indent(levels = 1, string_ = IndentedIO.default_indent, string: string_, bol: nil, &block)
6
21
  block.nil? || block.arity <= 1 or raise IndentedIO::Error.new "Wrong number of parameters"
7
22
  obj = IndentedIO::IndentedIO.send(:new, $stdout, levels, string, bol)
@@ -1,6 +1,6 @@
1
1
  require 'indented_io/indented_io_interface'
2
2
 
3
- # :nodoc:
3
+ # Includes the IndentedIOInterface that define the #indent method
4
4
  class StringIO
5
5
  include IndentedIO::IndentedIOInterface
6
6
  end
@@ -1,4 +1,4 @@
1
1
  module IndentedIO
2
2
  # Version number
3
- VERSION = "0.7.0"
3
+ VERSION = "0.7.1"
4
4
  end
data/lib/indented_io.rb CHANGED
@@ -10,11 +10,11 @@ require 'indented_io/stringio'
10
10
  # IndentedIO module
11
11
 
12
12
  module IndentedIO
13
- # Returns default indentation. ' ' (two spaces) is the default but it can be
14
- # set by #default_indent
13
+ # Returns default indentation. Two spaces is the default but it can be set by
14
+ # #default_indent
15
15
  def self.default_indent() @DEFAULT_INDENT end
16
16
 
17
- # Set default indentation
17
+ # Sets default indentation
18
18
  def self.default_indent=(indent) @DEFAULT_INDENT = indent end
19
19
 
20
20
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: indented_io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen