Class: DataMatrix::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/data_matrix/encoder.rb,
ext/data_matrix/data_matrix.c

Overview

encoder class ruby extensions

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Encoder

Returns a new instance of Encoder.

Raises:

  • (ArgumentError)


9
10
11
12
# File 'lib/data_matrix/encoder.rb', line 9

def initialize(value)
  raise ArgumentError, 'Value must be convertible to string' unless value.respond_to?(:to_s)
  encode_string(value.to_s)
end

Instance Method Details

#dataObject Also known as: to_a



112
113
114
115
116
117
118
119
120
# File 'ext/data_matrix/data_matrix.c', line 112

static VALUE data_matrix_data(VALUE self) {
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  if(semacode->data == NULL)
    return Qnil;
  else
    return data_matrix_grid(semacode);
}

#ecc_bytesObject



183
184
185
186
187
188
# File 'ext/data_matrix/data_matrix.c', line 183

static VALUE data_matrix_ecc_bytes(VALUE self) {
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return INT2FIX(semacode->ecc_bytes);
}

#encode(encoding) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/data_matrix/data_matrix.c', line 93

static VALUE data_matrix_encode(VALUE self, VALUE encoding) {
  semacode_t *semacode;

  if (!rb_respond_to(encoding, rb_intern ("to_s")))
    rb_raise(rb_eRuntimeError, "target must respond to 'to_s'");

  Data_Get_Struct(self, semacode_t, semacode);

  /* free previous string if that exists */
  if(semacode->data != NULL) {
    free(semacode->data);
    semacode->data = NULL;
  }

  /* do a new encoding */
  DATA_PTR(self) = encode_string(semacode, StringValuePtr(encoding));
  return data_matrix_grid(semacode);
}

#encode_string(message) ⇒ Object



84
85
86
87
88
89
90
91
# File 'ext/data_matrix/data_matrix.c', line 84

static VALUE data_matrix_init(VALUE self, VALUE message) {
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  encode_string(semacode, StringValuePtr(message));

  return self;
}

#encodingObject



122
123
124
125
126
127
# File 'ext/data_matrix/data_matrix.c', line 122

static VALUE data_matrix_encoded(VALUE self) {
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return rb_str_new2(semacode->encoding);
}

#heightObject



162
163
164
165
166
167
# File 'ext/data_matrix/data_matrix.c', line 162

static VALUE data_matrix_height(VALUE self) {
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return INT2FIX(semacode->height);
}

#lengthObject Also known as: size



14
15
16
# File 'lib/data_matrix/encoder.rb', line 14

def length
  height * width
end

#raw_encoded_lengthObject



169
170
171
172
173
174
# File 'ext/data_matrix/data_matrix.c', line 169

static VALUE data_matrix_raw_encoded_length(VALUE self) {
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return INT2FIX(semacode->raw_encoded_length);
}

#symbol_sizeObject



176
177
178
179
180
181
# File 'ext/data_matrix/data_matrix.c', line 176

static VALUE data_matrix_symbol_size(VALUE self) {
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return INT2FIX(semacode->symbol_capacity);
}

#to_sObject Also known as: to_str



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'ext/data_matrix/data_matrix.c', line 129

static VALUE data_matrix_to_s(VALUE self) {
  semacode_t *semacode;
  VALUE str;
  int x, y, w, h;

  Data_Get_Struct(self, semacode_t, semacode);

  if(semacode == NULL || semacode->data == NULL) return Qnil;

  w = semacode->width;
  h = semacode->height;

  str = rb_str_new2("");

  for (y = h - 1; y >= 0; y--) {
    for (x = 0; x < w; x++) {
      if(semacode->data[y * w + x])
        rb_str_cat(str, "1", 1);
      else
        rb_str_cat(str, "0", 1);
    }
    rb_str_cat(str, ",", 1);
  }
  return str;
}

#widthObject



155
156
157
158
159
160
# File 'ext/data_matrix/data_matrix.c', line 155

static VALUE data_matrix_width(VALUE self) {
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return INT2FIX(semacode->width);
}