Module: Eddy::Util
- Defined in:
- lib/eddy/util.rb,
lib/eddy/util/read.rb,
lib/eddy/util/time.rb,
lib/eddy/util/trim.rb,
lib/eddy/util/paths.rb,
lib/eddy/util/edi_data.rb,
lib/eddy/util/normalize.rb,
lib/eddy/util/timestamp.rb,
lib/eddy/util/new_number.rb,
lib/eddy/util/clean_folder.rb
Overview
Assorted helper functions.
Defined Under Namespace
Modules: Time
Normalize collapse
-
.normalize_id(id) ⇒ String
Given an Element Id (positive number under 1688, or I01-I64), returns a string sutable for use as a Ruby class name.
-
.normalize_name(name) ⇒ String
Convert a string to PascalCase, or UpperCamelCase Remove dashes, slashes, underscores, spaces, periods, and parens from a string then titleize it.
-
.snake_case(name) ⇒ String
Convert a string to snake_case.
- .trim_delims_from_interchange(itch, element_separator: "*", segment_separator: "~") ⇒ String
- .trim_delims_from_segment(segment, separator: "*") ⇒ String
Paths collapse
-
.data_dir ⇒ String
Directory containing EDI data & definitions.
-
.root_dir ⇒ String
Directory where the gem is located.
EDI Data collapse
-
.element_ids ⇒ Hash<String, String>
Returns a hash where the keys are Element ids and the values are unprocessed Element names.
-
.element_name_by_id(id) ⇒ Hash
Return the full name of an Element with the given id.
-
.list_built_elements ⇒ Array<String>
List names of Elements with Ruby files currently in
Eddy.config.build_dir/elements
. -
.list_element_classes ⇒ Array<String>
List the names of Elements for which Ruby classes have already been built.
-
.list_segment_classes ⇒ Array<String>
List the names of Segments for which Ruby classes have already been built.
-
.list_segment_definitions ⇒ Array<String>
List Segment definition files in
data/segments
. -
.raw_element_data ⇒ Array<Hash>
Return raw data from
data/elements.tsv
. -
.segment_ids ⇒ Hash<String, String>
Returns a hash where the keys are Segment ids and the values are unprocessed Segment names.
-
.segment_name_by_id(id) ⇒ Hash
Return the full name of a Segment with the given id.
Class Method Summary collapse
-
.clean_folder(path) ⇒ Integer
Delete all files from a folder; returns number of deleted files.
-
.new_number(existing) ⇒ Integer
Given an array of numbers, returns the lowest number not included in the array.
-
.parse_tsv(filepath) ⇒ Array<Hash{Symbol => String}>
Read a TSV file and return its contents as an array of hashes.
-
.read_json_or_yaml(path, symbolize: true) ⇒ Hash{Symbol => Object}
Read data in from either a JSON or YAML file.
- .timestamp ⇒ String
Class Method Details
.clean_folder(path) ⇒ Integer
Delete all files from a folder; returns number of deleted files.
Fails if the folder contains any symlinks.
9 10 11 12 13 14 15 |
# File 'lib/eddy/util/clean_folder.rb', line 9 def self.clean_folder(path) dir = File.(path) return 0 unless Dir.exist?(dir) children = Dir.entries(dir).select { |f| File.file?(File.join(dir, f)) } deleted = File.unlink(*children) return deleted end |
.data_dir ⇒ String
Directory containing EDI data & definitions.
13 14 15 |
# File 'lib/eddy/util/paths.rb', line 13 def self.data_dir return File.join(self.root_dir, "data") end |
.element_ids ⇒ Hash<String, String>
Returns a hash where the keys are Element ids and the values are unprocessed Element names.
38 39 40 41 42 43 44 45 46 |
# File 'lib/eddy/util/edi_data.rb', line 38 def self.element_ids() file = File.join(Eddy::Util.data_dir, "elements-short.tsv") data = {} CSV.foreach(file, { col_sep: "\t", quote_char: "\x00", headers: false }) do |row| next if row == ["id", "name"] data[row[0]] = row[1] end return data end |
.element_name_by_id(id) ⇒ Hash
Return the full name of an Element with the given id.
80 81 82 83 84 |
# File 'lib/eddy/util/edi_data.rb', line 80 def self.element_name_by_id(id) data = Eddy::Util.element_ids() return data[id] if data.key?(id) raise Eddy::Errors::Error, "No element found with id #{id}" end |
.list_built_elements ⇒ Array<String>
List names of Elements with Ruby files currently in Eddy.config.build_dir/elements
.
145 146 147 148 149 |
# File 'lib/eddy/util/edi_data.rb', line 145 def self.list_built_elements() dir = File.join(Eddy.config.build_dir, "elements", "**", "*.rb") files = Dir.glob(dir) return files.map { |f| File.basename(f).sub(/\..*/, "").upcase } end |
.list_element_classes ⇒ Array<String>
List the names of Elements for which Ruby classes have already been built.
105 106 107 108 109 |
# File 'lib/eddy/util/edi_data.rb', line 105 def self.list_element_classes() dir = File.join(Eddy::Util.root_dir, "lib", "definitions", "elements", "**", "*.rb") files = Dir.glob(dir) return files.map { |f| File.basename(f).sub(/\..*/, "").upcase } end |
.list_segment_classes ⇒ Array<String>
List the names of Segments for which Ruby classes have already been built.
117 118 119 120 121 |
# File 'lib/eddy/util/edi_data.rb', line 117 def self.list_segment_classes() dir = File.join(Eddy::Util.root_dir, "lib", "definitions", "segments", "**", "*.rb") files = Dir.glob(dir) return files.map { |f| File.basename(f).sub(/\..*/, "").upcase } end |
.list_segment_definitions ⇒ Array<String>
List Segment definition files in data/segments
.
133 134 135 136 137 |
# File 'lib/eddy/util/edi_data.rb', line 133 def self.list_segment_definitions() dir = File.join(Eddy::Util.data_dir, "segments") files = Dir.entries(dir).select { |f| File.file?(File.join(dir, f)) } return files.map { |f| File.join(dir, f) }.sort end |
.new_number(existing) ⇒ Integer
Given an array of numbers, returns the lowest number not included in the array.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/eddy/util/new_number.rb', line 8 def self.new_number(existing) n = nil i = 1 loop do if existing.include?(i) i += 1 next else n = i break end end return n end |
.normalize_id(id) ⇒ String
Given an Element Id (positive number under 1688, or I01-I64), returns a string sutable for use as a Ruby class name.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/eddy/util/normalize.rb', line 9 def self.normalize_id(id) name_regex = /\A(?<prefix>[iI]{1})?(?<numbers>\d+)\Z/ res = "" if matches = id.match(name_regex) if matches[:prefix] res << "I" else res << "E" end res << matches[:numbers] else raise Eddy::Errors::BuildError, "invalid element id" end end |
.normalize_name(name) ⇒ String
Convert a string to PascalCase, or UpperCamelCase Remove dashes, slashes, underscores, spaces, periods, and parens from a string then titleize it.
29 30 31 32 33 34 35 |
# File 'lib/eddy/util/normalize.rb', line 29 def self.normalize_name(name) return name.gsub(/\s*\(.*\)|[']/, "") .gsub(%r{[.,_\-/]}, " ") .split(" ") .map(&:capitalize) .join("") end |
.parse_tsv(filepath) ⇒ Array<Hash{Symbol => String}>
Read a TSV file and return its contents as an array of hashes.
12 13 14 15 16 17 18 19 20 |
# File 'lib/eddy/util/read.rb', line 12 def self.parse_tsv(filepath) return CSV.read( filepath, col_sep: "\t", headers: true, quote_char: "\x00", header_converters: :symbol, ).map(&:to_hash) end |
.raw_element_data ⇒ Array<Hash>
Return raw data from data/elements.tsv
.
17 18 19 |
# File 'lib/eddy/util/edi_data.rb', line 17 def self.raw_element_data() return Eddy::Util.parse_tsv(File.join(Eddy::Util.data_dir, "elements.tsv")) end |
.read_json_or_yaml(path, symbolize: true) ⇒ Hash{Symbol => Object}
Read data in from either a JSON or YAML file.
27 28 29 30 31 32 33 34 35 |
# File 'lib/eddy/util/read.rb', line 27 def self.read_json_or_yaml(path, symbolize: true) path = File.(path) data = case File.extname(path).downcase when /\.ya?ml/ then YAML.safe_load(File.read(path), symbolize_names: symbolize) when ".json" then JSON.parse(File.read(path), symbolize_names: symbolize) else raise Eddy::Errors::Error end return data end |
.root_dir ⇒ String
Directory where the gem is located.
7 8 9 |
# File 'lib/eddy/util/paths.rb', line 7 def self.root_dir return File.("../../../..", __FILE__) end |
.segment_ids ⇒ Hash<String, String>
Returns a hash where the keys are Segment ids and the values are unprocessed Segment names.
63 64 65 66 67 68 69 70 71 |
# File 'lib/eddy/util/edi_data.rb', line 63 def self.segment_ids() file = File.join(Eddy::Util.data_dir, "segments.tsv") data = {} CSV.foreach(file, { col_sep: "\t", quote_char: "\x00" }) do |row| next if row == ["id", "name"] data[row[0]] = row[1] end return data end |
.segment_name_by_id(id) ⇒ Hash
Return the full name of a Segment with the given id.
93 94 95 96 97 |
# File 'lib/eddy/util/edi_data.rb', line 93 def self.segment_name_by_id(id) data = Eddy::Util.segment_ids() return data[id] if data.key?(id) raise Eddy::Errors::Error, "No segment found with id #{id}" end |
.snake_case(name) ⇒ String
Convert a string to snake_case
41 42 43 44 45 46 47 |
# File 'lib/eddy/util/normalize.rb', line 41 def self.snake_case(name) return name.gsub(/\s*\(.*\)|['.]/, "") .gsub(%r{[,_\-/]}, " ") .split(" ") .map(&:downcase) .join("_") end |
.timestamp ⇒ String
7 8 9 |
# File 'lib/eddy/util/timestamp.rb', line 7 def self. return ::Time.now.strftime("%m-%d-%Y-%H-%M-%S") end |
.trim_delims_from_interchange(itch, element_separator: "*", segment_separator: "~") ⇒ String
11 12 13 14 15 |
# File 'lib/eddy/util/trim.rb', line 11 def self.trim_delims_from_interchange(itch, element_separator: "*", segment_separator: "~") e_sep = Regexp.escape(element_separator) s_sep = Regexp.escape(segment_separator) return itch.gsub(/#{e_sep}+(?=#{s_sep})/, "") end |
.trim_delims_from_segment(segment, separator: "*") ⇒ String
22 23 24 25 |
# File 'lib/eddy/util/trim.rb', line 22 def self.trim_delims_from_segment(segment, separator: "*") e_sep = Regexp.escape(separator) return segment.gsub(/#{e_sep}+(?=$)/, "") end |