slither 0.99.4 → 0.99.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +22 -0
- data/CHANGELOG.md +26 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +63 -0
- data/LICENSE.txt +21 -0
- data/README.md +95 -0
- data/Rakefile +12 -37
- data/TODO +2 -1
- data/lib/slither/column.rb +96 -76
- data/lib/slither/definition.rb +28 -13
- data/lib/slither/generator.rb +46 -26
- data/lib/slither/parser.rb +118 -53
- data/lib/slither/section.rb +39 -21
- data/lib/slither/version.rb +5 -0
- data/lib/slither.rb +68 -7
- data/sig/slither.rbs +4 -0
- metadata +81 -82
- data/History.txt +0 -16
- data/README.rdoc +0 -100
- data/lib/slither/slither.rb +0 -49
- data/slither.gemspec +0 -0
- data/spec/column_spec.rb +0 -224
- data/spec/definition_spec.rb +0 -85
- data/spec/generator_spec.rb +0 -42
- data/spec/parser_spec.rb +0 -74
- data/spec/section_spec.rb +0 -146
- data/spec/slither_spec.rb +0 -84
- data/spec/spec_helper.rb +0 -4
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 67bb6ad8dd4ac38f63231d77f9e366543cf9b11589e89760da6c85da9912995b
|
4
|
+
data.tar.gz: 9b7be1bf9747a39d72ee06097226676c2d7c7a634f56fcbb290bf5d665b23fbb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 72590afbfbceec05c54e67fa4f534c16207579a6fc3412ff19739e7ecc805864e044f6625b468827cb6dae7d627960a7b24895b25cced9b57ea5884bb217a82e
|
7
|
+
data.tar.gz: e695c7f0871aae93d0fb2ae8c8e7cd10e36737304f5f9b244009728f44153bd9028c6f3085c0c433eb6e703cb4ba558c11e27479e80c416d460448b5eeed8a38
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
AllCops:
|
2
|
+
NewCops: enable
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
8
|
+
Style/StringLiteralsInInterpolation:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Layout/LineLength:
|
13
|
+
Max: 120
|
14
|
+
|
15
|
+
Metrics/BlockLength:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Metrics/MethodLength:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Style/Documentation:
|
22
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# 0.99.5
|
2
|
+
* Use bundler to define the base skeleton of the Gem
|
3
|
+
* Specify dependencies so far: rspec, rubocop and pry
|
4
|
+
* Specify Ruby version (>3.0)
|
5
|
+
* Add some docs-comments
|
6
|
+
* Update specs to rspec 3.0
|
7
|
+
|
8
|
+
## 0.99.4
|
9
|
+
|
10
|
+
## 0.99.3
|
11
|
+
|
12
|
+
## 0.99.2 / 2009-04-28
|
13
|
+
|
14
|
+
* Added better support for float formatting
|
15
|
+
* Added the money_with_implied_decimal type
|
16
|
+
|
17
|
+
## 0.99.1 / 2009-04-22
|
18
|
+
|
19
|
+
* Make the missing method build a column i.e. body.record_type 1
|
20
|
+
* Prevent duplicate column names
|
21
|
+
* Better error messages
|
22
|
+
* Implement custom padding (spaces (default), zero)
|
23
|
+
|
24
|
+
## 0.99.0 / 2009-04-14
|
25
|
+
|
26
|
+
* Initial Release
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jorge.mgg94@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
slither (0.99.5)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
coderay (1.1.3)
|
11
|
+
diff-lcs (1.5.0)
|
12
|
+
json (2.6.3)
|
13
|
+
method_source (1.0.0)
|
14
|
+
parallel (1.22.1)
|
15
|
+
parser (3.2.0.0)
|
16
|
+
ast (~> 2.4.1)
|
17
|
+
pry (0.14.1)
|
18
|
+
coderay (~> 1.1)
|
19
|
+
method_source (~> 1.0)
|
20
|
+
rainbow (3.1.1)
|
21
|
+
rake (13.0.6)
|
22
|
+
regexp_parser (2.7.0)
|
23
|
+
rexml (3.2.5)
|
24
|
+
rspec (3.12.0)
|
25
|
+
rspec-core (~> 3.12.0)
|
26
|
+
rspec-expectations (~> 3.12.0)
|
27
|
+
rspec-mocks (~> 3.12.0)
|
28
|
+
rspec-core (3.12.1)
|
29
|
+
rspec-support (~> 3.12.0)
|
30
|
+
rspec-expectations (3.12.2)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.12.0)
|
33
|
+
rspec-mocks (3.12.3)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.12.0)
|
36
|
+
rspec-support (3.12.0)
|
37
|
+
rubocop (1.45.1)
|
38
|
+
json (~> 2.3)
|
39
|
+
parallel (~> 1.10)
|
40
|
+
parser (>= 3.2.0.0)
|
41
|
+
rainbow (>= 2.2.2, < 4.0)
|
42
|
+
regexp_parser (>= 1.8, < 3.0)
|
43
|
+
rexml (>= 3.2.5, < 4.0)
|
44
|
+
rubocop-ast (>= 1.24.1, < 2.0)
|
45
|
+
ruby-progressbar (~> 1.7)
|
46
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
47
|
+
rubocop-ast (1.24.1)
|
48
|
+
parser (>= 3.1.1.0)
|
49
|
+
ruby-progressbar (1.11.0)
|
50
|
+
unicode-display_width (2.4.2)
|
51
|
+
|
52
|
+
PLATFORMS
|
53
|
+
ruby
|
54
|
+
|
55
|
+
DEPENDENCIES
|
56
|
+
pry
|
57
|
+
rake (~> 13.0)
|
58
|
+
rspec (~> 3.0)
|
59
|
+
rubocop (~> 1.21)
|
60
|
+
slither!
|
61
|
+
|
62
|
+
BUNDLED WITH
|
63
|
+
2.3.25
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Jorge Garcia
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# Slither
|
2
|
+
by Ryan Wood http://ryanwood.com
|
3
|
+
|
4
|
+
# Description:
|
5
|
+
|
6
|
+
A simple, clean DSL for describing, writing, and parsing fixed-width text files.
|
7
|
+
|
8
|
+
## Features:
|
9
|
+
|
10
|
+
* Easy DSL syntax
|
11
|
+
* Can parse and format fixed width files
|
12
|
+
* Templated sections for reuse
|
13
|
+
* Helpful error messages for invalid data
|
14
|
+
|
15
|
+
## Synopsis:
|
16
|
+
|
17
|
+
Create a Slither::Defintion to describe a file format
|
18
|
+
```ruby
|
19
|
+
Slither.define :simple, :by_bytes => false do |d|
|
20
|
+
|
21
|
+
# This is a template section that can be reused in other sections
|
22
|
+
d.template :boundary do |t|
|
23
|
+
t.column :record_type, 4
|
24
|
+
t.column :company_id, 12
|
25
|
+
end
|
26
|
+
|
27
|
+
# Create a header section
|
28
|
+
# alternatively, you can define the section on the fly by using metaprogramming
|
29
|
+
# ex: d.header(:align => :left) { |header| ... }
|
30
|
+
d.section(:header, :align => :left) do |header|
|
31
|
+
# The trap tells Slither which lines should fall into this section
|
32
|
+
header.trap { |line| line[0,4] == 'HEAD' }
|
33
|
+
# Use the boundary template for the columns
|
34
|
+
header.template :boundary
|
35
|
+
end
|
36
|
+
|
37
|
+
d.section(:body) do |body|
|
38
|
+
body.trap { |line| line[0,4] =~ /[^(HEAD|FOOT)]/ }
|
39
|
+
body.column :id, 10, :type => :integer
|
40
|
+
body.column :name, 10, :align => :left
|
41
|
+
body.spacer 3
|
42
|
+
body.column :state, 2
|
43
|
+
end
|
44
|
+
|
45
|
+
d.section(:footer) do |footer|
|
46
|
+
footer.trap { |line| line[0,4] == 'FOOT' }
|
47
|
+
footer.template :boundary
|
48
|
+
footer.column :record_count, 10
|
49
|
+
end
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
Supported types are: `string, integer, date, float, money, and money_with_implied_decimal`.
|
54
|
+
|
55
|
+
Use `:by_bytes => true` (default) to allow newlines within rows and specify length in bytes.
|
56
|
+
Use `:by_bytes => false` to support sections of different lengths and length specification
|
57
|
+
in number of characters.
|
58
|
+
|
59
|
+
Then either feed it a nested struct with data values to create the file in the defined format:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
test_data = {
|
63
|
+
:body => [
|
64
|
+
{ :id => 12, :name => "Ryan", :state => 'SC' },
|
65
|
+
{ :id => 23, :name => "Joe", :state => 'VA' },
|
66
|
+
{ :id => 42, :name => "Tommy", :state => 'FL' },
|
67
|
+
],
|
68
|
+
:header => { :record_type => 'HEAD', :company_id => 'ABC' },
|
69
|
+
:footer => { :record_type => 'FOOT', :company_id => 'ABC' }
|
70
|
+
}
|
71
|
+
|
72
|
+
# Generates the file as a string
|
73
|
+
puts Slither.generate(:simple, test_data)
|
74
|
+
# =>
|
75
|
+
# HEAD ABC
|
76
|
+
# 12Ryan SC
|
77
|
+
# 23Joe VA
|
78
|
+
# 42Tommy FL
|
79
|
+
# FOOT ABC
|
80
|
+
|
81
|
+
# Writes the file
|
82
|
+
Slither.write(output_filename, :simple, test_data)
|
83
|
+
```
|
84
|
+
|
85
|
+
or parse files already in that format into a nested hash:
|
86
|
+
```ruby
|
87
|
+
parsed_data = Slither.parse(input_filename, :simple)
|
88
|
+
parsed_data = Slither.parseIo(io_object, :simple)
|
89
|
+
```
|
90
|
+
|
91
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
92
|
+
|
93
|
+
## Code of Conduct
|
94
|
+
|
95
|
+
Everyone interacting in the Slither project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ryanwood/slither/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
CHANGED
@@ -1,37 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
Bones.setup
|
14
|
-
rescue LoadError
|
15
|
-
load 'tasks/setup.rb'
|
16
|
-
end
|
17
|
-
|
18
|
-
ensure_in_path 'lib'
|
19
|
-
require 'bones'
|
20
|
-
|
21
|
-
task :default => 'spec:run'
|
22
|
-
|
23
|
-
PROJ.name = 'slither'
|
24
|
-
PROJ.authors = 'Ryan Wood'
|
25
|
-
PROJ.email = 'ryan.wood@gmail.com'
|
26
|
-
PROJ.url = 'http://github.com/ryanwood/slither'
|
27
|
-
PROJ.version = '0.99.3'
|
28
|
-
PROJ.exclude = %w(\.git .gitignore ^tasks \.eprj ^pkg)
|
29
|
-
PROJ.readme_file = 'README.rdoc'
|
30
|
-
|
31
|
-
#PROJ.rubyforge.name = 'codeforpeople'
|
32
|
-
|
33
|
-
PROJ.rdoc.exclude << '^data'
|
34
|
-
PROJ.notes.exclude = %w(^README\.rdoc$ ^data ^pkg)
|
35
|
-
|
36
|
-
# PROJ.svn.path = 'bones'
|
37
|
-
# PROJ.spec.opts << '--color'
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
require "rubocop/rake_task"
|
9
|
+
|
10
|
+
RuboCop::RakeTask.new
|
11
|
+
|
12
|
+
task default: %i[spec rubocop]
|
data/TODO
CHANGED
@@ -10,4 +10,5 @@
|
|
10
10
|
|
11
11
|
== 1.x
|
12
12
|
|
13
|
-
* Alternate Section Flow (other than linear), i.e. repeatable sections (think batch)
|
13
|
+
* Alternate Section Flow (other than linear), i.e. repeatable sections (think batch)
|
14
|
+
* Equivalent of parse by bytes, but with chars. Use "io.gets nil, num_chars_per_line"
|
data/lib/slither/column.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'date'
|
2
2
|
|
3
|
-
|
3
|
+
module Slither
|
4
4
|
class ParserError < RuntimeError; end
|
5
|
-
|
5
|
+
|
6
6
|
class Column
|
7
7
|
attr_reader :name, :length, :alignment, :type, :padding, :precision, :options
|
8
|
-
|
8
|
+
|
9
9
|
def initialize(name, length, options = {})
|
10
10
|
assert_valid_options(options)
|
11
11
|
@name = name
|
@@ -16,13 +16,13 @@ class Slither
|
|
16
16
|
@padding = options[:padding] || :space
|
17
17
|
@truncate = options[:truncate] || false
|
18
18
|
# Only used with floats, this determines the decimal places
|
19
|
-
@precision = options[:precision]
|
19
|
+
@precision = options[:precision]
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def unpacker
|
23
23
|
"A#{@length}"
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
def parse(value)
|
27
27
|
case @type
|
28
28
|
when :integer
|
@@ -40,87 +40,107 @@ class Slither
|
|
40
40
|
else value.strip
|
41
41
|
end
|
42
42
|
rescue
|
43
|
-
raise ParserError, "The value '#{value}' could not be converted to type #{@type}: #{$!}"
|
43
|
+
raise ParserError, "Error parsing column ''#{name}'. The value '#{value}' could not be converted to type #{@type}: #{$!}"
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
def format(value)
|
47
|
-
|
47
|
+
string_formatted = formatter % to_s(value)
|
48
|
+
|
49
|
+
pad(string_formatted)
|
48
50
|
rescue
|
49
51
|
puts "Could not format column '#{@name}' as a '#{@type}' with formatter '#{formatter}' and value of '#{value}' (formatted: '#{to_s(value)}'). #{$!}"
|
50
52
|
end
|
51
|
-
|
53
|
+
|
52
54
|
private
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
55
|
+
|
56
|
+
# String formatter https://www.rubyguides.com/2012/01/ruby-string-formatting/
|
57
|
+
# https://www.dotnetperls.com/format-ruby
|
58
|
+
# Generates a string formatter using padding. Supports padding to the right or left as follow:
|
59
|
+
# formatter (right side)
|
60
|
+
# => "%10s"
|
61
|
+
# This will generate a formatter of 10 char string right aligned.
|
62
|
+
#
|
63
|
+
# formatter (left side)
|
64
|
+
# => "%-10s"
|
65
|
+
# This will generate a formatter of 10 char strign left aligned.
|
66
|
+
def formatter
|
67
|
+
@formatter ||= "%#{aligner}#{sizer}s"
|
68
|
+
end
|
69
|
+
|
70
|
+
# Pad aligment, related with string formatting, we use - for left or nothing for right.
|
71
|
+
def aligner
|
72
|
+
@alignment == :left ? '-' : ''
|
73
|
+
end
|
74
|
+
|
75
|
+
def sizer
|
76
|
+
(@type == :float && @precision) ? @precision : @length
|
77
|
+
end
|
78
|
+
|
79
|
+
# Manually apply padding. sprintf only allows padding on numeric fields.
|
80
|
+
def pad(value)
|
81
|
+
return value unless @padding == :zero
|
82
|
+
|
83
|
+
matcher = @alignment == :right ? /^ +/ : / +$/
|
84
|
+
space = value.match(matcher)
|
85
|
+
|
86
|
+
return value unless space
|
87
|
+
|
88
|
+
value.gsub(space[0], '0' * space[0].size)
|
89
|
+
end
|
90
|
+
|
91
|
+
def inspect
|
92
|
+
"#<#{self.class} #{instance_variables.map{|iv| "#{iv}=>#{instance_variable_get(iv)}"}.join(', ')}>"
|
93
|
+
end
|
94
|
+
|
95
|
+
def to_s(value)
|
96
|
+
result = case @type
|
97
|
+
when :date
|
98
|
+
# If it's a DBI::Timestamp object, see if we can convert it to a Time object
|
99
|
+
unless value.respond_to?(:strftime)
|
100
|
+
value = value.to_time if value.respond_to?(:to_time)
|
101
|
+
end
|
102
|
+
if value.respond_to?(:strftime)
|
103
|
+
if @options[:format]
|
104
|
+
value.strftime(@options[:format])
|
88
105
|
else
|
89
|
-
value.
|
106
|
+
value.strftime
|
90
107
|
end
|
91
|
-
|
92
|
-
@options[:format] ? @options[:format] % value.to_f : value.to_f.to_s
|
93
|
-
when :money
|
94
|
-
"%.2f" % value.to_f
|
95
|
-
when :money_with_implied_decimal
|
96
|
-
"%d" % (value.to_f * 100)
|
97
|
-
else
|
108
|
+
else
|
98
109
|
value.to_s
|
99
|
-
|
100
|
-
|
110
|
+
end
|
111
|
+
when :float
|
112
|
+
@options[:format] ? @options[:format] % value.to_f : value.to_f.to_s
|
113
|
+
when :money
|
114
|
+
"%.2f" % value.to_f
|
115
|
+
when :money_with_implied_decimal
|
116
|
+
"%d" % (value.to_f * 100)
|
117
|
+
else
|
118
|
+
value.to_s
|
101
119
|
end
|
120
|
+
validate_size result
|
121
|
+
end
|
102
122
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
end
|
107
|
-
unless options[:padding].nil? || [:space, :zero].include?(options[:padding])
|
108
|
-
raise ArgumentError, "Option :padding only accepts :space (default) or :zero"
|
109
|
-
end
|
123
|
+
def assert_valid_options(options)
|
124
|
+
unless options[:align].nil? || [:left, :right].include?(options[:align])
|
125
|
+
raise ArgumentError, "Option :align only accepts :right (default) or :left"
|
110
126
|
end
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
127
|
+
unless options[:padding].nil? || [:space, :zero].include?(options[:padding])
|
128
|
+
raise ArgumentError, "Option :padding only accepts :space (default) or :zero"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def validate_size(result)
|
133
|
+
# Handle when length is out of range
|
134
|
+
if result.length > @length
|
135
|
+
if @truncate
|
136
|
+
start = @alignment == :left ? 0 : -@length
|
137
|
+
result = result[start, @length]
|
138
|
+
else
|
139
|
+
raise Slither::FormattedStringExceedsLengthError,
|
140
|
+
"The formatted value '#{result}' in column '#{@name}' exceeds the allowed length of #{@length} chararacters."
|
122
141
|
end
|
123
|
-
result
|
124
142
|
end
|
125
|
-
|
126
|
-
end
|
143
|
+
result
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
data/lib/slither/definition.rb
CHANGED
@@ -1,33 +1,48 @@
|
|
1
|
-
|
1
|
+
module Slither
|
2
2
|
class Definition
|
3
3
|
attr_reader :sections, :templates, :options
|
4
|
-
|
4
|
+
|
5
5
|
def initialize(options = {})
|
6
6
|
@sections = []
|
7
7
|
@templates = {}
|
8
|
-
@options = { :align => :right }.merge(options)
|
8
|
+
@options = { :align => :right, :by_bytes => true }.merge(options)
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def section(name, options = {}, &block)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
if section_using_reserved_name?(name) || section_already_defined?(name)
|
13
|
+
raise( ArgumentError, "Reserved or duplicate section name: '#{name}'")
|
14
|
+
end
|
15
|
+
|
16
16
|
section = Slither::Section.new(name, @options.merge(options))
|
17
17
|
section.definition = self
|
18
|
+
|
18
19
|
yield(section)
|
19
|
-
|
20
|
+
|
21
|
+
sections << section
|
20
22
|
section
|
21
23
|
end
|
22
|
-
|
24
|
+
|
23
25
|
def template(name, options = {}, &block)
|
24
26
|
section = Slither::Section.new(name, @options.merge(options))
|
25
27
|
yield(section)
|
26
28
|
@templates[name] = section
|
27
29
|
end
|
28
|
-
|
30
|
+
|
29
31
|
def method_missing(method, *args, &block)
|
30
32
|
section(method, *args, &block)
|
31
33
|
end
|
32
|
-
|
33
|
-
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def section_using_reserved_name?(name)
|
38
|
+
Section::RESERVED_NAMES.include?(name)
|
39
|
+
end
|
40
|
+
|
41
|
+
def section_already_defined?(name)
|
42
|
+
return false if sections.empty?
|
43
|
+
|
44
|
+
section_names = sections.map(&:name)
|
45
|
+
section_names.include?(name)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|