Module: NokoHacks

Extended by:
ActiveSupport::Concern
Included in:
XmlToForm::XmlHandler, XmlToForm::XmlHandlersController
Defined in:
lib/noko_hacks.rb

Instance Method Summary collapse

Instance Method Details

#build_ds(node, node_array, attr_accessor_hash) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/noko_hacks.rb', line 54

def build_ds(node, node_array,  attr_accessor_hash)
  case [node.attributes.empty? , node.content.empty? ,node.children.empty?]

    when [false, true, true] then node_array.push({(node.name + encode_node_path(node)).gsub('-','_') => node.attributes.transform_keys{|key| (key + encode_node_path(node)).gsub('-','_')} }) && node.attributes.each {|key,value|
      attr_accessor_hash.merge!((key + encode_node_path(node)).gsub('-','_') => value.value)}

    when [true, false, false] then node_array.push({(node.name + encode_node_path(node)).gsub('-','_') => node.content, :child_node => node.child_node_ds(attr_accessor_hash) }) && attr_accessor_hash.merge!((node.name + encode_node_path(node)).gsub('-','_') => node.content)

    when [false, true, false] then node_array.push({(node.name + encode_node_path(node)).gsub('-','_')=> node.attributes.transform_keys{|key| (key + encode_node_path(node)).gsub('-','_')}, :child_node => node.child_node_ds(attr_accessor_hash) }) && node.attributes.each {|key,value| attr_accessor_hash.merge!((key + encode_node_path(node)).gsub('-','_') => value.value)}

    when [false, false, false] then node_array.push({(node.name + encode_node_path(node)).gsub('-','_') => (node.attributes.transform_keys{|key| (key + encode_node_path(node)).gsub('-','_')}).merge!((node.name + encode_node_path(node)).gsub('-','_') => node.content) }) && node.attributes.each {|key,value| attr_accessor_hash.merge!((key + encode_node_path(node)).gsub('-','_') => value.value)} && attr_accessor_hash.merge!((node.name + encode_node_path(node)).gsub('-','_') => node.content)
  end
end

#child_node_ds(attr_accessor_hash) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/noko_hacks.rb', line 68

def child_node_ds(attr_accessor_hash)
  _temp=[]
  self.children.each do |node|
    case [node.attributes.empty? , node.content.empty? ,node.children.empty?]

      when [false, true, true] then  _temp.push({(node.name + encode_node_path(node)).gsub('-','_') => node.attributes.transform_keys{|key| (key + encode_node_path(node)).gsub('-','_')}}) && node.attributes.each {|key,value| attr_accessor_hash.merge!((key + encode_node_path(node)).gsub('-','_') => value.value)}

      when [true, false, false] then  _temp.push({(node.parent.name + encode_node_path(node)).gsub('-','_') => node.content, :child_node => node.child_node_ds(attr_accessor_hash) }) && attr_accessor_hash.merge!((node.parent.name + encode_node_path(node)).gsub('-','_') => node.content)

      when [false, true, false] then   _temp.push({(node.name + encode_node_path(node)).gsub('-','_')  => node.attributes.transform_keys{|key| (key + encode_node_path(node)).gsub('-','_')}, :child_node => node.child_node_ds(attr_accessor_hash)})&& node.attributes.each {|key,value| attr_accessor_hash.merge!((key + encode_node_path(node)).gsub('-','_') => value.value)}

      when [false, false, false] then node_array.push({(node.name + encode_node_path(node)).gsub('-','_') => (node.attributes.transform_keys{|key| (key + encode_node_path(node)).gsub('-','_')}).merge!((node.name + encode_node_path(node)).gsub('-','_') => node.content)}) && node.attributes.each {|key,value| attr_accessor_hash.merge!((key + encode_node_path(node)).gsub('-','_') => value.value)} && attr_accessor_hash.merge!((node.name + encode_node_path(node)).gsub('-','_') => node.content)
    end
  end
  return _temp
end

#decode_node_path(node) ⇒ Object



36
37
38
# File 'lib/noko_hacks.rb', line 36

def decode_node_path(node)
  Base64.decode64(node.split('_').last)
end

#encode_node_path(node) ⇒ Object



50
51
52
# File 'lib/noko_hacks.rb', line 50

def encode_node_path(node)
  ('_' + Base64.urlsafe_encode64((Nokogiri::CSS.xpath_for node.css_path).first)).gsub('=','')
end

#get_node_attribute_name(key) ⇒ Object



40
41
42
# File 'lib/noko_hacks.rb', line 40

def get_node_attribute_name(key)
  (_split_key = key.split('_')).size > 2 ? _split_key.take(_split_key.size - 1).join('-').sub('@','') : _split_key.first.sub('@','')
end

#iterate_child_nodes(node_array, attr_accessor_hash) ⇒ Object



44
45
46
47
48
# File 'lib/noko_hacks.rb', line 44

def iterate_child_nodes(node_array, attr_accessor_hash)
  self.children.each do |node|
    build_ds(node, node_array, attr_accessor_hash)
  end
end

#remove_blank_nodeObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/noko_hacks.rb', line 7

def remove_blank_node
  self.children.each do |node|
    if node.blank?
      node.remove
    end
    if node.children
      node.remove_blank_node
    end
  end
end

#update_xml(node, key, value) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/noko_hacks.rb', line 26

def update_xml(node, key, value)
  case [node.content.empty?, node.attributes.empty?]
    when [false, true] then node.content = value

    when [false, false] then node.content = value && update_xml_attributes(node, key, value)

    when [true, false] then update_xml_attributes(node, key, value)
  end
end

#update_xml_attributes(node, key, value) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/noko_hacks.rb', line 18

def update_xml_attributes(node, key, value)
  node.attributes.each do |k, v|
    if k == get_node_attribute_name(key)
      v.value = value
    end
  end
end