Class: Kvx

Inherits:
Object
  • Object
show all
Includes:
RXFHelperModule
Defined in:
lib/kvx.rb

Overview

Kvx does the following:

  • h -> xml

  • xml -> h

  • s -> h

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = nil, attributes: {}, debug: false) ⇒ Kvx

Returns a new instance of Kvx.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kvx.rb', line 37

def initialize(x=nil, attributes: {}, debug: false)

  @header = false
  @identifier = 'kvx'
  @summary = {}
  @ignore_blank_lines ||= false
  
  @attributes, @debug = attributes, debug
  
  h = {
    hash: :passthru, 
    :'rexle::element' => :xml_to_h, 
    string: :parse_string,
    rexle: :doc_to_h,
    :"rexle::element::value" => :parse_string
  }
  
  if x then
    
    sym = h[x.class.to_s.downcase.to_sym]
    puts 'sym: ' + sym.inspect if @debug
    @body = method(sym).call x
    methodize(@body)
  end

end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



34
35
36
# File 'lib/kvx.rb', line 34

def attributes
  @attributes
end

#summaryObject

Returns the value of attribute summary.



34
35
36
# File 'lib/kvx.rb', line 34

def summary
  @summary
end

#to_hObject (readonly)

Returns the value of attribute to_h.



35
36
37
# File 'lib/kvx.rb', line 35

def to_h
  @to_h
end

Instance Method Details

#import(s) ⇒ Object



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

def import(s)
  @body = parse_string(s)
  methodize(@body)
end

#itemObject Also known as: body



69
70
71
# File 'lib/kvx.rb', line 69

def item()
  @body
end

#save(filename) ⇒ Object



75
76
77
# File 'lib/kvx.rb', line 75

def save(filename)
  FileX.write filename, self.to_s
end

#to_docObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kvx.rb', line 89

def to_doc()
  
  a = if @summary.empty? then
    
    [self.class.to_s.downcase, @attributes, '', *make_xml(@body)]      
    
  else
    
    summary = make_xml(@summary)
    
    tags_found = summary.assoc(:tags)
    
    if tags_found then
      tags = tags_found.pop
      tags_found.push *tags.split.map {|x| [:tag,{},x]} 
    end

    summary = [:summary, {}, *summary]
    body = [:body, {}, *make_xml(@body)]
    [self.class.to_s.downcase, @attributes, '', summary, body]      
    
  end    
  
  doc = Rexle.new a
  doc.instructions = @instructions
  doc
  
end

#to_sObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/kvx.rb', line 118

def to_s()
  
  header = ''
  
  if @header or (@summary and @summary.any?) then
    
    attr = @attributes ? ' ' + @attributes\
                                     .map {|x| "%s='%s'" % x }.join(' ') : ''
    header = '<?' + @identifier
    header += attr
    header += "?>\n"
    header += scan_to_s @summary
    header += "\n----------------------------------\n\n"
  end
  
  header + scan_to_s(@body)

end

#to_xml(options = {pretty: true}) ⇒ Object



137
138
139
140
141
142
# File 'lib/kvx.rb', line 137

def to_xml(options={pretty: true})
        
  doc = self.to_doc
  doc.xml(options)

end