byteobject 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/lib/ByteObject.rb +94 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3ab37ded572abaff2cb86708ec1eb8112a3cdd9c00f9897d585b79c2d84bc952
|
4
|
+
data.tar.gz: 6de79aed9da21d1d68dbadd355549a6ac37845bf01f5041b8e25accfd02d30e6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 35d5f629e496af281ca02147162cfd166e1e10fbd6c21b7f2784d9e8403174b247943e5e4996fb53d7058d2726ff84a77d0f763fba2a1bee350b885418eb18f5
|
7
|
+
data.tar.gz: 112d18d7f157b09ac4f2013065079b87b15c4a8bcbcdff913c1deeaf238c4fa091b7a0fde00a9f612dbe1233e7c6d83d565498adf7f23dc0c16dfce0fbfdffac
|
data/lib/ByteObject.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
##
|
2
|
+
# The ByteObject module adds several attribute methods to any class that includes it, to aid in writing programs that
|
3
|
+
# require manipulating bytes of specific size. It also has helper methods to make working with binary files easier.
|
4
|
+
module ByteObject
|
5
|
+
|
6
|
+
# @!visibility private
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ByteAttributes
|
9
|
+
end
|
10
|
+
|
11
|
+
##
|
12
|
+
# Whenever ByteObject is included into a class, it also extends that class with the ByteAttributes module. This is
|
13
|
+
# the module that bestows the byte-specific attribute methods.
|
14
|
+
module ByteAttributes
|
15
|
+
|
16
|
+
##
|
17
|
+
# @!method attr_u8bit(*keys)
|
18
|
+
# Creates an 8-bit unsigned attribute reader and writer.
|
19
|
+
|
20
|
+
# @!method attr_s8bit(*keys)
|
21
|
+
# Creates an 8-bit signed attribute reader and writer.
|
22
|
+
|
23
|
+
# @!method attr_u16bit(*keys)
|
24
|
+
# Creates a 16-bit unsigned attribute reader and writer.
|
25
|
+
|
26
|
+
# @!method attr_s16bit(*keys)
|
27
|
+
# Creates an 16-bit signed attribute reader and writer.
|
28
|
+
|
29
|
+
# @!method attr_u32bit(*keys)
|
30
|
+
# Creates an 32-bit unsigned attribute reader and writer.
|
31
|
+
|
32
|
+
# @!method attr_s32bit(*keys)
|
33
|
+
# Creates an 32-bit signed attribute reader and writer.
|
34
|
+
|
35
|
+
# @!method attr_u64bit(*keys)
|
36
|
+
# Creates an 64-bit unsigned attribute reader and writer.
|
37
|
+
|
38
|
+
# @!method attr_s64bit(*keys)
|
39
|
+
# Creates an 64-bit signed attribute reader and writer.
|
40
|
+
|
41
|
+
##
|
42
|
+
# This method creates an ordinary attribute reader and a specialized attribute writer for the given key. The writer
|
43
|
+
# will clamp any values passed to the new attribute based on its size and whether or not it is signed.
|
44
|
+
# Under normal circumstances, you will not need to call this method yourself. It is used by this module to generate
|
45
|
+
# the attribute methods given to any extended classes.
|
46
|
+
# @param key [Symbol] The name of the attribute.
|
47
|
+
# @param size [Integer] The size, in *bits*, of the attribute.
|
48
|
+
# @param signed [Boolean] Whether or not the attribute can be negative.
|
49
|
+
# @return [void]
|
50
|
+
def attr_byte(key, size, signed)
|
51
|
+
attr_reader(key)
|
52
|
+
|
53
|
+
bytesize = (2**size)
|
54
|
+
min = signed ? -bytesize/2 : 0
|
55
|
+
max = signed ? (bytesize/2) - 1 : bytesize - 1
|
56
|
+
|
57
|
+
define_method("#{key}=") do |val|
|
58
|
+
instance_variable_set("@#{key}", val.clamp(min, max))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# @!visibility private
|
63
|
+
[8, 16, 32, 64].each do |size|
|
64
|
+
define_method("attr_u#{size}bit") do |*keys|
|
65
|
+
keys.each{|key| attr_byte(key, size, false)}
|
66
|
+
end
|
67
|
+
|
68
|
+
define_method("attr_s#{size}bit") do |*keys|
|
69
|
+
keys.each{|key| attr_byte(key, size, true)}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# Takes a hash (or hash-like object whose #each method yields a key-value pair) and assigns the values of its keys to
|
77
|
+
# instance variables with the same names. If there is an attribute writer method, it will attempt to use that;
|
78
|
+
# otherwise it will set the instance variable directly (much to the chagrin of whoever wrote the official Ruby
|
79
|
+
# documentation).
|
80
|
+
#
|
81
|
+
# As the bang might suggest, this method is dangerous and modifies the calling object.
|
82
|
+
# @param hash
|
83
|
+
def from_hash!(hash)
|
84
|
+
hash.each do |key, value|
|
85
|
+
if methods.include?("#{key}=".to_sym)
|
86
|
+
send("#{key}=", value)
|
87
|
+
else
|
88
|
+
instance_variable_set("@#{key}", value)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: byteobject
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael K Gremillion
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
ByteObject is a module designed to make working with exact-length byte
|
15
|
+
values in Ruby programs easier and quicker. It mostly provides attribute
|
16
|
+
methods to clamp values within the range of a given byte length.
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/ByteObject.rb
|
23
|
+
homepage: https://github.com/mkgremillion/ByteObject
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata:
|
27
|
+
source_code_uri: https://github.com/mkgremillion/ByteObject
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.7.6
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: A module to assist with specific byte-length attributes.
|
48
|
+
test_files: []
|