radt 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +15 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/radt.rb +9 -0
- data/lib/radt/column.rb +92 -0
- data/lib/radt/globals.rb +14 -0
- data/lib/radt/record.rb +67 -0
- data/lib/radt/table.rb +260 -0
- data/lib/radt/version.rb +3 -0
- data/radt.gemspec +30 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6e23c8a81e4bae41593a219aecea70118088d551
|
4
|
+
data.tar.gz: 36d0503e43e33e1b370b7336fd2b920a64437bc5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42ce4216a4197928a249e1f3f6397e63b4b1cf3ee6cdf3feb5fef54090e2c62e42afd7916cd807ba8ba6a19b44b2e587df9724174a91713c31731d9cfbe4b745
|
7
|
+
data.tar.gz: 8ad65f9675b6fa849bd13141c254590ec47a7dd200bf25c3dbbe5d11bb20ac382c6aeb27535785f709426944a917763af0ca93b1b428e1504c9e549fcbb1e1be
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at nick@ndfine.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Nicholas Fine
|
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,15 @@
|
|
1
|
+
# Radt
|
2
|
+
|
3
|
+
Updated version of Ruby-ADT gem to read Advantage Database files, originally found at [https://github.com/chasemgray/Ruby-ADT](https://github.com/chasemgray/Ruby-ADT)
|
4
|
+
|
5
|
+
## Contributing
|
6
|
+
|
7
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yrgoldteeth/radt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
8
|
+
|
9
|
+
## License
|
10
|
+
|
11
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
12
|
+
|
13
|
+
## Code of Conduct
|
14
|
+
|
15
|
+
Everyone interacting in the Radt project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/yrgoldteeth/radt/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "radt"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/radt.rb
ADDED
data/lib/radt/column.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Original Code from https://github.com/chasemgray/Ruby-ADT/blob/master/lib/adt/column.rb
|
2
|
+
module RADT
|
3
|
+
class ColumnLengthError < RADTError; end
|
4
|
+
class ColumnNameError < RADTError; end
|
5
|
+
|
6
|
+
TYPES = {4 => 'character', 10 => 'double', 11 => 'integer', 12 => 'short', 20 => 'cicharacter', 3 => 'date', 13 => 'time', 14 => 'timestamp', 15 => 'autoinc'}
|
7
|
+
FLAGS = {'character' => 'A', 'double' => 'D', 'integer' => 'i', 'short' => 'S', 'cicharacter' => 'A', 'date' => '?', 'time' => '?', 'timestamp' => '?', 'autoinc' => 'I'}
|
8
|
+
|
9
|
+
|
10
|
+
class Column
|
11
|
+
attr_reader :name, :type, :length
|
12
|
+
|
13
|
+
# Initialize a new RADT::Column
|
14
|
+
#
|
15
|
+
# @param [String] name
|
16
|
+
# @param [String] type
|
17
|
+
# @param [Fixnum] length
|
18
|
+
def initialize(name, type, length)
|
19
|
+
@name, @type, @length = strip_non_ascii_chars(name), type, length
|
20
|
+
|
21
|
+
raise ColumnLengthError, "field length must be greater than 0" unless length > 0
|
22
|
+
raise ColumnNameError, "column name cannot be empty" if @name.length == 0
|
23
|
+
end
|
24
|
+
|
25
|
+
def data_type(id)
|
26
|
+
TYPES[id]
|
27
|
+
end
|
28
|
+
|
29
|
+
def flag(type, length = 0)
|
30
|
+
data_type = data_type(type)
|
31
|
+
flag = FLAGS[data_type]
|
32
|
+
if flag.eql? 'A'
|
33
|
+
return flag + length.to_s
|
34
|
+
end
|
35
|
+
return flag
|
36
|
+
end
|
37
|
+
|
38
|
+
# Decode a DateTime value
|
39
|
+
#
|
40
|
+
# @param [String] value
|
41
|
+
# @return [DateTime]
|
42
|
+
def decode_datetime(value)
|
43
|
+
days, milliseconds = value.unpack('l2')
|
44
|
+
seconds = milliseconds / 1000
|
45
|
+
DateTime.jd(days, seconds/3600, seconds/60 % 60, seconds % 60) rescue nil
|
46
|
+
end
|
47
|
+
|
48
|
+
# Schema definition
|
49
|
+
#
|
50
|
+
# @return [String]
|
51
|
+
def schema_definition
|
52
|
+
"\"#{name.underscore}\", #{schema_data_type}\n"
|
53
|
+
end
|
54
|
+
|
55
|
+
# Column type for schema definition
|
56
|
+
#
|
57
|
+
# @return [String]
|
58
|
+
def schema_data_type
|
59
|
+
case data_type(type)
|
60
|
+
when "character"
|
61
|
+
":string, :limit => #{length}"
|
62
|
+
when "cicharacter"
|
63
|
+
":string, :limit => #{length}"
|
64
|
+
when "double"
|
65
|
+
":float"
|
66
|
+
when "date"
|
67
|
+
":date"
|
68
|
+
when "time"
|
69
|
+
":timestamp"
|
70
|
+
when "timestamp"
|
71
|
+
":timestamp"
|
72
|
+
when "integer"
|
73
|
+
":integer"
|
74
|
+
when "autoinc"
|
75
|
+
":integer"
|
76
|
+
else
|
77
|
+
":string, :limit => #{length}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Strip all non-ascii and non-printable characters
|
82
|
+
#
|
83
|
+
# @param [String] s
|
84
|
+
# @return [String]
|
85
|
+
def strip_non_ascii_chars(s)
|
86
|
+
# truncate the string at the first null character
|
87
|
+
s = s[0, s.index("\x00")] if s.index("\x00")
|
88
|
+
|
89
|
+
s.gsub(/[^\x20-\x7E]/,"")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/lib/radt/globals.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# Original code from https://github.com/chasemgray/Ruby-ADT/blob/master/lib/adt/globals.rb
|
2
|
+
module RADT
|
3
|
+
class RADTError < StandardError; end
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
MS_PER_SECOND = 1000
|
8
|
+
MS_PER_MINUTE = MS_PER_SECOND * 60
|
9
|
+
MS_PER_HOUR = MS_PER_MINUTE * 60
|
10
|
+
|
11
|
+
HEADER_LENGTH = 400
|
12
|
+
|
13
|
+
COLUMN_LENGTH = 200
|
14
|
+
end
|
data/lib/radt/record.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Original code from https://github.com/chasemgray/Ruby-ADT/blob/master/lib/adt/record.rb
|
2
|
+
module RADT
|
3
|
+
# An instance of RADT::Record represents a row in the RADT file
|
4
|
+
class Record
|
5
|
+
extend Forwardable
|
6
|
+
attr_reader :attributes
|
7
|
+
|
8
|
+
def_delegators :@table, :columns
|
9
|
+
|
10
|
+
# Initialize a new ADT::Record
|
11
|
+
#
|
12
|
+
# @param [RADT::Table] table
|
13
|
+
def initialize(table)
|
14
|
+
@table, @data = table, table.data
|
15
|
+
|
16
|
+
initialize_values
|
17
|
+
define_accessors
|
18
|
+
end
|
19
|
+
|
20
|
+
# Equality
|
21
|
+
#
|
22
|
+
# @param [RADT::Record] other
|
23
|
+
# @return [Boolean]
|
24
|
+
def ==(other)
|
25
|
+
other.respond_to?(:attributes) && other.attributes == attributes
|
26
|
+
end
|
27
|
+
|
28
|
+
# Maps a row to an array of values
|
29
|
+
#
|
30
|
+
# @return [Array]
|
31
|
+
def to_a
|
32
|
+
columns.map { |column| @attributes[column.name.underscore] }
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
# Defined attribute accessor methods
|
38
|
+
def define_accessors
|
39
|
+
columns.each do |column|
|
40
|
+
underscored_column_name = column.name.underscore
|
41
|
+
unless respond_to?(underscored_column_name)
|
42
|
+
self.class.send :define_method, underscored_column_name do
|
43
|
+
@attributes[column.name.underscore]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Initialize values for a row
|
50
|
+
def initialize_values
|
51
|
+
#skip the first 5 bytes, don't know what they are for and they don't contain the data.
|
52
|
+
@data.read(5)
|
53
|
+
|
54
|
+
@attributes = columns.inject({}) do |hash, column|
|
55
|
+
|
56
|
+
#get the unpack flag to get this data.
|
57
|
+
value = @data.read(column.length).unpack("#{column.flag(column.type, column.length)}").first
|
58
|
+
hash[column.name] = value
|
59
|
+
hash[column.name.underscore] = value
|
60
|
+
|
61
|
+
hash
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
data/lib/radt/table.rb
ADDED
@@ -0,0 +1,260 @@
|
|
1
|
+
# Original code from https://github.com/chasemgray/Ruby-ADT/blob/master/lib/adt/table.rb
|
2
|
+
module RADT
|
3
|
+
|
4
|
+
# RADT::Table is the primary interface to a single RADT file and provides
|
5
|
+
# methods for enumerating and searching the records.
|
6
|
+
class Table
|
7
|
+
attr_reader :column_count # The total number of columns
|
8
|
+
attr_reader :columns # An array of DBF::Column
|
9
|
+
attr_reader :options # The options hash used to initialize the table
|
10
|
+
attr_reader :data # RADT file handle
|
11
|
+
attr_reader :record_count # Total number of records
|
12
|
+
|
13
|
+
# Opens a RADT:Table
|
14
|
+
# Example:
|
15
|
+
# table = RADT::Table.new 'data.adt'
|
16
|
+
#
|
17
|
+
# @param [String] path Path to the adt file
|
18
|
+
def initialize(path)
|
19
|
+
@data = File.open(path, 'rb')
|
20
|
+
reload!
|
21
|
+
end
|
22
|
+
|
23
|
+
# Closes the table
|
24
|
+
def close
|
25
|
+
@data.close
|
26
|
+
end
|
27
|
+
|
28
|
+
# Reloads the database
|
29
|
+
def reload!
|
30
|
+
@records = nil
|
31
|
+
get_header_info
|
32
|
+
get_column_descriptors
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
# Retrieve a Column by name
|
37
|
+
#
|
38
|
+
# @param [String, Symbol] column_name
|
39
|
+
# @return [RADT::Column]
|
40
|
+
def column(column_name)
|
41
|
+
@columns.detect {|f| f.name == column_name.to_s}
|
42
|
+
end
|
43
|
+
|
44
|
+
# Calls block once for each record in the table. The record may be nil
|
45
|
+
# if the record has been marked as deleted.
|
46
|
+
#
|
47
|
+
# @yield [nil, RADT::Record]
|
48
|
+
def each
|
49
|
+
0.upto(@record_count - 1) do |n|
|
50
|
+
seek_to_record(n)
|
51
|
+
yield RADT::Record.new(self)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Retrieve a record by index number
|
56
|
+
#
|
57
|
+
# @param [Fixnum] index
|
58
|
+
# @return [RADT::Record]
|
59
|
+
def record(index)
|
60
|
+
seek_to_record(index)
|
61
|
+
RADT::Record.new(self)
|
62
|
+
end
|
63
|
+
|
64
|
+
alias_method :row, :record
|
65
|
+
|
66
|
+
|
67
|
+
# Generate an ActiveRecord::Schema
|
68
|
+
#
|
69
|
+
# xBase data types are converted to generic types as follows:
|
70
|
+
# - Number columns with no decimals are converted to :integer
|
71
|
+
# - Number columns with decimals are converted to :float
|
72
|
+
# - Date columns are converted to :datetime
|
73
|
+
# - Logical columns are converted to :boolean
|
74
|
+
# - Memo columns are converted to :text
|
75
|
+
# - Character columns are converted to :string and the :limit option is set
|
76
|
+
# to the length of the character column
|
77
|
+
#
|
78
|
+
# Example:
|
79
|
+
# create_table "mydata" do |t|
|
80
|
+
# t.column :name, :string, :limit => 30
|
81
|
+
# t.column :last_update, :datetime
|
82
|
+
# t.column :is_active, :boolean
|
83
|
+
# t.column :age, :integer
|
84
|
+
# t.column :notes, :text
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# @param [optional String] path
|
88
|
+
# @return [String]
|
89
|
+
def schema(path = nil)
|
90
|
+
s = "ActiveRecord::Schema.define do\n"
|
91
|
+
s << " create_table \"#{File.basename(@data.path, ".*")}\" do |t|\n"
|
92
|
+
columns.each do |column|
|
93
|
+
s << " t.column #{column.schema_definition}"
|
94
|
+
end
|
95
|
+
s << " end\nend"
|
96
|
+
|
97
|
+
if path
|
98
|
+
File.open(path, 'w') {|f| f.puts(s)}
|
99
|
+
end
|
100
|
+
|
101
|
+
s
|
102
|
+
end
|
103
|
+
|
104
|
+
def to_a
|
105
|
+
records = []
|
106
|
+
each {|record| records << record if record}
|
107
|
+
records
|
108
|
+
end
|
109
|
+
|
110
|
+
# Dumps all records to a CSV file. If no filename is given then CSV is
|
111
|
+
# output to STDOUT.
|
112
|
+
#
|
113
|
+
# @param [optional String] path Defaults to basename of adt file
|
114
|
+
def to_csv(path = nil)
|
115
|
+
path = File.basename(@data.path, '.adt') + '.csv' if path.nil?
|
116
|
+
CSV.open(path, 'w', :force_quotes => true) do |csv|
|
117
|
+
each do |record|
|
118
|
+
csv << record.to_a
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# Find records using a simple ActiveRecord-like syntax.
|
124
|
+
#
|
125
|
+
# Examples:
|
126
|
+
# table = RADT::Table.new 'mydata.adt'
|
127
|
+
#
|
128
|
+
# # Find record number 5
|
129
|
+
# table.find(5)
|
130
|
+
#
|
131
|
+
# # Find all records for Chase Gray
|
132
|
+
# table.find :all, :first_name => "Chase", :last_name => "Gray"
|
133
|
+
#
|
134
|
+
# # Find first record
|
135
|
+
# table.find :first, :first_name => "Chase"
|
136
|
+
#
|
137
|
+
# The <b>command</b> may be a record index, :all, or :first.
|
138
|
+
# <b>options</b> is optional and, if specified, should be a hash where the keys correspond
|
139
|
+
# to column names in the database. The values will be matched exactly with the value
|
140
|
+
# in the database. If you specify more than one key, all values must match in order
|
141
|
+
# for the record to be returned. The equivalent SQL would be "WHERE key1 = 'value1'
|
142
|
+
# AND key2 = 'value2'".
|
143
|
+
#
|
144
|
+
# @param [Fixnum, Symbol] command
|
145
|
+
# @param [optional, Hash] options Hash of search parameters
|
146
|
+
# @yield [optional, RADT::Record]
|
147
|
+
def find(command, options = {}, &block)
|
148
|
+
case command
|
149
|
+
when Fixnum
|
150
|
+
record(command)
|
151
|
+
when Array
|
152
|
+
command.map {|i| record(i)}
|
153
|
+
when :all
|
154
|
+
find_all(options, &block)
|
155
|
+
when :first
|
156
|
+
find_first(options)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
private
|
161
|
+
|
162
|
+
# Find all matching
|
163
|
+
#
|
164
|
+
# @param [Hash] options
|
165
|
+
# @yield [optional RADT::Record]
|
166
|
+
# @return [Array]
|
167
|
+
def find_all(options, &block)
|
168
|
+
results = []
|
169
|
+
each do |record|
|
170
|
+
if all_values_match?(record, options)
|
171
|
+
if block_given?
|
172
|
+
yield(record)
|
173
|
+
else
|
174
|
+
results << record
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
results
|
179
|
+
end
|
180
|
+
|
181
|
+
# Find first matching
|
182
|
+
#
|
183
|
+
# @param [Hash] options
|
184
|
+
# @return [RADT::Record, nil]
|
185
|
+
def find_first(options)
|
186
|
+
each do |record|
|
187
|
+
return record if all_values_match?(record, options)
|
188
|
+
end
|
189
|
+
nil
|
190
|
+
end
|
191
|
+
|
192
|
+
# Do all search parameters match?
|
193
|
+
#
|
194
|
+
# @param [RADT::Record] record
|
195
|
+
# @param [Hash] options
|
196
|
+
# @return [Boolean]
|
197
|
+
def all_values_match?(record, options)
|
198
|
+
options.all? {|key, value| record.attributes[key.to_s.underscore] == value}
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
# Replace the file extension
|
203
|
+
#
|
204
|
+
# @param [String] path
|
205
|
+
# @param [String] extension
|
206
|
+
# @return [String]
|
207
|
+
def replace_extname(path, extension)
|
208
|
+
path.sub(/#{File.extname(path)[1..-1]}$/, extension)
|
209
|
+
end
|
210
|
+
|
211
|
+
|
212
|
+
# Determine record count, record_count, and record length
|
213
|
+
def get_header_info
|
214
|
+
@data.rewind
|
215
|
+
|
216
|
+
#column_count_offset = 33, record_count_offset = 24, record_length_offset = 36
|
217
|
+
@record_count, @data_offset, @record_length = data.read(HEADER_LENGTH).unpack("@24 I x4 I I")
|
218
|
+
@column_count = (@data_offset-400)/200
|
219
|
+
end
|
220
|
+
|
221
|
+
|
222
|
+
# Retrieves column information from the database
|
223
|
+
def get_column_descriptors
|
224
|
+
#skip past header to get to column information
|
225
|
+
@data.seek(HEADER_LENGTH)
|
226
|
+
|
227
|
+
# column names are the first 128 bytes and column info takes up the last 72 bytes.
|
228
|
+
# byte 130 contains a 16-bit column type
|
229
|
+
# byte 136 contains a 16-bit length field
|
230
|
+
@columns = []
|
231
|
+
@column_count.times do
|
232
|
+
name, type, length = @data.read(200).unpack('A128 x S x4 S')
|
233
|
+
if length > 0
|
234
|
+
@columns << Column.new(name.strip, type, length)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
# Reset the column count in case any were skipped
|
238
|
+
@column_count = @columns.size
|
239
|
+
|
240
|
+
@columns
|
241
|
+
end
|
242
|
+
|
243
|
+
|
244
|
+
# Seek to a byte offset in the record data
|
245
|
+
#
|
246
|
+
# @params [Fixnum] offset
|
247
|
+
def seek(offset)
|
248
|
+
@data.seek(@data_offset + offset)
|
249
|
+
end
|
250
|
+
|
251
|
+
# Seek to a record
|
252
|
+
#
|
253
|
+
# @param [Fixnum] index
|
254
|
+
def seek_to_record(index)
|
255
|
+
seek(index * @record_length)
|
256
|
+
end
|
257
|
+
|
258
|
+
end
|
259
|
+
|
260
|
+
end
|
data/lib/radt/version.rb
ADDED
data/radt.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "radt/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "radt"
|
8
|
+
spec.version = RADT::VERSION
|
9
|
+
spec.authors = ["Nicholas Fine"]
|
10
|
+
spec.email = ["nick@ndfine.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Updated version of Ruby-ADT gem to read Advantage Database files}
|
13
|
+
spec.description = %q{Updated version of Ruby-ADT gem to read Advantage Database files, originally found at https://github.com/chasemgray/Ruby-ADT}
|
14
|
+
spec.homepage = "https://github.com/yrgoldteeth/rabt"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "activesupport"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "pry"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: radt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicholas Fine
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.15'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.15'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Updated version of Ruby-ADT gem to read Advantage Database files, originally
|
84
|
+
found at https://github.com/chasemgray/Ruby-ADT
|
85
|
+
email:
|
86
|
+
- nick@ndfine.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CODE_OF_CONDUCT.md
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- lib/radt.rb
|
102
|
+
- lib/radt/column.rb
|
103
|
+
- lib/radt/globals.rb
|
104
|
+
- lib/radt/record.rb
|
105
|
+
- lib/radt/table.rb
|
106
|
+
- lib/radt/version.rb
|
107
|
+
- radt.gemspec
|
108
|
+
homepage: https://github.com/yrgoldteeth/rabt
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 2.6.11
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: Updated version of Ruby-ADT gem to read Advantage Database files
|
132
|
+
test_files: []
|