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 = {}, locals = {}) ⇒ Datagnan

Constructor

Parameters:

  • file_path (String)

    path to .html-template file

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

    options for parsing and filling the template @option options [String] :attrs_sep (“;”) Separate string for ‘data-attrs’ attribute @option options [Object] :scope (self) Scope for variables @option options [Hash] :locals (locals || {}) Local variables for filling the template

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

    local variables for filling the template



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

def initialize(file_path, options = {}, locals = {})
	@doc = Oga.parse_html(File.read(file_path))
	## debug
	# puts "-- @doc = #{@doc}"
	@attrs_sep = options.delete(:attrs_sep) || ";"
	@scope = options.delete(:scope) || self
	@vars = options.delete(:locals) || locals || {}
	@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 = {}, locals = {}) {|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 @option options [String] :attrs_sep (“;”) Separate string for ‘data-attrs’ attribute @option options [Object] :scope (self) Scope for variables @option options [Hash] :locals (locals || {}) Local variables for filling the template

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

    local variables for filling the template

Yields:

  • (obj)


42
43
44
45
46
# File 'lib/datagnan.rb', line 42

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

.read(file_path, options = {}, locals = {}) ⇒ 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 @option options [String] :attrs_sep (“;”) Separate string for ‘data-attrs’ attribute @option options [Object] :scope (self) Scope for variables @option options [Hash] :locals (locals || {}) Local variables for filling the template

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

    local variables for filling the template



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

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

Instance Method Details

#readObject

Return HTML-String. As File.new.read



64
65
66
67
68
# File 'lib/datagnan.rb', line 64

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



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
104
105
106
107
108
109
# File 'lib/datagnan.rb', line 76

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)
	## debug
	# puts "Datagnan.write after attrs ( vars = #{vars} )"
	# puts "Datagnan.write after attrs ( root = #{root.to_xml} )"
	## result
	return self
end