Class: Kvx

Inherits:
Object
  • Object
show all
Includes:
RXFReadWriteModule
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, summary: {}, body: {}, attributes: {}, comment_char: nil, 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
63
64
65
66
# File 'lib/kvx.rb', line 37

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

  @header = attributes.any?
  @identifier = 'kvx'
  @summary, @body = summary, body
  @ignore_blank_lines ||= false
  
  @attributes, @debug = attributes, debug
  @comment_char = comment_char
  
  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

  end

  methodize(@body)

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



86
87
88
# File 'lib/kvx.rb', line 86

def to_h
  @to_h
end

Instance Method Details

#import(s) ⇒ Object



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

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

#itemObject Also known as: body



73
74
75
# File 'lib/kvx.rb', line 73

def item()
  @body
end

#save(filename) ⇒ Object



79
80
81
# File 'lib/kvx.rb', line 79

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

#to_docObject



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
139
140
141
142
# File 'lib/kvx.rb', line 104

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



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
181
182
183
184
# File 'lib/kvx.rb', line 144

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



186
187
188
189
190
191
# File 'lib/kvx.rb', line 186

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

end

#to_xsltObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/kvx.rb', line 193

def to_xslt()

  summary = self.summary.keys.map do |key|
    "     <xsl:element name='#{key}'><xsl:value-of select='#{key}' /></xsl:element>"
  end.join("\n")

  body = self.body.keys.map do |key|
    "     <xsl:element name='#{key}'><xsl:value-of select='#{key}' /></xsl:element>"
  end.join("\n")

s = "
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>

<xsl:template match='kvx'>

  <xsl:element name='kvx'>

      <xsl:attribute name='created'>
      <xsl:value-of select='@created'/>
    </xsl:attribute>
    <xsl:attribute name='last_modified'>
      <xsl:value-of select='@last_modified'/>
    </xsl:attribute>

    <xsl:apply-templates select='summary' />
    <xsl:apply-templates select='body' />

  </xsl:element>

</xsl:template>

<xsl:template match='summary'>

  <xsl:element name='summary'>

#{summary}

  </xsl:element>

</xsl:template>

<xsl:template match='body'>

  <xsl:element name='body'>

#{body}

  </xsl:element>

</xsl:template>

</xsl:stylesheet>
"

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



252
253
254
# File 'lib/kvx.rb', line 252

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