Class: Kvx
Overview
Kvx does the following:
-
h -> xml
-
xml -> h
-
s -> h
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#summary ⇒ Object
Returns the value of attribute summary.
-
#to_h(flatten: false) ⇒ Object
readonly
flattening is helpful when passing the Hash object to RecordX as a new record.
Instance Method Summary collapse
- #import(s) ⇒ Object
-
#initialize(x = nil, summary: {}, body: {}, attributes: {}, debug: false) ⇒ Kvx
constructor
A new instance of Kvx.
- #item ⇒ Object (also: #body)
- #save(filename) ⇒ Object
- #to_doc ⇒ Object
- #to_s ⇒ Object
- #to_xml(options = {pretty: true}) ⇒ Object
- #to_xslt ⇒ Object
-
#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.
Constructor Details
#initialize(x = nil, summary: {}, body: {}, 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 63 64 |
# File 'lib/kvx.rb', line 37 def initialize(x=nil, summary: {}, body: {}, attributes: {}, debug: false) @header = attributes.any? @identifier = 'kvx' @summary, @body = summary, body @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 end methodize(@body) end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
34 35 36 |
# File 'lib/kvx.rb', line 34 def attributes @attributes end |
#summary ⇒ Object
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
84 85 86 |
# File 'lib/kvx.rb', line 84 def to_h @to_h end |
Instance Method Details
#import(s) ⇒ Object
66 67 68 69 |
# File 'lib/kvx.rb', line 66 def import(s) @body = parse_string(s) methodize(@body) end |
#item ⇒ Object Also known as: body
71 72 73 |
# File 'lib/kvx.rb', line 71 def item() @body end |
#save(filename) ⇒ Object
77 78 79 |
# File 'lib/kvx.rb', line 77 def save(filename) FileX.write filename, self.to_s end |
#to_doc ⇒ Object
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 139 140 |
# File 'lib/kvx.rb', line 102 def to_doc() a = if @summary.empty? then [self.class.to_s.downcase, @attributes, '', *make_xml(@body)] else summary = make_xml(@summary) = summary.assoc(:tags) if then = .pop .push *.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_s ⇒ Object
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 181 182 |
# File 'lib/kvx.rb', line 142 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
184 185 186 187 188 189 |
# File 'lib/kvx.rb', line 184 def to_xml(={pretty: true}) doc = self.to_doc doc.xml() end |
#to_xslt ⇒ Object
191 192 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 |
# File 'lib/kvx.rb', line 191 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
250 251 252 |
# File 'lib/kvx.rb', line 250 def update(id=nil, hpair={}) @body.merge! hpair end |