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 = attributes.any?
  @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_h(flatten: false) ⇒ Object (readonly)

flattening is helpful when passing the Hash object to RecordX as a new record



82
83
84
# File 'lib/kvx.rb', line 82

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/kvx.rb', line 100

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]
    
    # -- use the nested description Hash object if there are multiple lines
    h = {}
    
    @body.each do |key, value|
            
      h[key] = value.is_a?(String) ? value : value[:description]
      
    end
    
    body = [:body, {}, *make_xml(h)]
    [self.class.to_s.downcase, @attributes, '', summary, body]      
    
  end    
  
  puts 'a: ' + a.inspect if @debug
  doc = Rexle.new a
  doc.instructions = @instructions || []
  doc
  
end

#to_sObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/kvx.rb', line 140

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"
    
    if @summary and @summary.any? then
      header += scan_to_s @summary
      header += "\n----------------------------------\n\n"
    end
    
  end

  # -- use the nested description Hash object if there are multiple lines
  h = {}
  
  @body.each do |key, value|
          
    h[key] = if value.is_a?(String) then
    
      if value.lines.length < 2 then
        value 
      else
        "\n" + value.lines.map {|x| '  ' + x }.join          
      end
      
    else
        "\n" + value[:description].lines.map {|x| '  ' + x }.join
    end
    
  end    
  
  header + scan_to_s(h)

end

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



182
183
184
185
186
187
# File 'lib/kvx.rb', line 182

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

end

#update(id = nil, hpair = {}) ⇒ Object

used by RecordX to update a KVX record id is unnecssary because there is only 1 record mapped to RecordX



192
193
194
# File 'lib/kvx.rb', line 192

def update(id=nil, hpair={})
  @body.merge! hpair
end