express_templates 0.2.0 → 0.2.2

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.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -6
  3. data/lib/core_extensions/proc.rb +42 -0
  4. data/lib/express_templates.rb +7 -2
  5. data/lib/express_templates/compiler.rb +27 -0
  6. data/lib/express_templates/components.rb +7 -4
  7. data/lib/express_templates/components/base.rb +54 -0
  8. data/lib/express_templates/components/capabilities/conditionality.rb +52 -0
  9. data/lib/express_templates/components/capabilities/configurable.rb +91 -0
  10. data/lib/express_templates/components/capabilities/iterating.rb +80 -0
  11. data/lib/express_templates/components/capabilities/parenting.rb +74 -0
  12. data/lib/express_templates/components/capabilities/rendering.rb +93 -0
  13. data/lib/express_templates/components/capabilities/templating.rb +196 -0
  14. data/lib/express_templates/components/capabilities/wrapping.rb +100 -0
  15. data/lib/express_templates/components/column.rb +13 -0
  16. data/lib/express_templates/components/container.rb +7 -0
  17. data/lib/express_templates/components/form_rails_support.rb +19 -0
  18. data/lib/express_templates/components/row.rb +28 -0
  19. data/lib/express_templates/expander.rb +51 -35
  20. data/lib/express_templates/indenter.rb +44 -0
  21. data/lib/express_templates/macro.rb +44 -0
  22. data/lib/express_templates/markup.rb +9 -0
  23. data/lib/express_templates/{components → markup}/html_tag.rb +9 -3
  24. data/lib/express_templates/markup/tag.rb +118 -0
  25. data/lib/express_templates/markup/wrapper.rb +88 -0
  26. data/lib/express_templates/{components → markup}/yielder.rb +2 -2
  27. data/lib/express_templates/renderer.rb +3 -8
  28. data/lib/express_templates/template/handler.rb +1 -1
  29. data/lib/express_templates/version.rb +1 -1
  30. data/lib/tasks/{gara_tasks.rake → express_templates.rake} +0 -0
  31. data/test/compiler_test.rb +9 -0
  32. data/test/components/base_test.rb +77 -0
  33. data/test/components/column_test.rb +11 -0
  34. data/test/components/conditionality_test.rb +37 -0
  35. data/test/components/configurable_test.rb +43 -0
  36. data/test/components/container_test.rb +49 -0
  37. data/test/components/iterating_test.rb +85 -0
  38. data/test/components/row_test.rb +16 -0
  39. data/test/core_extensions/proc_test.rb +41 -0
  40. data/test/dummy/log/test.log +72489 -0
  41. data/test/expander_test.rb +55 -13
  42. data/test/{gara_test.rb → express_templates_test.rb} +0 -0
  43. data/test/handler_test.rb +4 -4
  44. data/test/indenter_test.rb +25 -0
  45. data/test/markup/tag_test.rb +127 -0
  46. data/test/markup/wrapper_test.rb +42 -0
  47. data/test/markup/yielder_test.rb +9 -0
  48. data/test/performance_test.rb +2 -2
  49. data/test/test_helper.rb +6 -0
  50. metadata +78 -24
  51. data/lib/express_templates/component.rb +0 -92
  52. data/lib/express_templates/components/wrapper.rb +0 -51
  53. data/lib/express_templates/html5_emitter.rb +0 -45
  54. data/test/component_test.rb +0 -77
  55. data/test/wrapper_test.rb +0 -23
@@ -1,92 +0,0 @@
1
- module ExpressTemplates
2
- class Component
3
-
4
- attr_accessor :children
5
-
6
- INDENT = ' '
7
-
8
- def initialize(*children_or_options)
9
- @children = []
10
- @options = {}.with_indifferent_access
11
- _process(*children_or_options)
12
- end
13
-
14
- def self.macro_name
15
- @macro_name ||= to_s.split('::').last.underscore
16
- end
17
-
18
- def macro_name ; self.class.macro_name end
19
-
20
- def html_options
21
- @options.each_pair.map do |name, value|
22
- %Q(#{name}=\\"#{value}\\")
23
- end.join(" ")
24
- end
25
-
26
- def start_tag
27
- "<#{macro_name}#{html_options.empty? ? '' : ' '+html_options}>"
28
- end
29
-
30
- def close_tag
31
- "</#{macro_name}>"
32
- end
33
-
34
- def add_css_class(css_class)
35
- @options['class'] ||= ''
36
- @options['class'] = (@options['class'].split + [css_class]).join(" ")
37
- end
38
-
39
- def method_missing(name, *args)
40
- add_css_class(name)
41
- _process(*args) unless args.empty?
42
- return self
43
- end
44
-
45
- def compile
46
- ruby_fragments = @children.map do |child|
47
- if child.respond_to?(:compile)
48
- child.compile
49
- else
50
- %Q("#{child}")
51
- end
52
- end
53
- ruby_fragments.unshift %Q("#{start_tag}")
54
- ruby_fragments.push %Q("#{close_tag}")
55
- ruby_fragments.reject {|frag| frag.empty? }.join("+")
56
- end
57
-
58
- def to_template(depth = 0)
59
- template_fragments = @children.map do |child|
60
- if child.kind_of?(ExpressTemplates::Component)
61
- child.to_template(depth+1)
62
- else
63
- child
64
- end
65
- end
66
- indent = INDENT*(depth+1)
67
- macro_name + _blockify(template_fragments.join("\n#{indent}"), depth)
68
- end
69
-
70
- private
71
- def _indent(code)
72
- code.split("\n").map {|line| INDENT + line }.join("\n")
73
- end
74
-
75
- def _blockify(code, depth)
76
- indent = INDENT*depth
77
- code.empty? ? code : " {\n#{_indent(code)}\n}\n"
78
- end
79
-
80
- def _process(*children_or_options)
81
- children_or_options.each do |child_or_option|
82
- if child_or_option.kind_of?(Hash)
83
- @options.merge!(child_or_option)
84
- else
85
- @children << child_or_option
86
- end
87
- end
88
- end
89
-
90
-
91
- end
92
- end
@@ -1,51 +0,0 @@
1
- module ExpressTemplates
2
- module Components
3
- # wrap locals and helpers for evaluation during render
4
- class Wrapper
5
-
6
- attr_accessor :name, :args
7
-
8
- def initialize(name, *args)
9
- @name = name
10
- @args = args
11
- end
12
-
13
- def compile
14
- # insure nils do not blow up view
15
- %Q("\#\{#{_compile}\}")
16
- end
17
-
18
- def to_template
19
- _compile
20
- end
21
-
22
- def children
23
- []
24
- end
25
-
26
- private
27
- def _compile
28
- if @args.empty?
29
- return name
30
- else
31
- args_string = args.slice(0..-2).map(&:inspect).join(', ')
32
- last_arg = ''
33
- if args.last.is_a?(Hash) # expand a hash
34
- unless args.last.empty?
35
- # This approach has limitations - will only work on structures of
36
- # immediate values
37
- last_arg = args.last.inspect.match(/^\{(.*)\}$/)[1]
38
- last_arg.gsub!(/:(\w+)=>/, '\1: ') # use ruby 2 hash syntax
39
- else
40
- last_arg = "{}" # empty hash
41
- end
42
- else
43
- last_arg = args.last.inspect
44
- end
45
- args_string << (args_string.empty? ? last_arg : ", #{last_arg}")
46
- return "#{name}(#{args_string})"
47
- end
48
- end
49
- end
50
- end
51
- end
@@ -1,45 +0,0 @@
1
- module ExpressTemplates
2
-
3
- HTML5_TAGS = [ :a, :abbr, :address, :area, :article, :aside, :audio,
4
- :b, :base, :bdi, :bdo, :blockquote, :body, :br, :button,
5
- :canvas, :caption, :cite, :code, :col, :colgroup,
6
- :data, :datalist, :dd, :del, :details, :dfn, :div, :dl, :dt,
7
- :em, :embed,
8
- :fieldset, :figcaption, :figure, :footer, :form,
9
- :h1, :h2, :h3, :h4, :h5, :h6, :head, :header, :hr, :html,
10
- :i, :iframe, :img, :input, :ins,
11
- :kbd, :keygen,
12
- :label, :legend, :li, :link,
13
- :main, :map, :mark, :math, :menu, :menuitem, :meta, :meter,
14
- :nav, :noscript,
15
- :object, :ol, :optgroup, :option, :output,
16
- :p, :param, :pre, :progress,
17
- :q,
18
- :rp, :rt, :ruby,
19
- :s, :samp, :script, :section, :select, :small, :source,
20
- :span, :strong, :style, :sub, :sup, :summary, :svg,
21
- :table, :tbody, :td, :textarea, :tfoot, :th, :thead, :time, :title,
22
- :tr, :track,
23
- :u, :ul,
24
- :var, :video,
25
- :wbr]
26
-
27
- module Html5Emitter
28
-
29
- HTML5_TAGS.each do |tag|
30
- define_method(tag) do |*args, &block|
31
- if block
32
- content_tag(tag, capture(&block), *args)
33
- else
34
- # adjust args because content_tag expects options in the
35
- # second position when have nil content
36
- if args.first.is_a?(Hash)
37
- content_tag(tag, *(args.unshift(nil)))
38
- else
39
- content_tag(tag, *args)
40
- end
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,77 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ComponentTest < ActiveSupport::TestCase
4
-
5
- class Bare < ExpressTemplates::Component ; end
6
- class Sub < ExpressTemplates::Component ; end
7
-
8
- def bare_component(*args)
9
- Bare.new(*args)
10
- end
11
-
12
- def sub_component(*args)
13
- Sub.new(*args)
14
- end
15
-
16
-
17
- test "#macro_name returns the name of the class" do
18
- assert_equal 'bare', bare_component.macro_name
19
- end
20
-
21
- test "#compile returns a string" do
22
- assert_kind_of String, bare_component.compile
23
- end
24
-
25
- test "has no children" do
26
- assert_empty bare_component.children
27
- end
28
-
29
- def bare_with_2_children
30
- component = bare_component "child1", "child2"
31
- end
32
-
33
- test "can be created with children" do
34
- assert_equal 2, bare_with_2_children.children.size
35
- assert_equal "child2", bare_with_2_children.children.last
36
- end
37
-
38
- test "#compile on bare_with_2_children yields '\"<bare>\"+\"child1\"+\"child2\"+\"</bare>\"'" do
39
- assert_equal '"<bare>"+"child1"+"child2"+"</bare>"', bare_with_2_children.compile
40
- end
41
-
42
- test "#start_tag is my macro_name as an xml start tag" do
43
- assert_equal "<#{bare_component.macro_name}>", bare_component.start_tag
44
- end
45
-
46
- test "#close_tag is my macro_name as an xml close tag" do
47
- assert_equal "</#{bare_component.macro_name}>", bare_component.close_tag
48
- end
49
-
50
- def component_with_subcomponent
51
- bare_component sub_component
52
- end
53
-
54
- test "#compile on component_with_subcomponent returns a string which when eval'd looks like '<bare><sub></sub></bare>'" do
55
- assert_equal '<bare><sub></sub></bare>', eval(component_with_subcomponent.compile)
56
- end
57
-
58
- test "#to_template on bare_component returns 'bare'" do
59
- assert_equal 'bare', bare_component.to_template
60
- end
61
-
62
- test "#to_template on component_with_subcomponent returns 'bare {\n sub\n}\n'" do
63
- assert_equal "bare {\n sub\n}\n", component_with_subcomponent.to_template
64
- end
65
-
66
- test "#to_template on nested components indents properly'" do
67
- expected = %Q(bare {
68
- sub {
69
- sub
70
- }
71
- }
72
- )
73
- assert_equal expected, Bare.new(Sub.new(Sub.new)).to_template
74
- end
75
-
76
-
77
- end
data/test/wrapper_test.rb DELETED
@@ -1,23 +0,0 @@
1
- require 'test_helper'
2
-
3
- class WrapperTest < ActiveSupport::TestCase
4
-
5
- test "name compiles to just name" do
6
- assert_equal '"#{foo}"', ExpressTemplates::Components::Wrapper.new('foo').compile
7
- end
8
-
9
- test "simple args are preserved" do
10
- wrapper = ExpressTemplates::Components::Wrapper.new('foo', "xyzzy", 'bar', "baz", 1, false, 3)
11
- assert_equal '"#{foo("xyzzy", "bar", "baz", 1, false, 3)}"', wrapper.compile
12
- end
13
-
14
- test "args are preserved" do
15
- wrapper = ExpressTemplates::Components::Wrapper.new('foo', "xyzzy", bar: "baz")
16
- assert_equal '"#{foo("xyzzy", bar: "baz")}"', wrapper.compile
17
- end
18
-
19
- test "something returning nil when wrapped and compiled, evals to an empty string" do
20
- assert_equal '', eval(ExpressTemplates::Components::Wrapper.new('nil').compile)
21
- end
22
-
23
- end