express_templates 0.11.11 → 0.11.13

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: 3e40896f62363f0d0d56561edadcfc3bed0ff65c
4
- data.tar.gz: eed5459b88dbba34fbbb3b333aa3d7b84deb7bb6
3
+ metadata.gz: 777b6b9b183caeb311d0c70cac1f01d28926fc26
4
+ data.tar.gz: 2a92573b49a5853af5b99f86125998985c65e164
5
5
  SHA512:
6
- metadata.gz: 730210a7b8ea70b45d4668d7da93e0756e52f1e1a3771919b547e74e75958efac1baa6c374a100f04f9da7a710e9dde9265b76265daf704065d761c0c88bde16
7
- data.tar.gz: e1d8537af4ef7094e9d29ae3b67f9083dfb3e14aa583849811b07ba741bf16b5cf1d684b55fa973cc469e8a5d4c4d319a4b50d0e1eadf3cbbd85902e0aeeeb87
6
+ metadata.gz: acb771f468452735b568bf8ce8aee2541b9e0a8605c70e58263cd9d91c3052008c7ec68255e2cdef081074d5102c8ef9ca83827acae9f6065629e610fcf6dfdb
7
+ data.tar.gz: 1c7ea931f212a887c2ffe4a1991ac45f1b3850329eb45948ed247c5864a8160f7b692a428622cf8eee30c75c2a21d47f384c83e9957ebbaa53a784e6c77bc83b
data/lib/arbre/patches.rb CHANGED
@@ -14,6 +14,7 @@ module Arbre
14
14
  # are expected to be able to contain other components or template code
15
15
  # without use of a builder style syntax
16
16
  def build_tag(klass, *args, &block)
17
+ return if should_supress_output?(args)
17
18
  tag = klass.new(arbre_context)
18
19
  tag.parent = current_arbre_element
19
20
 
@@ -28,6 +29,22 @@ module Arbre
28
29
  tag
29
30
  end
30
31
 
32
+ # Conditionally do not emit markup
33
+ # Example that would supress generation of the h2 markup:
34
+ # * h2(only_when: false) { "Some text" }
35
+ # * h2(unless: true) { "Some text" }
36
+ def should_supress_output?(args)
37
+ if args.last.kind_of?(Hash)
38
+ only_when = args.last[:only_when]
39
+ unless_condition = args.last[:unless]
40
+ return true if only_when === false
41
+ return true if !unless_condition.nil? && !!unless_condition
42
+ else
43
+ false
44
+ end
45
+ end
46
+
47
+
31
48
  def on_component_error(tag, exception)
32
49
  tag.content = "Error rendering #{tag.class} component: #{exception.message}"
33
50
  ::Rails.logger.error exception
@@ -0,0 +1,165 @@
1
+ module ExpressTemplates
2
+ module Components
3
+ module Capabilities
4
+
5
+ module Configurable
6
+
7
+ def self.included(base)
8
+ base.class_eval do
9
+
10
+ class_attribute :supported_options
11
+ self.supported_options = {}
12
+
13
+ class_attribute :supported_arguments
14
+ self.supported_arguments = {}
15
+
16
+
17
+ def self.emits(*args, &block)
18
+ warn ".emits is deprecrated"
19
+ self.contains(*args, &block)
20
+ end
21
+
22
+ def build(*args, &block)
23
+ _process_builder_args!(args)
24
+ super(*args, &block)
25
+ end
26
+
27
+ def config
28
+ @config ||= {}
29
+ end
30
+
31
+ def self.has_option(name, description, type: :string, required: nil, default: nil, attribute: nil, values: nil)
32
+ raise "name must be a symbol" unless name.kind_of?(Symbol)
33
+ option_definition = {description: description}
34
+ option_definition.merge!(type: type, required: required, default: default, attribute: attribute, values: values)
35
+ self.supported_options =
36
+ self.supported_options.merge(name => option_definition)
37
+ end
38
+
39
+ def required_options
40
+ supported_options.select {|k,v| v[:required] unless v[:default] }
41
+ end
42
+
43
+ def self.has_argument(name, description, as: nil, type: :string, default: nil, optional: false)
44
+ raise "name must be a symbol" unless name.kind_of?(Symbol)
45
+ argument_definition = {description: description, as: as, type: type, default: default, optional: optional}
46
+ self.supported_arguments =
47
+ self.supported_arguments.merge(name => argument_definition)
48
+ end
49
+
50
+ has_argument :id, "The id of the component.", type: :symbol, optional: true
51
+
52
+ protected
53
+
54
+ def _default_options
55
+ supported_options.select {|k,v| v[:default] }
56
+ end
57
+
58
+ def _check_required_options(supplied)
59
+ missing = required_options.keys - supplied.keys
60
+ if missing.any?
61
+ raise "#{self.class} missing required option(s): #{missing}"
62
+ end
63
+ end
64
+
65
+ def _set_defaults
66
+ _default_options.each do |key, value|
67
+ default_value = value[:default].respond_to?(:call) ? value[:default].call : value[:default]
68
+ if !!value[:attribute]
69
+ set_attribute key, default_value
70
+ else
71
+ if config[key].nil?
72
+ config[key] = default_value
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ def _valid_types(definition)
79
+ valid_type_names = if definition[:type].kind_of?(Symbol)
80
+ [definition[:type]]
81
+ elsif definition[:type].respond_to?(:keys)
82
+ definition[:type].keys
83
+ else
84
+ definition[:type] || []
85
+ end
86
+ valid_type_names.map do |type_name|
87
+ if type_name.eql?(:boolean)
88
+ type_name
89
+ else
90
+ type_name.to_s.classify.constantize
91
+ end
92
+ end
93
+ end
94
+
95
+ def _is_valid?(value, definition)
96
+ valid_types = _valid_types(definition)
97
+ if valid_types.empty? && value.kind_of?(String)
98
+ true
99
+ elsif valid_types.include?(value.class)
100
+ true
101
+ elsif valid_types.include?(:boolean) &&
102
+ [1, 0, true, false].include?(value)
103
+ true
104
+ else
105
+ false
106
+ end
107
+ end
108
+
109
+ def _optional_argument?(definition)
110
+ definition[:default] || definition[:optional]
111
+ end
112
+
113
+ def _required_argument?(definition)
114
+ !_optional_argument?(definition)
115
+ end
116
+
117
+ def _extract_supported_arguments!(args)
118
+ supported_arguments.each do |key, definition|
119
+ value = args.shift
120
+ if value.nil? && _required_argument?(definition)
121
+ raise "argument for #{key} not supplied"
122
+ end
123
+ unless _is_valid?(value, definition)
124
+ if _required_argument?(definition)
125
+ raise "argument for #{key} invalid (#{value.class}) '#{value.to_s}'; Allowable: #{_valid_types(definition).inspect}"
126
+ else
127
+ args.unshift value
128
+ next
129
+ end
130
+ end
131
+ config_key = definition[:as] || key
132
+ config[config_key] = value || definition[:default]
133
+ end
134
+ end
135
+
136
+ def _set_id_attribute
137
+ attributes[:id] = config[:id]
138
+ end
139
+
140
+ def _extract_supported_options!(builder_options)
141
+ builder_options.each do |key, value|
142
+ if supported_options.keys.include?(key)
143
+ unless supported_options[key][:attribute]
144
+ config[key] = builder_options.delete(key)
145
+ end
146
+ end
147
+ end
148
+ end
149
+
150
+ # TODO: this method should probably save the unprocessed builder options
151
+ # for passing to builder/helpers like in FormComponent
152
+ def _process_builder_args!(args)
153
+ _extract_supported_arguments!(args)
154
+ builder_options = args.last.try(:kind_of?, Hash) ? args.last : {}
155
+ _check_required_options(builder_options)
156
+ _extract_supported_options!(builder_options)
157
+ _set_defaults
158
+ _set_id_attribute
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,21 @@
1
+ module ExpressTemplates
2
+ module Components
3
+ module Capabilities
4
+
5
+ module Hideable
6
+
7
+ def self.included(base)
8
+ base.class_eval do
9
+
10
+ has_option :hidden, "Toggles visiblity of the component. Assumes hidden CSS class.", type: :boolean, default: false
11
+
12
+ before_build -> {
13
+ add_class 'hidden' if config[:hidden]
14
+ }
15
+
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -31,153 +31,7 @@ module ExpressTemplates
31
31
 
32
32
  abstract_component
33
33
 
34
- class_attribute :supported_options
35
- self.supported_options = {}
36
-
37
- class_attribute :supported_arguments
38
- self.supported_arguments = {}
39
-
40
- def self.emits(*args, &block)
41
- warn ".emits is deprecrated"
42
- self.contains(*args, &block)
43
- end
44
-
45
- def build(*args, &block)
46
- _process_builder_args!(args)
47
- super(*args, &block)
48
- end
49
-
50
- def config
51
- @config ||= {}
52
- end
53
-
54
- def self.has_option(name, description, type: :string, required: nil, default: nil, attribute: nil, values: nil)
55
- raise "name must be a symbol" unless name.kind_of?(Symbol)
56
- option_definition = {description: description}
57
- option_definition.merge!(type: type, required: required, default: default, attribute: attribute, values: values)
58
- self.supported_options =
59
- self.supported_options.merge(name => option_definition)
60
- end
61
-
62
- def required_options
63
- supported_options.select {|k,v| v[:required] unless v[:default] }
64
- end
65
-
66
- def self.has_argument(name, description, as: nil, type: :string, default: nil, optional: false)
67
- raise "name must be a symbol" unless name.kind_of?(Symbol)
68
- argument_definition = {description: description, as: as, type: type, default: default, optional: optional}
69
- self.supported_arguments =
70
- self.supported_arguments.merge(name => argument_definition)
71
- end
72
-
73
- has_argument :id, "The id of the component.", type: :symbol, optional: true
74
-
75
- protected
76
-
77
- def _default_options
78
- supported_options.select {|k,v| v[:default] }
79
- end
80
-
81
- def _check_required_options(supplied)
82
- missing = required_options.keys - supplied.keys
83
- if missing.any?
84
- raise "#{self.class} missing required option(s): #{missing}"
85
- end
86
- end
87
-
88
- def _set_defaults
89
- _default_options.each do |key, value|
90
- default_value = value[:default].respond_to?(:call) ? value[:default].call : value[:default]
91
- if !!value[:attribute]
92
- set_attribute key, default_value
93
- else
94
- if config[key].nil?
95
- config[key] = default_value
96
- end
97
- end
98
- end
99
- end
100
-
101
- def _valid_types(definition)
102
- valid_type_names = if definition[:type].kind_of?(Symbol)
103
- [definition[:type]]
104
- elsif definition[:type].respond_to?(:keys)
105
- definition[:type].keys
106
- else
107
- definition[:type] || []
108
- end
109
- valid_type_names.map do |type_name|
110
- if type_name.eql?(:boolean)
111
- type_name
112
- else
113
- type_name.to_s.classify.constantize
114
- end
115
- end
116
- end
117
-
118
- def _is_valid?(value, definition)
119
- valid_types = _valid_types(definition)
120
- if valid_types.empty? && value.kind_of?(String)
121
- true
122
- elsif valid_types.include?(value.class)
123
- true
124
- elsif valid_types.include?(:boolean) &&
125
- [1, 0, true, false].include?(value)
126
- true
127
- else
128
- false
129
- end
130
- end
131
-
132
- def _optional_argument?(definition)
133
- definition[:default] || definition[:optional]
134
- end
135
-
136
- def _required_argument?(definition)
137
- !_optional_argument?(definition)
138
- end
139
-
140
- def _extract_supported_arguments!(args)
141
- supported_arguments.each do |key, definition|
142
- value = args.shift
143
- if value.nil? && _required_argument?(definition)
144
- raise "argument for #{key} not supplied"
145
- end
146
- unless _is_valid?(value, definition)
147
- if _required_argument?(definition)
148
- raise "argument for #{key} invalid (#{value.class}) '#{value.to_s}'; Allowable: #{_valid_types(definition).inspect}"
149
- else
150
- args.unshift value
151
- next
152
- end
153
- end
154
- config_key = definition[:as] || key
155
- config[config_key] = value || definition[:default]
156
- end
157
- end
158
-
159
- def _set_id_attribute
160
- attributes[:id] = config[:id]
161
- end
162
-
163
- def _extract_supported_options!(builder_options)
164
- builder_options.each do |key, value|
165
- if supported_options.keys.include?(key)
166
- unless supported_options[key][:attribute]
167
- config[key] = builder_options.delete(key)
168
- end
169
- end
170
- end
171
- end
172
-
173
- def _process_builder_args!(args)
174
- _extract_supported_arguments!(args)
175
- builder_options = args.last.try(:kind_of?, Hash) ? args.last : {}
176
- _check_required_options(builder_options)
177
- _extract_supported_options!(builder_options)
178
- _set_defaults
179
- _set_id_attribute
180
- end
34
+ include Capabilities::Configurable
181
35
 
182
36
  end
183
37
  end
@@ -1,6 +1,8 @@
1
1
  module ExpressTemplates
2
2
  module Components
3
- class Container < Configurable
3
+ class Container < Base
4
+ include Capabilities::Configurable
5
+ include Capabilities::Hideable
4
6
 
5
7
  abstract_component
6
8
 
@@ -1,3 +1,3 @@
1
1
  module ExpressTemplates
2
- VERSION = "0.11.11"
2
+ VERSION = "0.11.13"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: express_templates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.11
4
+ version: 0.11.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Talcott Smith
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-03-17 00:00:00.000000000 Z
12
+ date: 2017-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -169,6 +169,8 @@ files:
169
169
  - lib/express_templates/components.rb
170
170
  - lib/express_templates/components/all.rb
171
171
  - lib/express_templates/components/base.rb
172
+ - lib/express_templates/components/capabilities/configurable.rb
173
+ - lib/express_templates/components/capabilities/hideable.rb
172
174
  - lib/express_templates/components/capabilities/resourceful.rb
173
175
  - lib/express_templates/components/configurable.rb
174
176
  - lib/express_templates/components/container.rb
@@ -259,7 +261,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
259
261
  version: '0'
260
262
  requirements: []
261
263
  rubyforge_project:
262
- rubygems_version: 2.5.1
264
+ rubygems_version: 2.4.7
263
265
  signing_key:
264
266
  specification_version: 4
265
267
  summary: Reusable view components
@@ -315,4 +317,3 @@ test_files:
315
317
  - test/express_templates_test.rb
316
318
  - test/handler_test.rb
317
319
  - test/test_helper.rb
318
- has_rdoc: