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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'ext/data_matrix/data_matrix.c', line 77

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

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

  Data_Get_Struct(self, semacode_t, semacode);

  encode_string(semacode, StringValuePtr(message));

  return self;
}

Class Method Details

.encode_string(value) ⇒ Object



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

def encode_string(value)
  new(value)
end

Instance Method Details

#dataObject Also known as: to_a



109
110
111
112
113
114
115
116
117
# File 'ext/data_matrix/data_matrix.c', line 109

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



180
181
182
183
184
185
# File 'ext/data_matrix/data_matrix.c', line 180

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



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

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);
}

#encodingObject



119
120
121
122
123
124
# File 'ext/data_matrix/data_matrix.c', line 119

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

  return rb_str_new2(semacode->encoding);
}

#heightObject



159
160
161
162
163
164
# File 'ext/data_matrix/data_matrix.c', line 159

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



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

def length
  height * width
end

#raw_encoded_lengthObject



166
167
168
169
170
171
# File 'ext/data_matrix/data_matrix.c', line 166

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



173
174
175
176
177
178
# File 'ext/data_matrix/data_matrix.c', line 173

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



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

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



152
153
154
155
156
157
# File 'ext/data_matrix/data_matrix.c', line 152

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

  return INT2FIX(semacode->width);
}