faml 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 23f087ba97e9ffb104d240af74b6704fd100a5e8
4
- data.tar.gz: 2e5f8a48de615415b4343fa7f818106b26f8d3ae
3
+ metadata.gz: b0b565977825d5840091b528c95126d1145470ff
4
+ data.tar.gz: 6d7bbd18e0915cdf9a9feb241c40009b85eacddb
5
5
  SHA512:
6
- metadata.gz: 84c1f440f966f32e7163fe610317d5fa4da8d625204aa72b8e09488fd872c2b3d62ff26c2ff83ea3e743bc48d351257be80f02dbffcc44d219266417f6f6aa29
7
- data.tar.gz: 4ff1f89b8bf2935abe157da73197c12614a32a7fadfb644b596d996868ebc4d99101243ae83b04833488734393522645125aabfb2b50409bd4d57fd35fa4644d
6
+ metadata.gz: ea2a871d173b410deccaa0d693f3a6c6aff6479a78e74c1424147a33ff64277eaf4652e921567bf75bc56f58b61148fe14d1f2149bfe62367f1eb9f023bf428a
7
+ data.tar.gz: fb9112e98d3df4146c058f2075498de3645ef480e1ebb5af2655775e3459d4f79b8291b2a2b82e12e758922b077063f14d63758cdd58bfc605a01242e819a8c4
@@ -1,3 +1,7 @@
1
+ ## 0.3.1 (2015-09-20)
2
+ - Improve `Faml::AttributeBuilder.build` performance
3
+ - Reduce String allocations
4
+
1
5
  ## 0.3.0 (2015-09-13)
2
6
  - Move Haml parser and AST definition to haml_parser
3
7
  - https://github.com/eagletmt/haml_parser
@@ -10,6 +10,7 @@
10
10
 
11
11
  VALUE rb_mAttributeBuilder;
12
12
  static ID id_keys, id_sort_bang, id_uniq_bang, id_merge_bang, id_temple, id_utils, id_escape_html, id_gsub, id_to_s;
13
+ static ID id_id, id_class, id_underscore, id_hyphen, id_space, id_equal;
13
14
 
14
15
  static void
15
16
  concat_array_attribute(VALUE attributes, VALUE hash, VALUE key)
@@ -54,7 +55,7 @@ normalize_data_i2(VALUE key, VALUE value, VALUE ptr)
54
55
  struct normalize_data_i2_arg *arg = (struct normalize_data_i2_arg *)ptr;
55
56
  VALUE k = rb_funcall(arg->key, id_to_s, 0);
56
57
 
57
- k = rb_funcall(k, id_gsub, 2, rb_str_new_cstr("_"), rb_str_new_cstr("-"));
58
+ k = rb_funcall(k, id_gsub, 2, rb_const_get(rb_mAttributeBuilder, id_underscore), rb_const_get(rb_mAttributeBuilder, id_hyphen));
58
59
  rb_str_cat(k, "-", 1);
59
60
  rb_str_append(k, key);
60
61
  rb_hash_aset(arg->normalized, k, value);
@@ -73,7 +74,7 @@ normalize_data_i(VALUE key, VALUE value, VALUE normalized)
73
74
  rb_hash_foreach(normalize_data(value), normalize_data_i2, (VALUE)(&arg));
74
75
  } else {
75
76
  key = rb_funcall(key, id_to_s, 0);
76
- key = rb_funcall(key, id_gsub, 2, rb_str_new_cstr("_"), rb_str_new_cstr("-"));
77
+ key = rb_funcall(key, id_gsub, 2, rb_const_get(rb_mAttributeBuilder, id_underscore), rb_const_get(rb_mAttributeBuilder, id_hyphen));
77
78
  rb_hash_aset(normalized, key, value);
78
79
  }
79
80
  return ST_CONTINUE;
@@ -132,36 +133,32 @@ merge(VALUE attributes, int argc, VALUE *argv)
132
133
 
133
134
  Check_Type(argv[i], T_HASH);
134
135
  h = stringify_keys(argv[i]);
135
- concat_array_attribute(attributes, h, rb_str_new_cstr("class"));
136
- concat_array_attribute(attributes, h, rb_str_new_cstr("id"));
136
+ concat_array_attribute(attributes, h, rb_const_get(rb_mAttributeBuilder, id_class));
137
+ concat_array_attribute(attributes, h, rb_const_get(rb_mAttributeBuilder, id_id));
137
138
  normalize(h);
138
139
  rb_funcall(attributes, id_merge_bang, 1, h);
139
140
  }
140
141
  }
141
142
 
142
- static VALUE
143
- put_attribute(VALUE attr_quote, VALUE key, VALUE value)
143
+ static void
144
+ put_attribute(VALUE buf, VALUE attr_quote, VALUE key, VALUE value)
144
145
  {
145
- VALUE utils_class, str;
146
- long len;
146
+ VALUE utils_class;
147
147
 
148
148
  value = rb_funcall(value, id_to_s, 0);
149
149
  utils_class = rb_const_get(rb_const_get(rb_cObject, id_temple), id_utils);
150
150
  value = rb_funcall(utils_class, id_escape_html, 1, value);
151
151
 
152
- len = 2 + 2*RSTRING_LEN(attr_quote) + RSTRING_LEN(key) + RSTRING_LEN(value);
153
- str = rb_str_buf_new(len);
154
- rb_str_buf_cat(str, " ", 1);
155
- rb_str_buf_append(str, key);
156
- rb_str_buf_cat(str, "=", 1);
157
- rb_str_buf_append(str, attr_quote);
158
- rb_str_buf_append(str, value);
159
- rb_str_buf_append(str, attr_quote);
160
- return str;
152
+ rb_ary_push(buf, rb_const_get(rb_mAttributeBuilder, id_space));
153
+ rb_ary_push(buf, key);
154
+ rb_ary_push(buf, rb_const_get(rb_mAttributeBuilder, id_equal));
155
+ rb_ary_push(buf, attr_quote);
156
+ rb_ary_push(buf, value);
157
+ rb_ary_push(buf, attr_quote);
161
158
  }
162
159
 
163
- static VALUE
164
- build_attribute(VALUE attr_quote, int is_html, VALUE key, VALUE value)
160
+ static void
161
+ build_attribute(VALUE buf, VALUE attr_quote, int is_html, VALUE key, VALUE value)
165
162
  {
166
163
  const char *key_cstr = StringValueCStr(key);
167
164
  if (strcmp(key_cstr, "class") == 0) {
@@ -169,9 +166,7 @@ build_attribute(VALUE attr_quote, int is_html, VALUE key, VALUE value)
169
166
 
170
167
  Check_Type(value, T_ARRAY);
171
168
  len = RARRAY_LEN(value);
172
- if (len == 0) {
173
- return rb_str_new_cstr("");
174
- } else {
169
+ if (len != 0) {
175
170
  long i;
176
171
  VALUE ary = rb_ary_new_capa(len);
177
172
  for (i = 0; i < len; i++) {
@@ -180,39 +175,34 @@ build_attribute(VALUE attr_quote, int is_html, VALUE key, VALUE value)
180
175
  }
181
176
  rb_funcall(ary, id_sort_bang, 0);
182
177
  rb_funcall(ary, id_uniq_bang, 0);
183
- return put_attribute(attr_quote, key, rb_ary_join(ary, rb_str_new_cstr(" ")));
178
+ put_attribute(buf, attr_quote, key, rb_ary_join(ary, rb_const_get(rb_mAttributeBuilder, id_space)));
184
179
  }
185
180
  } else if (strcmp(key_cstr, "id") == 0) {
186
181
  long len = RARRAY_LEN(value);
187
182
 
188
183
  Check_Type(value, T_ARRAY);
189
184
  len = RARRAY_LEN(value);
190
- if (len == 0) {
191
- return rb_str_new_cstr("");
192
- } else {
185
+ if (len != 0) {
193
186
  long i;
194
187
  VALUE ary = rb_ary_new_capa(len);
195
188
  for (i = 0; i < len; i++) {
196
189
  VALUE v = RARRAY_AREF(value, i);
197
190
  rb_ary_push(ary, rb_funcall(v, id_to_s, 0));
198
191
  }
199
- return put_attribute(attr_quote, key, rb_ary_join(ary, rb_str_new_cstr("_")));
192
+ put_attribute(buf, attr_quote, key, rb_ary_join(ary, rb_const_get(rb_mAttributeBuilder, id_underscore)));
200
193
  }
201
194
  } else if (RB_TYPE_P(value, T_TRUE)) {
202
195
  if (is_html) {
203
- VALUE attr = rb_str_buf_new(1 + RSTRING_LEN(key));
204
- rb_str_buf_cat(attr, " ", 1);
205
- rb_str_buf_append(attr, key);
206
- return attr;
196
+ rb_ary_push(buf, rb_const_get(rb_mAttributeBuilder, id_space));
197
+ rb_ary_push(buf, key);
207
198
  } else {
208
- return put_attribute(attr_quote, key, key);
199
+ put_attribute(buf, attr_quote, key, key);
209
200
  }
210
201
  } else if (RB_TYPE_P(value, T_FALSE) || NIL_P(value)) {
211
- return Qnil;
202
+ /* do nothing */
212
203
  } else {
213
- return put_attribute(attr_quote, key, value);
204
+ put_attribute(buf, attr_quote, key, value);
214
205
  }
215
- return value;
216
206
  }
217
207
 
218
208
  static VALUE
@@ -226,17 +216,17 @@ m_build(int argc, VALUE *argv, RB_UNUSED_VAR(VALUE self))
226
216
  attr_quote = argv[0];
227
217
  is_html = RTEST(argv[1]);
228
218
  attributes = rb_hash_new();
229
- rb_hash_aset(attributes, rb_str_new_cstr("id"), rb_ary_new());
230
- rb_hash_aset(attributes, rb_str_new_cstr("class"), rb_ary_new());
219
+ rb_hash_aset(attributes, rb_const_get(rb_mAttributeBuilder, id_id), rb_ary_new());
220
+ rb_hash_aset(attributes, rb_const_get(rb_mAttributeBuilder, id_class), rb_ary_new());
231
221
  merge(attributes, argc-2, argv+2);
232
222
 
233
223
  keys = rb_funcall(attributes, id_keys, 0);
234
224
  rb_funcall(keys, id_sort_bang, 0);
235
225
  len = RARRAY_LEN(keys);
236
- buf = rb_ary_new_capa(len);
226
+ buf = rb_ary_new();
237
227
  for (i = 0; i < len; i++) {
238
228
  VALUE k = RARRAY_AREF(keys, i);
239
- rb_ary_push(buf, build_attribute(attr_quote, is_html, k, rb_hash_lookup(attributes, k)));
229
+ build_attribute(buf, attr_quote, is_html, k, rb_hash_lookup(attributes, k));
240
230
  }
241
231
 
242
232
  return rb_ary_join(buf, Qnil);
@@ -265,5 +255,20 @@ Init_attribute_builder(void)
265
255
  id_escape_html = rb_intern("escape_html");
266
256
  id_gsub = rb_intern("gsub");
267
257
  id_to_s = rb_intern("to_s");
258
+
259
+ id_id = rb_intern("ID");
260
+ id_class = rb_intern("CLASS");
261
+ id_underscore = rb_intern("UNDERSCORE");
262
+ id_hyphen = rb_intern("HYPHEN");
263
+ id_space = rb_intern("SPACE");
264
+ id_equal = rb_intern("=");
265
+
266
+ rb_const_set(rb_mAttributeBuilder, id_id, rb_obj_freeze(rb_str_new_cstr("id")));
267
+ rb_const_set(rb_mAttributeBuilder, id_class, rb_obj_freeze(rb_str_new_cstr("class")));
268
+ rb_const_set(rb_mAttributeBuilder, id_underscore, rb_obj_freeze(rb_str_new_cstr("_")));
269
+ rb_const_set(rb_mAttributeBuilder, id_hyphen, rb_obj_freeze(rb_str_new_cstr("-")));
270
+ rb_const_set(rb_mAttributeBuilder, id_space, rb_obj_freeze(rb_str_new_cstr(" ")));
271
+ rb_const_set(rb_mAttributeBuilder, id_equal, rb_obj_freeze(rb_str_new_cstr("=")));
272
+
268
273
  rb_require("temple");
269
274
  }
@@ -1,10 +1,11 @@
1
- require 'parser/current'
1
+ require 'ripper'
2
2
  require 'temple'
3
3
  require 'haml_parser/ast'
4
4
  require 'faml/error'
5
5
  require 'faml/filter_compilers'
6
6
  require 'faml/helpers'
7
7
  require 'faml/rails_helpers'
8
+ require 'faml/ruby_syntax_checker'
8
9
  require 'faml/static_hash_parser'
9
10
  require 'faml/text_compiler'
10
11
 
@@ -295,13 +296,9 @@ module Faml
295
296
  end
296
297
 
297
298
  def assert_valid_ruby_code!(text)
298
- parser = ::Parser::CurrentRuby.new
299
- parser.diagnostics.consumer = nil
300
- buffer = ::Parser::Source::Buffer.new('(faml)')
301
- buffer.source = "call(#{text})"
302
- parser.parse(buffer)
299
+ RubySyntaxChecker.new("call(#{text})", '(faml)').parse
303
300
  true
304
- rescue ::Parser::SyntaxError
301
+ rescue RubySyntaxChecker::Error
305
302
  raise UnparsableRubyCode.new("Unparsable Ruby code is given to attributes: #{text}", nil)
306
303
  end
307
304
 
@@ -0,0 +1,14 @@
1
+ require 'ripper'
2
+
3
+ module Faml
4
+ class RubySyntaxChecker < Ripper
5
+ class Error < StandardError
6
+ end
7
+
8
+ private
9
+
10
+ def on_parse_error(*)
11
+ raise Error
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module Faml
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kohei Suzuki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-13 00:00:00.000000000 Z
11
+ date: 2015-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escape_utils
@@ -357,6 +357,7 @@ files:
357
357
  - lib/faml/rails_handler.rb
358
358
  - lib/faml/rails_helpers.rb
359
359
  - lib/faml/railtie.rb
360
+ - lib/faml/ruby_syntax_checker.rb
360
361
  - lib/faml/static_hash_parser.rb
361
362
  - lib/faml/text_compiler.rb
362
363
  - lib/faml/tilt.rb