faml 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f0a4ce48d756e7ce5bc2bfe6f2ee8506c936c13
4
- data.tar.gz: a15d13d5b441128441e921c58e71299d938059cc
3
+ metadata.gz: c7f71b5e360cafa8cef276e06dbc8c8b0c2214bb
4
+ data.tar.gz: b94247f91e3508b3261686ad678eb539cf535ea5
5
5
  SHA512:
6
- metadata.gz: 7a7fb11f2ac4d7066de1015abc4a97f5c978a21701ef7e9a151bb3df82ce766fcca7ff1bce41bd6fc4765616db4e336d56da2f4a72b4e2752386650af6e2d591
7
- data.tar.gz: 46e713540d00f7f3bc0c6972b6e048f72d0751c1cf10d4a40b31bf7b0d8707b7a0abe618a9f8e997eeca5c89e5d51c955f7b0bd5cd96418ac7d474c6147554de
6
+ metadata.gz: 2aa2f8a5e0e40444fa7c87d1f864fe0ba713b7f2a76ddcc722635233a43a43a8b20ad480f599636a2f85f84a0018ba0adf912f0b2399f537b8c95ae0af26552c
7
+ data.tar.gz: e11276dff335fe35d1c95753539ea45be0500e09ff030a616d53b587b6cc5570869cc40ad0247f33c8deaefd0c764e9a8c1a424ae522faf87f50712e1a62087a
@@ -1,3 +1,7 @@
1
+ ## 0.5.0 (2015-11-17)
2
+ - Support object reference syntax
3
+ - https://github.com/eagletmt/faml/pull/32
4
+
1
5
  ## 0.4.2 (2015-11-16)
2
6
  - Fix CLI slow startup
3
7
  - https://github.com/eagletmt/faml/issues/21
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.required_ruby_version = '>= 2.0.0'
22
22
 
23
23
  spec.add_dependency 'escape_utils'
24
- spec.add_dependency 'haml_parser', '>= 0.2.0'
24
+ spec.add_dependency 'haml_parser', '>= 0.3.0'
25
25
  spec.add_dependency 'parser'
26
26
  spec.add_dependency 'temple', '>= 0.7.0'
27
27
  spec.add_dependency 'tilt'
@@ -1,7 +1,7 @@
1
1
  # Incompatibilities
2
2
  ## Versions
3
3
  - Haml 4.0.7
4
- - Faml 0.4.2
4
+ - Faml 0.5.0
5
5
  - Hamlit 1.7.2
6
6
 
7
7
  ## Table of contents
@@ -459,3 +459,57 @@
459
459
  ... ^
460
460
  ```
461
461
 
462
+ # [./spec/render/attribute_spec.rb:215](../../../spec/render/attribute_spec.rb#L215)
463
+ ## Input
464
+ ```haml
465
+ %span[Faml::TestStruct.new(123)] hello
466
+ ```
467
+
468
+ ## Faml, Haml
469
+ ```html
470
+ <span class='faml_test_struct' id='faml_test_struct_123'>hello</span>
471
+
472
+ ```
473
+
474
+ ## Hamlit
475
+ ```html
476
+ <span>[Faml::TestStruct.new(123)] hello</span>
477
+
478
+ ```
479
+
480
+ # [./spec/render/attribute_spec.rb:219](../../../spec/render/attribute_spec.rb#L219)
481
+ ## Input
482
+ ```haml
483
+ %span[Faml::TestStruct.new(123), :hello] hello
484
+ ```
485
+
486
+ ## Faml, Haml
487
+ ```html
488
+ <span class='hello_faml_test_struct' id='hello_faml_test_struct_123'>hello</span>
489
+
490
+ ```
491
+
492
+ ## Hamlit
493
+ ```html
494
+ <span>[Faml::TestStruct.new(123), :hello] hello</span>
495
+
496
+ ```
497
+
498
+ # [./spec/render/attribute_spec.rb:223](../../../spec/render/attribute_spec.rb#L223)
499
+ ## Input
500
+ ```haml
501
+ %span[Faml::TestRefStruct.new(123)] hello
502
+ ```
503
+
504
+ ## Faml, Haml
505
+ ```html
506
+ <span class='faml_test' id='faml_test_123'>hello</span>
507
+
508
+ ```
509
+
510
+ ## Hamlit
511
+ ```html
512
+ <span>[Faml::TestRefStruct.new(123)] hello</span>
513
+
514
+ ```
515
+
@@ -4,6 +4,7 @@ require 'haml_parser/ast'
4
4
  require_relative 'error'
5
5
  require_relative 'filter_compilers'
6
6
  require_relative 'helpers'
7
+ require_relative 'object_ref'
7
8
  require_relative 'rails_helpers'
8
9
  require_relative 'ruby_syntax_checker'
9
10
  require_relative 'static_hash_parser'
@@ -181,7 +182,7 @@ module Faml
181
182
  :haml, :tag,
182
183
  ast.tag_name,
183
184
  self_closing?(ast),
184
- compile_attributes(ast.attributes, ast.static_id, ast.static_class),
185
+ compile_attributes(ast.attributes, ast.static_id, ast.static_class, ast.object_ref),
185
186
  ]
186
187
 
187
188
  if ast.oneline_child
@@ -223,15 +224,17 @@ module Faml
223
224
  ast.nuke_inner_whitespace || options[:preserve].include?(ast.tag_name)
224
225
  end
225
226
 
226
- def compile_attributes(text, static_id, static_class)
227
- if text.empty?
227
+ def compile_attributes(text, static_id, static_class, object_ref)
228
+ if !object_ref && text.empty?
228
229
  return compile_static_id_and_class(static_id, static_class)
229
230
  end
230
231
 
231
- attrs = try_optimize_attributes(text, static_id, static_class)
232
- if attrs
233
- line_count = text.count("\n")
234
- return [:multi, [:html, :attrs, *attrs]].concat([[:newline]] * line_count)
232
+ unless object_ref
233
+ attrs = try_optimize_attributes(text, static_id, static_class)
234
+ if attrs
235
+ line_count = text.count("\n")
236
+ return [:multi, [:html, :attrs, *attrs]].concat([[:newline]] * line_count)
237
+ end
235
238
  end
236
239
 
237
240
  # Slow version
@@ -244,13 +247,17 @@ module Faml
244
247
  h[:id] = static_id
245
248
  end
246
249
 
247
- t =
248
- if h.empty?
249
- text
250
- else
251
- "#{h.inspect}, #{text}"
252
- end
253
- [:haml, :attrs, t]
250
+ codes = []
251
+ unless h.empty?
252
+ codes << h.inspect
253
+ end
254
+ if object_ref
255
+ codes << "::Faml::ObjectRef.render(#{object_ref})"
256
+ end
257
+ unless text.empty?
258
+ codes << text
259
+ end
260
+ [:haml, :attrs, codes.join(', ')]
254
261
  end
255
262
 
256
263
  def compile_static_id_and_class(static_id, static_class)
@@ -0,0 +1,50 @@
1
+ module Faml
2
+ module ObjectRef
3
+ class << self
4
+ def render(ref = nil, prefix = nil, *)
5
+ h = {}
6
+ if ref.nil?
7
+ return h
8
+ end
9
+ c = class_name(ref)
10
+ i = "#{c}_#{id(ref) || 'new'}"
11
+ if prefix
12
+ c = "#{prefix}_#{c}"
13
+ i = "#{prefix}_#{i}"
14
+ end
15
+ { id: i, class: c }
16
+ end
17
+
18
+ private
19
+
20
+ def class_name(ref)
21
+ if ref.respond_to?(:haml_object_ref)
22
+ ref.haml_object_ref
23
+ else
24
+ underscore(ref.class)
25
+ end
26
+ end
27
+
28
+ def id(ref)
29
+ if ref.respond_to?(:to_key)
30
+ key = ref.to_key
31
+ if key
32
+ key.join('_')
33
+ end
34
+ else
35
+ ref.id
36
+ end
37
+ end
38
+
39
+ def underscore(camel_cased_word)
40
+ word = camel_cased_word.to_s.dup
41
+ word.gsub!(/::/, '_')
42
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
43
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
44
+ word.tr!('-', '_')
45
+ word.downcase!
46
+ word
47
+ end
48
+ end
49
+ end
50
+ end
@@ -13,6 +13,7 @@ module Faml
13
13
  :dynamic_attribute_with_data_count,
14
14
  :dynamic_attribute_with_newline_count,
15
15
  :ruby_attribute_count,
16
+ :object_reference_count,
16
17
  :multi_attribute_count,
17
18
  :ast_types
18
19
  ) do
@@ -99,6 +100,11 @@ module Faml
99
100
  end
100
101
 
101
102
  def collect_attribute_info(info, ast)
103
+ if ast.object_ref
104
+ info.object_reference_count += 1
105
+ return
106
+ end
107
+
102
108
  if ast.attributes.empty?
103
109
  if ast.static_class.empty? && ast.static_id.empty?
104
110
  info.empty_attribute_count += 1
@@ -133,7 +139,7 @@ module Faml
133
139
  def report_attribute_stats(info)
134
140
  static = info.static_attribute_count
135
141
  dynamic = info.dynamic_attribute_count + info.dynamic_attribute_with_data_count + info.dynamic_attribute_with_newline_count
136
- ruby = info.ruby_attribute_count + info.multi_attribute_count
142
+ ruby = info.ruby_attribute_count + info.multi_attribute_count + info.object_reference_count
137
143
  total = static + dynamic + ruby
138
144
  puts 'Attribute stats'
139
145
  printf(" Empty attributes: %d\n", info.empty_attribute_count)
@@ -144,6 +150,7 @@ module Faml
144
150
  printf(" with newline: %d\n", info.dynamic_attribute_with_newline_count)
145
151
  printf(" Ruby attributes: %d (%.2f%%)\n", ruby, ruby * 100.0 / total)
146
152
  printf(" with multiple arguments: %d\n", info.multi_attribute_count)
153
+ printf(" with object reference: %d\n", info.object_reference_count)
147
154
  end
148
155
 
149
156
  def report_ast_stats(info)
@@ -1,3 +1,3 @@
1
1
  module Faml
2
- VERSION = '0.4.2'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -5,4 +5,8 @@ class BooksController < ApplicationController
5
5
  def with_variables
6
6
  @book = Book.new(params.permit(:title))
7
7
  end
8
+
9
+ def object_ref
10
+ @book = Book.new(id: params[:id])
11
+ end
8
12
  end
@@ -1,7 +1,7 @@
1
1
  class Book
2
2
  include ActiveModel::Model
3
3
 
4
- attr_accessor :title
4
+ attr_accessor :id, :title
5
5
 
6
6
  def to_s
7
7
  "<span>#{title}</span>".html_safe
@@ -0,0 +1,2 @@
1
+ %div[@book]
2
+ %p= @book.title
@@ -12,5 +12,6 @@ Rails.application.routes.draw do
12
12
  get :invalid_interpolation
13
13
  get :filter_not_found
14
14
  get :filter_invalid_interpolation
15
+ get :object_ref
15
16
  end
16
17
  end
@@ -45,6 +45,20 @@ RSpec.describe 'Faml with Rails', type: :request do
45
45
  expect(response.body).to include('&lt;marquee&gt;escape me&lt;/marquee&gt;')
46
46
  end
47
47
 
48
+ describe 'object reference' do
49
+ it 'works with new object' do
50
+ get '/books/object_ref'
51
+ expect(response).to be_ok
52
+ expect(response.body).to include("<div class='book' id='book_new'>")
53
+ end
54
+
55
+ it 'works with id' do
56
+ get '/books/object_ref', id: 123
57
+ expect(response).to be_ok
58
+ expect(response.body).to include("<div class='book' id='book_123'>")
59
+ end
60
+ end
61
+
48
62
  describe 'preserve helper' do
49
63
  it 'returns html_safe string' do
50
64
  get '/books/preserve'
@@ -210,4 +210,18 @@ HAML
210
210
  %span{key => val}
211
211
  HAML
212
212
  end
213
+
214
+ describe 'object reference' do
215
+ it 'renders id and class attribute' do
216
+ expect(render_string('%span[Faml::TestStruct.new(123)] hello')).to eq("<span class='faml_test_struct' id='faml_test_struct_123'>hello</span>\n")
217
+ end
218
+
219
+ it 'renders id and class attribute with prefix' do
220
+ expect(render_string('%span[Faml::TestStruct.new(123), :hello] hello')).to eq("<span class='hello_faml_test_struct' id='hello_faml_test_struct_123'>hello</span>\n")
221
+ end
222
+
223
+ it 'renders id and class attribute with haml_object_ref' do
224
+ expect(render_string('%span[Faml::TestRefStruct.new(123)] hello')).to eq("<span class='faml_test' id='faml_test_123'>hello</span>\n")
225
+ end
226
+ end
213
227
  end
@@ -31,6 +31,15 @@ module RenderSpecHelper
31
31
  end
32
32
  end
33
33
 
34
+ module Faml
35
+ TestStruct = Struct.new(:id)
36
+ TestRefStruct = Struct.new(:id) do
37
+ def haml_object_ref
38
+ 'faml_test'
39
+ end
40
+ end
41
+ end
42
+
34
43
  RSpec.configure do |config|
35
44
  config.expect_with :rspec do |expectations|
36
45
  expectations.include_chain_clauses_in_custom_matcher_descriptions = true
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.4.2
4
+ version: 0.5.0
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-11-16 00:00:00.000000000 Z
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escape_utils
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.2.0
33
+ version: 0.3.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 0.2.0
40
+ version: 0.3.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: parser
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -371,6 +371,7 @@ files:
371
371
  - lib/faml/helpers.rb
372
372
  - lib/faml/html.rb
373
373
  - lib/faml/newline.rb
374
+ - lib/faml/object_ref.rb
374
375
  - lib/faml/rails_handler.rb
375
376
  - lib/faml/rails_helpers.rb
376
377
  - lib/faml/railtie.rb
@@ -400,6 +401,7 @@ files:
400
401
  - spec/rails/app/views/books/hello.html.haml
401
402
  - spec/rails/app/views/books/indent_error.html.haml
402
403
  - spec/rails/app/views/books/invalid_interpolation.html.haml
404
+ - spec/rails/app/views/books/object_ref.html.haml
403
405
  - spec/rails/app/views/books/preserve.html.haml
404
406
  - spec/rails/app/views/books/syntax_error.html.haml
405
407
  - spec/rails/app/views/books/unparsable.html.haml
@@ -557,6 +559,7 @@ test_files:
557
559
  - spec/rails/app/views/books/hello.html.haml
558
560
  - spec/rails/app/views/books/indent_error.html.haml
559
561
  - spec/rails/app/views/books/invalid_interpolation.html.haml
562
+ - spec/rails/app/views/books/object_ref.html.haml
560
563
  - spec/rails/app/views/books/preserve.html.haml
561
564
  - spec/rails/app/views/books/syntax_error.html.haml
562
565
  - spec/rails/app/views/books/unparsable.html.haml