Class: CommentBox

Inherits:
Object
  • Object
show all
Defined in:
lib/commentbox.rb

Constant Summary collapse

@@default_params =
CommentBoxStyles::DefaultParams
@@styles =
CommentBoxStyles::Styles

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ CommentBox

needs more reliance on setter methods. changing instance variables after initialization is a total house of cards right now



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/commentbox.rb', line 122

def initialize (params) # params: {text: String or Array, style: Symbol, padding: Integer, spacelines: Boolean, alignment: Symbol or Array, and some others}
  # for now require an argument of some sort
  if params.class != Hash && params.class != String then raise 'CommentBox#initialize : you gotta initialize this with Hash or String.' end
  
  # if it's not a hash, then make it one.
  if params.is_a? String then params = {text: params} end 

  # fill in a bunch of instance variables from params or default values
  @padding = (params[:padding] || @@default_params[:padding]).to_i
  @offset = (params[:offset] || @@default_params[:offset]).to_i
  # one of the options for this is false, so it's not gonna play nice with ||
  @spacelines = (params[:spacelines] != nil) ? params[:spacelines] : @@default_params[:spacelines] 
  @stretch = (params[:stretch] || @@default_params[:stretch]).to_i
  @min_width = (params[:min_width] || @@default_params[:min_width]).to_i

  # call on some special methods to parse text and alignment
  set_text params[:text]
  set_alignment params[:alignment] 
  set_style params[:style]
  # @max_line_length += (params[:stretch] || @@default_params[:stretch]).to_i
  if !@max_line_length.is_even? then @max_line_length += 1 end
end

Instance Attribute Details

#alignment=(value) ⇒ Object (writeonly)

instance setter methods



109
110
111
# File 'lib/commentbox.rb', line 109

def alignment=(value)
  @alignment = value
end

#offset=(value) ⇒ Object (writeonly)

instance setter methods



109
110
111
# File 'lib/commentbox.rb', line 109

def offset=(value)
  @offset = value
end

#padding=(value) ⇒ Object (writeonly)

instance setter methods



109
110
111
# File 'lib/commentbox.rb', line 109

def padding=(value)
  @padding = value
end

#spacelines=(value) ⇒ Object (writeonly)

instance setter methods



109
110
111
# File 'lib/commentbox.rb', line 109

def spacelines=(value)
  @spacelines = value
end

Class Method Details

.add_style(value) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/commentbox.rb', line 96

def self.add_style(value)
  value.each do |key, val|
    if val[:default] || val[:default?] 
      @@default_params[:style] = key
      break # set the first default we find and stop the madness immediately
    end
  end
  # I could attempt to remove a :default? key here but it doesn't matter
  # this is a terse one but trust me
  @@styles.merge! value.transform_keys(&:to_sym).transform_values { |v| v.transform_keys(&:to_sym) }
end

.default_paramsObject

class methods for messing with default values make sure everything is symbolized that needs to be



91
# File 'lib/commentbox.rb', line 91

def self.default_params; @@default_params end

.default_params=(value) ⇒ Object



92
# File 'lib/commentbox.rb', line 92

def self.default_params=(value); @@default_params = value.transform_keys(&:to_sym) end

.set_default_params(value = {}) ⇒ Object

def self.default_params[]= (key, value); @@default_params = value end



94
# File 'lib/commentbox.rb', line 94

def self.set_default_params (value = {}); value.each { |key, val| @@default_params[key.to_sym] = val } end

.stylesObject



95
# File 'lib/commentbox.rb', line 95

def self.styles; @@styles end

Instance Method Details

#stretch=(value) ⇒ Object

there is no @stretch, stretch is just added to @max_line_length but, if stretch= is called, then @max_line_length must be recalculated



116
117
118
119
# File 'lib/commentbox.rb', line 116

def stretch= (value)
  @max_line_length = @text.map(&:length).max + value
  if !@max_line_length.is_even? then @max_line_length += 1 end
self end

#style=(value) ⇒ Object



113
# File 'lib/commentbox.rb', line 113

def style= (value); set_style value; self end

#text=(value) ⇒ Object

I don’t know how to call param= methods from the inside so I just use a set_param in private



111
# File 'lib/commentbox.rb', line 111

def text= (value); set_text value; self end

#to_sObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/commentbox.rb', line 144

def to_s
  # exact value of spaceLine is calculated if @spacelines is true, otherwise it's just an empty string
  spaceLine = @spacelines ? [@style[:oddlines][0], ' ' * (@max_line_length + @padding * 2), @style[:oddlines][1], "\n"].join : ''
  
  # construct an array of lines, join them together and return
  return [
    t_line(:begin),
    spaceLine,
    (0..@text.size - 1).map { |line| fmt_text_line line }, # this is a nested array and must be flattened
    spaceLine,
    t_line(:end)
  # flatten, map offset in front of each line if it's not empty, join them together, and remove trailing newline
  ].flatten.map { |line| line == '' ? '' : (' ' * @offset) << line }.join.chomp 
end