Class: Jig::Gap
- Inherits:
-
Object
- Object
- Jig::Gap
- Defined in:
- lib/jig.rb
Overview
A Gap represents a named position within the ordered sequence of objects stored in a jig. In addition to a name, a gap can also have an associated filter. When a gap is filled by a plug operation, the replacement items are passed to the filter and the return value(s) are used to fill the gap. The default filter simply returns the unchanged list of items.
Constant Summary collapse
- ATTRS =
:__a
- GAP =
:___
Instance Attribute Summary collapse
-
#filter ⇒ Object
readonly
the lambda associated with the gap.
-
#name ⇒ Object
readonly
the name associated with the gap.
Class Method Summary collapse
-
.comment(prefix = "", open = nil, close = nil, width = 72, name = GAP) ⇒ Object
Construct a new gap, name.
-
.wrap(width = 72, name = GAP) ⇒ Object
Construct a new gap, _name..
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two gaps are equal if they have the same name and use the same filter.
-
#fill(*filling) ⇒ Object
Pass the replacement items through the filter.
-
#initialize(name = GAP, &filter) ⇒ Gap
constructor
Construct a new gap with the specified name.
- #inspect ⇒ Object
-
#rename(name) ⇒ Object
Change the name of the gap.
- #terse_inspect ⇒ Object
Constructor Details
#initialize(name = GAP, &filter) ⇒ Gap
Construct a new gap with the specified name. A block, if given, becomes the filter for replacement items.
64 65 66 67 |
# File 'lib/jig.rb', line 64 def initialize(name=GAP, &filter) @name = name.to_sym @filter = filter && lambda(&filter) end |
Instance Attribute Details
#filter ⇒ Object (readonly)
the lambda associated with the gap
60 61 62 |
# File 'lib/jig.rb', line 60 def filter @filter end |
#name ⇒ Object (readonly)
the name associated with the gap
57 58 59 |
# File 'lib/jig.rb', line 57 def name @name end |
Class Method Details
.comment(prefix = "", open = nil, close = nil, width = 72, name = GAP) ⇒ Object
Construct a new gap, name. This gap will try to re-format lines of text into a single or multi-line comment block with each line of text limited to width columns.
If prefix is provided, each line of text will be prefixed accordingly. If open is provided, a single line of text will be wrapped with the open and close strings but multiple lines of text will be formatted as a block comment. If close is not provided it is taken to be the open.reverse.
Jig[Jig::Gap.comment] # text reformated to 72 columns
Jig[Jig::Gap.comment("# ")] # text reformated as Ruby comments
Jig[Jig::Gap.comment("// ")] # text reformated as Javascript comments
Jig[Jig::Gap.comment(" *", "/* ")] # text reformated as C comments
If the default gap name isn’t appropriate you must fill in all the arguments:
Jig[Jig::Gap.comment("# ", nil, nil, 72, :alternate)] # alternate gap name
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/jig.rb', line 123 def self.comment(prefix="", open=nil, close=nil, width=72, name=GAP) wrap = Jig[Jig::Gap.wrap(width, name)] if open close ||= open.reverse block_line = Jig.new(prefix, " ", GAP, "\n") one_line = Jig.new(open, GAP, close, "\n") block = Jig.new(open, "\n", GAP, close, "\n") else one_line = Jig.new(prefix, GAP, "\n") block_line = one_line block = Jig.new end new(name) do |plug| text = (wrap % plug.to_s).to_s if text.index("\n") block % (block_line * text.split(/\n/)) else block % (one_line % text) end end end |
.wrap(width = 72, name = GAP) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/jig.rb', line 101 def self.wrap(width=72, name=GAP) new(name) do |plug| # From James Edward Gray II's entry to Ruby Quiz #113 [ruby-talk:238693] plug.to_s.sub("\n"," ").strip.gsub(/(.{1,#{width}}|\S{#{width+1},})(?: +|$\n?)/, "\\1\n").chomp end end |
Instance Method Details
#==(other) ⇒ Object
Two gaps are equal if they have the same name and use the same filter.
84 85 86 87 88 |
# File 'lib/jig.rb', line 84 def ==(other) self.class == other.class && name == other.name && filter == other.filter end |
#fill(*filling) ⇒ Object
Pass the replacement items through the filter.
78 79 80 |
# File 'lib/jig.rb', line 78 def fill(*filling) return *(filter && filter[*filling] || filling) end |
#inspect ⇒ Object
69 70 71 |
# File 'lib/jig.rb', line 69 def inspect "#<#{self.class.name}: [#{name}, #{filter.inspect}]>" end |
#rename(name) ⇒ Object
Change the name of the gap.
91 92 93 94 |
# File 'lib/jig.rb', line 91 def rename(name) @name = name.to_sym self end |
#terse_inspect ⇒ Object
73 74 75 |
# File 'lib/jig.rb', line 73 def terse_inspect filter && "#{name}{}".to_sym || name end |