Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default option #13

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/slither/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize(name, length, options = {})
@type = options[:type] || :string
@padding = options[:padding] || :space
@truncate = options[:truncate] || false
@default = options[:default]
# Only used with floats, this determines the decimal places
@precision = options[:precision]
end
Expand Down Expand Up @@ -44,7 +45,7 @@ def parse(value)
end

def format(value)
pad(formatter % to_s(value))
pad(version_secure_format(value))
rescue
puts "Could not format column '#{@name}' as a '#{@type}' with formatter '#{formatter}' and value of '#{value}' (formatted: '#{to_s(value)}'). #{$!}"
end
Expand All @@ -58,6 +59,22 @@ def formatter
def aligner
@alignment == :left ? '-' : ''
end

def version_secure_format(value)
if RUBY_VERSION < "1.9" && defined?(ActiveSupport)
parsed_value(value).mb_chars.send(alignment_secure_method,sizer)
else
formatter % parsed_value(value)
end
end

def alignment_secure_method
@alignment == :left ? :ljust : :rjust;
end

def parsed_value(value)
value.nil? && @default.present? ? to_s(@default) : to_s(value)
end

def sizer
(@type == :float && @precision) ? @precision : @length
Expand Down
4 changes: 2 additions & 2 deletions lib/slither/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def generate(data)
raise(Slither::RequiredSectionEmptyError, "Required section '#{section.name}' was empty.") unless section.optional
end
end
@builder.join("\n")
@builder.join(@definition.options[:new_line] || "\n")
end

end
end
end