commentbox 0.1.2 → 0.2.1

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/commentbox.rb +40 -16
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 922e9531c4f2d9e403c9343697e58086c40217a625de7b66b8b145c619b8eae9
4
- data.tar.gz: 6d4ba601200f5fef70d43203dbc4e2ae4cd76d105c5e884cef96fccee26627ba
3
+ metadata.gz: a43575a5fce78f88644c9bb88e0291a89455ab0431d28ce196fd7c9297817f45
4
+ data.tar.gz: 4da8a222cdb0bbc9051ac63f42d394754b62d51b1e2582517be3ade0938a7003
5
5
  SHA512:
6
- metadata.gz: f862760a55925ce00d08746251bc8512189e0acd301e6bb4c45d84cc42115da0fd97eb64eb508429ef959021c48e14361ddafe0e24e6ece5d9d9e09a776d4667
7
- data.tar.gz: 9eed5ab62484e8acefad8dccc13ddd843555adcac89eb6cc37a23bbd4cf06f3ae760475e75f498ce3b678ea7291ea79e67ad46310d164824947b7d5857d10df9
6
+ metadata.gz: b3c437d82fe9a730adc67f3c54a67d76dd7dd6cc30851d173d24f725ce16cc33a043b105bbe640033455e916a4ad3596e896198e6e1c46a9867e298d3604fcaa
7
+ data.tar.gz: 33e51c50a9d063ae040b9071d325e2f32dd1535b19a7266e5af3d25551c3dd00cad85c6f34273e967169e661ad338a7aca047d4e576c3cbf2d35d81957972e95
data/lib/commentbox.rb CHANGED
@@ -1,10 +1,11 @@
1
+
1
2
  module CommentBoxStyles
2
- private
3
3
  DefaultParams = {
4
4
  style: :stub,
5
5
  padding: 4,
6
6
  stretch: 0,
7
7
  offset: 2,
8
+ min_width: 0,
8
9
  spacelines: true,
9
10
  alignment: :left
10
11
  }
@@ -15,7 +16,7 @@ private
15
16
  evenlines: ['/ ', ' /'],
16
17
  oddcorners: ['=/','/=']
17
18
  },
18
- bars: {
19
+ parallax: {
19
20
  hlines: '==',
20
21
  oddlines: ['||', '||'],
21
22
  evenlines: ['||', '||'],
@@ -36,12 +37,12 @@ private
36
37
  }
37
38
  end
38
39
 
39
-
40
40
  module CommentBoxIntegerExtensions
41
41
  def is_even?
42
42
  self % 2 == 0
43
43
  end
44
44
  end
45
+
45
46
  module CommentBoxStringExtensions
46
47
  Integer.prepend CommentBoxIntegerExtensions
47
48
  def justify_to (length)
@@ -73,11 +74,30 @@ end
73
74
  class CommentBox
74
75
  String.prepend CommentBoxStringExtensions # string align/justify methods
75
76
  Integer.prepend CommentBoxIntegerExtensions # integer is_even? method
76
- include CommentBoxStyles
77
+ @@default_params = CommentBoxStyles::DefaultParams
78
+ @@styles = CommentBoxStyles::Styles
79
+
80
+ # class methods for messing with default values
81
+ def self.default_params; @@default_params end
82
+ def self.default_params=(value); @@default_params = value end
83
+ def self.set_default_params (value = {}); value.each { |key, val| @@default_params[key] = val } end
84
+ def self.styles; @@styles end
85
+ def self.add_style(value)
86
+ value.each do |key, val|
87
+ if val[:default] || val[:default?]
88
+ @@default_params[:style] = key
89
+ break # set the first default we find and stop the madness immediately
90
+ end
91
+ end
92
+ # I could attempt to remove a :default? key here but it doesn't matter
93
+ @@styles.merge! value
94
+ end
77
95
 
96
+ # instance setter methods
78
97
  attr_writer :padding, :spacelines, :alignment, :offset
79
98
  # I don't know how to call param= methods from the inside so I just use a set_param in private
80
- def text= (value); set_text value; self end; def alignment= (value); set_alignment value; self end
99
+ def text= (value); set_text value; self end;
100
+ def alignment= (value); set_alignment value; self end
81
101
  def style= (value); set_style value; self end
82
102
  # there is no @stretch, stretch is just added to @max_line_length
83
103
  # but, if stretch= is called, then @max_line_length must be recalculated
@@ -86,7 +106,8 @@ class CommentBox
86
106
  if !@max_line_length.is_even? then @max_line_length += 1 end
87
107
  self end
88
108
 
89
- def initialize (params) # params: {text: String or Array, style: Symbol, padding: Integer, spacelines: Boolean, alignment: Symbol or Array}
109
+ # needs more reliance on setter methods. changing instance variables after initialization is a total house of cards right now
110
+ def initialize (params) # params: {text: String or Array, style: Symbol, padding: Integer, spacelines: Boolean, alignment: Symbol or Array, and some others}
90
111
  # for now require an argument of some sort
91
112
  if params.class != Hash && params.class != String then raise 'CommentBox#initialize : you gotta initialize this with Hash or String.' end
92
113
 
@@ -94,20 +115,22 @@ class CommentBox
94
115
  if params.is_a? String then params = {text: params} end
95
116
 
96
117
  # fill in a bunch of instance variables from params or default values
97
- # style_symbol = params[:style] || DefaultParams[:style]; @style = Styles[style_symbol]
98
- @padding = params[:padding] || DefaultParams[:padding]
99
- @offset = params[:offset] || DefaultParams[:offset]
118
+ @padding = params[:padding] || @@default_params[:padding]
119
+ @offset = params[:offset] || @@default_params[:offset]
100
120
  # one of the options for this is false, so it's not gonna play nice with ||
101
- @spacelines = (params[:spacelines] != nil) ? params[:spacelines] : DefaultParams[:spacelines]
121
+ @spacelines = (params[:spacelines] != nil) ? params[:spacelines] : @@default_params[:spacelines]
122
+ @stretch = (params[:stretch] || @@default_params[:stretch]).to_i
123
+ @min_width = (params[:min_width] || @@default_params[:min_width]).to_i
102
124
 
103
125
  # call on some special methods to parse text and alignment
104
126
  set_text params[:text]
105
127
  set_alignment params[:alignment]
106
128
  set_style params[:style]
107
- @max_line_length += (params[:stretch] || DefaultParams[:stretch]).to_i
129
+ # @max_line_length += (params[:stretch] || @@default_params[:stretch]).to_i
108
130
  if !@max_line_length.is_even? then @max_line_length += 1 end
109
131
  end
110
132
  def to_s
133
+ # exact value of spaceLine is calculated if @spacelines is true, otherwise it's just an empty string
111
134
  spaceLine = @spacelines ? [@style[:oddlines][0], ' ' * (@max_line_length + @padding * 2), @style[:oddlines][1], "\n"].join : ''
112
135
 
113
136
  # construct an array of lines, join them together and return
@@ -130,14 +153,15 @@ private
130
153
  else
131
154
  raise 'CommentBox#text= : expected String or Array here'
132
155
  end
133
- @max_line_length = @text.map(&:length).max
156
+ @max_line_length = @text.map(&:length).max + @stretch
157
+ @max_line_length = @min_width unless @max_line_length > @min_width
134
158
  if !@max_line_length.is_even? then @max_line_length += 1 end
135
159
  insert_line_if_even
136
160
  end
137
161
  def set_alignment (align)
138
162
  if align == nil
139
163
  # set default value if no argument is given
140
- @alignment = [ DefaultParams[:alignment] ] * (@text.size)
164
+ @alignment = [ @@default_params[:alignment] ] * (@text.size)
141
165
  else
142
166
  # fill array with align if one symbol is given
143
167
  if align.is_a? Symbol
@@ -158,10 +182,10 @@ private
158
182
  insert_line_if_even
159
183
  end
160
184
  def set_style (style)
161
- if style == nil then @style = Styles[DefaultParams[:style]]
162
- elsif style.is_a? Symbol then @style = Styles[style]
185
+ if style == nil then @style = @@styles[@@default_params[:style]]
186
+ elsif style.is_a? Symbol then @style = @@styles[style]
163
187
  elsif style.is_a? Hash then @style = style
164
- elsif style.is_a? String then @style = Styles[style.to_sym]
188
+ elsif style.is_a? String then @style = @@styles[style.to_sym]
165
189
  else raise 'CommentBox#style= : expected Symbol, Hash, or String here' end
166
190
  end
167
191
  def insert_line_if_even # also will delete a blank line if there is one
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commentbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leonard H. Phelan IV
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-31 00:00:00.000000000 Z
11
+ date: 2023-01-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A little gem for generating pretty multiline comment boxes in C/C++ templates
14
14
  email: lenny@lenny.beer