fixed_width_dsl 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/fixed_width_dsl.rb +55 -0
  3. metadata +58 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6382168b3e9570d7fd8cdc41cbbc95b17cb580ef
4
+ data.tar.gz: 73d77e01d1fc6859002a4e6d322ef2880a68da2e
5
+ SHA512:
6
+ metadata.gz: 59cf7b4cb5efdea86ad0432ce42c5157bd3df652a75a2368ad87b15cc84bf5b849ee0f6f8cec0712fa0607b3b85515d22e7190369a592cd02e390947c0de0ad2
7
+ data.tar.gz: 1ca6c924886afa54d6bb95ebb29c6064b01d2c7242ed4cba2cbb7090ff2c122cefededa8024c6c78b04a778bf260d4645f8a17946a3cf57b2e3cb4bd1838d7b8
@@ -0,0 +1,55 @@
1
+ module FixedWidthDSL
2
+ class FormatString
3
+ Field = Struct.new(:name, :width, :type, :flags)
4
+
5
+ attr_reader :format_string
6
+
7
+ def initialize
8
+ @fields = []
9
+ @format_string = ''
10
+ end
11
+
12
+ def debug
13
+ output = "field | wi | fro | to | type | flags\n"
14
+ lines = []
15
+ current_column = 1
16
+ @fields.each do |field|
17
+ end_column = current_column + field.width - 1
18
+ output.concat format "%-20s | %2i | %3i | %3i | %-7s | %3s\n",
19
+ field.name,
20
+ field.width,
21
+ current_column,
22
+ end_column,
23
+ field.type,
24
+ field.flags
25
+ current_column += end_column
26
+ end
27
+ output
28
+ end
29
+
30
+ def apply attributes
31
+ values = @fields.map do |field|
32
+ attributes.fetch(field.name)
33
+ end
34
+ format format_string, *values
35
+ end
36
+
37
+ private
38
+
39
+ def field name, width, type, flags = ''
40
+ type_code = case type
41
+ when :string then 's'
42
+ when :integer then 'i'
43
+ else raise ArgumentError, "Invalid type #{ type }"
44
+ end
45
+ @format_string += "%#{ flags }#{ width }#{ type_code }"
46
+ @fields << Field.new(name, width, type, flags)
47
+ end
48
+ end
49
+
50
+ def self.define &block
51
+ format_string = FormatString.new
52
+ format_string.instance_eval &block
53
+ format_string
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fixed_width_dsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eloy Espinaco
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: A DSL to build and use format/sprintf etc without readability issues
28
+ email: eloyesp@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/fixed_width_dsl.rb
34
+ homepage: https://github.com/eloyesp/fixed_width_dsl
35
+ licenses:
36
+ - AGPL-3.0+
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.5.1
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: DSL to build readable format strings
58
+ test_files: []