datagnan 1.0.3 → 1.1.0

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 (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/datagnan.rb +74 -10
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f425736e18d39486f8e1fc680fd8450bde7a4d1a
4
- data.tar.gz: 46b8bdba14ff01e2dd64cedf69828fe7acb8458a
3
+ metadata.gz: eb67371aa0b5e42c13faf09b9b89192bd758af17
4
+ data.tar.gz: 2f96688ca5ccb0b35f8ac2839431dab3b9b4bc30
5
5
  SHA512:
6
- metadata.gz: 96bd8819def16e59acc2223cdb23bdea2bf16cd9089238f1ddd3b9d6142fd06082e56fe526acab30f0a9f470d2bacdcdfeedf4b0c8f87e2a5dd813edb23167c1
7
- data.tar.gz: 01eaa06ae0cd544c99123af7757e93e0ddea66dab4b7f97ed119e60e23fbf2d4ba5c5fe35c96b3fa4004852d923642cafeab19971495e81eafc960ec481a9765
6
+ metadata.gz: a3805ac064b852bf470362406452b58deb4eefd1f1684d77c6bb6d4127beea75f1a24c30bf755ebbb6e1e745abda42c1983a76e6c47705b3a81010e7ac2afc77
7
+ data.tar.gz: 1bc54f6aab35831a7f21efe81ad1ba867900889e71742bca7c5af11610d6d5d29ef30e80aade05920ab5e52bed37a1ef2cff3fbf48cf216322775c00e856f6fa
@@ -6,8 +6,16 @@ class Datagnan
6
6
  attr_accessor :doc, :vars
7
7
 
8
8
  ## Constructor
9
+ # @param [String] file_path
10
+ # path to .html-template file
11
+ # @param [Hash] options
12
+ # options for parsing and filling the template
13
+ # @option options [String] :attrs_sep (";") Separate string for 'data-attrs' attribute
14
+ # @option options [Object] :scope (self) Scope for variables
15
+ # @option options [Hash] :locals (locals || {}) Local variables for filling the template
16
+ # @param [Hash] locals
17
+ # local variables for filling the template
9
18
  def initialize(file_path, options = {}, locals = {})
10
- ## TODO: return fragment, not full html with auto-tags
11
19
  @doc = Oga.parse_html(File.read(file_path))
12
20
  ## debug
13
21
  # puts "-- @doc = #{@doc}"
@@ -19,19 +27,40 @@ class Datagnan
19
27
  # puts "-- Datagnan.new ( @vars = #{@vars} )"
20
28
  end
21
29
 
22
- ## aka File.open
30
+ ## Create Datagnan instance and return this.
31
+ ## Can receive implicity block for 'yield.
32
+ ## Like File.open(file_path)
33
+ # @param [String] file_path
34
+ # path to .html-template file
35
+ # @param [Hash] options
36
+ # options for parsing and filling the template
37
+ # @option options [String] :attrs_sep (";") Separate string for 'data-attrs' attribute
38
+ # @option options [Object] :scope (self) Scope for variables
39
+ # @option options [Hash] :locals (locals || {}) Local variables for filling the template
40
+ # @param [Hash] locals
41
+ # local variables for filling the template
23
42
  def self.open(file_path, options = {}, locals = {})
24
43
  obj = Datagnan.new(file_path, options, locals).write
25
44
  yield obj if block_given?
26
45
  return obj
27
46
  end
28
47
 
29
- ## aka File.read(file_path)
48
+ ## Create Datagnan instance and return the filling with variables HTML-template as String.
49
+ ## Like File.read(file_path)
50
+ # @param [String] file_path
51
+ # path to .html-template file
52
+ # @param [Hash] options
53
+ # options for parsing and filling the template
54
+ # @option options [String] :attrs_sep (";") Separate string for 'data-attrs' attribute
55
+ # @option options [Object] :scope (self) Scope for variables
56
+ # @option options [Hash] :locals (locals || {}) Local variables for filling the template
57
+ # @param [Hash] locals
58
+ # local variables for filling the template
30
59
  def self.read(file_path, options = {}, locals = {})
31
60
  Datagnan.new(file_path, options, locals).write.read
32
61
  end
33
62
 
34
- ## aka File.read
63
+ ## Return HTML-String. As File.new.read
35
64
  def read
36
65
  ## debug
37
66
  # puts "-- Datagnan.read ( @doc = #{@doc} )"
@@ -39,6 +68,11 @@ class Datagnan
39
68
  end
40
69
 
41
70
  ## Parse and replace
71
+ ## Like File.new.write
72
+ # @param [Hash] vars
73
+ # variables for filling the template
74
+ # @param [Oga::XML::Document] root
75
+ # HTML-element for fill it
42
76
  def write(vars = {}, root = nil)
43
77
  ## debug
44
78
  # puts "Datagnan.write ( vars = #{vars} )"
@@ -56,6 +90,7 @@ class Datagnan
56
90
  # puts "-- Datagnan.write after each ( root = #{root.to_xml} )"
57
91
  ## replace data-when
58
92
  data_when(vars, root)
93
+ data_when_not(vars, root)
59
94
  ## debug
60
95
  # puts "-- Datagnan.write after when ( vars = #{vars} )"
61
96
  # puts "-- Datagnan.write after when ( root = #{root.to_xml} )"
@@ -88,7 +123,7 @@ private
88
123
  # puts "-- Datagnan.find_value split ( @scope.respond_to?(key) = #{@scope.respond_to?(key)} )"
89
124
  # puts "-- Datagnan.find_value split ( @scope.send(key) = #{@scope.send(key)} )" if @scope.respond_to?(key)
90
125
  var = ( var.is_a?(Hash) ?
91
- ( var[key].nil? ?
126
+ ( var[key].nil? ?
92
127
  ( @scope.respond_to?(key) ?
93
128
  @scope.send(key) :
94
129
  nil
@@ -168,7 +203,8 @@ private
168
203
  unless var.nil?
169
204
  # --------
170
205
  node.unset('data-each')
171
- var.each do |item|
206
+ var.each do |key, item|
207
+ item ||= key
172
208
  # puts "-- Datagnan.data_each each ( item = #{item} )"
173
209
  # puts "-- Datagnan.data_each each ( item.inspect = #{item.inspect} )"
174
210
  new_node = Oga.parse_html(node.to_xml)
@@ -200,22 +236,50 @@ private
200
236
  root.css('*[data-when]').each do |node|
201
237
 
202
238
  var = find_value(node.get('data-when'), vars)
239
+ # puts "Datagnan.data_when each ( var = #{var} )"
203
240
 
204
- unless var.nil?
205
- var ? node.unset('data-when') : node.remove
206
- end
241
+ ## allow for nil
242
+ var ? node.unset('data-when') : node.remove
207
243
  ## TODO: if var not exist remember it (maybe return to model)
208
244
  end
209
245
  ## debug
210
246
  # puts "Datagnan.data_when end ( root = #{root} )"
211
247
  end
212
248
 
249
+ ## display if false
250
+ # @since 1.1.0
251
+ def data_when_not(vars, root)
252
+ ## debug
253
+ # puts "Datagnan.data_when_not ( vars = #{vars} )"
254
+ # puts "Datagnan.data_when_not ( root = #{root} )"
255
+ root.css('*[data-when-not]').each do |node|
256
+
257
+ var = find_value(node.get('data-when-not'), vars)
258
+
259
+ ## allow for nil
260
+ var ? node.remove : node.unset('data-when')
261
+ ## TODO: if var not exist remember it (maybe return to model)
262
+ end
263
+ ## debug
264
+ # puts "Datagnan.data_when_not end ( root = #{root} )"
265
+ end
266
+
213
267
  end
214
268
 
269
+ ## Helper function (for Sinatra, primarily)
270
+ # @param [String] file_path
271
+ # path to .html-template file
272
+ # @param [Hash] options
273
+ # options for parsing and filling the template
274
+ # @option options [Object] :scope (self) Scope for variables
275
+ # @option options [String] :views (scope.settings.views || "./views") Path to views directory
276
+ # @option options [Hash] :locals (locals || {}) Local variables for filling the template
277
+ # @param [Hash] locals
278
+ # local variables for filling the template
215
279
  def datagnan(template_file, options = {}, locals = {})
216
280
  ## default options
217
- views = options.delete(:views) || "./views"
218
281
  scope = options.delete(:scope) || self
282
+ views = File.realpath(options.delete(:views) || scope.settings.views || "./views")
219
283
  locals = options.delete(:locals) || locals || {}
220
284
 
221
285
  html = Datagnan.read(views+'/'+template_file+'.html', :scope => scope, :locals => locals)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datagnan
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Popov
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-04-28 00:00:00.000000000 Z
12
+ date: 2015-04-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oga