Class: Datagnan

Inherits:
Object
  • Object
show all
Defined in:
lib/datagnan.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, options = {}, local_vars = {}) ⇒ Datagnan

Constructor

Parameters:

  • file_path (String)

    path to .html-template file

  • options (Hash) (defaults to: {})

    options for parsing and filling the template

  • locals (Hash)

    local variables for filling the template



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/datagnan.rb', line 15

def initialize(file_path, options = {}, local_vars = {})
	@doc = Oga.parse_html File.read(file_path)
	## debug
	# puts "-- @doc = #{@doc}"
	@options = {}
	@options[:attrs_sep] = options[:attrs_sep] || ";"
	@options[:path_filters] = options[:path_filters] || {}
	@scope = options[:scope] || self
	@vars = (options[:local_vars] || {}).merge! local_vars
	@vars.merge! Hash[@scope.instance_variables.map { |name| [name.to_s, @scope.instance_variable_get(name)] } ] unless @scope.nil?
	## debug
	# puts "-- Datagnan.new ( @vars = #{@vars} )"
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



6
7
8
# File 'lib/datagnan.rb', line 6

def doc
  @doc
end

#varsObject

Returns the value of attribute vars.



6
7
8
# File 'lib/datagnan.rb', line 6

def vars
  @vars
end

Class Method Details

.open(file_path, options = {}, local_vars = {}) {|obj| ... } ⇒ Object

Create Datagnan instance and return this. Can receive implicity block for ‘yield. Like File.open(file_path)

Parameters:

  • file_path (String)

    path to .html-template file

  • options (Hash) (defaults to: {})

    options for parsing and filling the template

  • locals (Hash)

    local variables for filling the template

Yields:

  • (obj)


38
39
40
41
42
# File 'lib/datagnan.rb', line 38

def self.open(file_path, options = {}, local_vars = {})
	obj = Datagnan.new(file_path, options, local_vars).write
	yield obj if block_given?
	return obj
end

.read(file_path, options = {}, local_vars = {}) ⇒ Object

Create Datagnan instance and return the filling with variables HTML-template as String. Like File.read(file_path)

Parameters:

  • file_path (String)

    path to .html-template file

  • options (Hash) (defaults to: {})

    options for parsing and filling the template

  • locals (Hash)

    local variables for filling the template



52
53
54
# File 'lib/datagnan.rb', line 52

def self.read(file_path, options = {}, local_vars = {})
	Datagnan.new(file_path, options, local_vars).write.read
end

Instance Method Details

#readObject

Return HTML-String. As File.new.read



57
58
59
60
61
# File 'lib/datagnan.rb', line 57

def read
	## debug
	# puts "-- Datagnan.read ( @doc = #{@doc} )"
	@doc.to_xml
end

#write(vars = {}, root = nil) ⇒ Object

Parse and replace Like File.new.write

Parameters:

  • vars (Hash) (defaults to: {})

    variables for filling the template

  • root (Oga::XML::Document) (defaults to: nil)

    HTML-element for fill it



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/datagnan.rb', line 69

def write(vars = {}, root = nil)
	## debug
	# puts "Datagnan.write ( vars = #{vars} )"
	# puts "Datagnan.write ( @doc = #{@doc} )"
	## use global if empty
	vars = @vars if vars.is_a?(Hash) && vars.empty?
	root = @doc if root.nil?
	## debug
	# puts "-- Datagnan.write ( vars = #{vars.inspect} )"
	# puts "-- Datagnan.write ( root = #{root.to_xml} )"
	## replace data-each
	data_each(vars, root)
	## debug
	# puts "-- Datagnan.write after each ( vars = #{vars} )"
	# puts "-- Datagnan.write after each ( root = #{root.to_xml} )"
	## replace data-when
	data_when(vars, root)
	data_when_not(vars, root)
	## debug
	# puts "-- Datagnan.write after when ( vars = #{vars} )"
	# puts "-- Datagnan.write after when ( root = #{root.to_xml} )"
	## replace data-var
	data_var(vars, root)
	## debug
	# puts "Datagnan.write after vars ( vars = #{vars} )"
	# puts "Datagnan.write after vars ( root = #{root.to_xml} )"
	## replace data-attrs
	data_attrs(vars, root)
	data_attr(vars, root)
	## debug
	# puts "Datagnan.write after attrs ( vars = #{vars} )"
	# puts "Datagnan.write after attrs ( root = #{root.to_xml} )"
	## result
	return self
end