Class: RecordX
- Inherits:
-
Object
- Object
- RecordX
- Defined in:
- lib/recordx.rb
Defined Under Namespace
Classes: RXHash
Instance Attribute Summary collapse
-
#created ⇒ Object
readonly
Returns the value of attribute created.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#last_modified ⇒ Object
readonly
Returns the value of attribute last_modified.
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #build(xml, h) ⇒ Object
- #delete ⇒ Object
- #each(&blk) ⇒ Object (also: #each_pair)
- #h ⇒ Object
-
#initialize(x = nil, callerx = nil, id = nil, created = nil, last_modified = nil, debug: false) ⇒ RecordX
constructor
A new instance of RecordX.
- #inspect ⇒ Object
- #keys ⇒ Object
- #to_h ⇒ Object
- #to_html(xslt: '') ⇒ Object
- #to_kvx ⇒ Object
- #to_s ⇒ Object
- #to_xml ⇒ Object
- #update(h) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(x = nil, callerx = nil, id = nil, created = nil, last_modified = nil, debug: false) ⇒ RecordX
Returns a new instance of RecordX.
36 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 67 68 |
# File 'lib/recordx.rb', line 36 def initialize(x=nil, callerx=nil, id=nil, created=nil, last_modified=nil, debug: false) @debug = debug puts ('x: ' + x.inspect).debug if @debug h = if x.is_a? Hash then x x elsif x.is_a? Array then x.inject({}) do |r,y| val = y.text.is_a?(String) ? y.text.unescape : '' r.merge(y.name.to_sym => val) end elsif x.respond_to? :to_h x.to_h else x end @id, @created, @last_modified = id, created, last_modified @h = RXHash.new(self).merge h h.each {|name,val| attr_accessor2(name.to_s, val) } @callerx = callerx end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *raw_args) ⇒ Object (private)
142 143 144 145 146 147 148 |
# File 'lib/recordx.rb', line 142 def method_missing(method_name, *raw_args) arg = raw_args.length > 0 ? raw_args.first : nil attr_accessor2(method_name[/\w+/], arg) arg ? self.send(method_name, arg) : self.send(method_name) end |
Instance Attribute Details
#created ⇒ Object (readonly)
Returns the value of attribute created.
15 16 17 |
# File 'lib/recordx.rb', line 15 def created @created end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
15 16 17 |
# File 'lib/recordx.rb', line 15 def id @id end |
#last_modified ⇒ Object (readonly)
Returns the value of attribute last_modified.
15 16 17 |
# File 'lib/recordx.rb', line 15 def last_modified @last_modified end |
Instance Method Details
#[](k) ⇒ Object
70 |
# File 'lib/recordx.rb', line 70 def [](k) @h[k] end |
#[]=(k, v) ⇒ Object
71 |
# File 'lib/recordx.rb', line 71 def []=(k,v) @h[k] = v end |
#build(xml, h) ⇒ Object
127 128 129 |
# File 'lib/recordx.rb', line 127 def build(xml, h) h.each_pair {|key, value| xml.send(key.to_sym, value) } end |
#delete ⇒ Object
77 78 79 |
# File 'lib/recordx.rb', line 77 def delete() @callerx.delete @id end |
#each(&blk) ⇒ Object Also known as: each_pair
74 |
# File 'lib/recordx.rb', line 74 def each(&blk) @h.each(&blk) end |
#h ⇒ Object
81 82 83 |
# File 'lib/recordx.rb', line 81 def h() @h end |
#inspect ⇒ Object
85 86 87 |
# File 'lib/recordx.rb', line 85 def inspect() "#<RecordX:%s @h=%s>" % [self.object_id, @h] end |
#keys ⇒ Object
72 |
# File 'lib/recordx.rb', line 72 def keys() @h.keys end |
#to_h ⇒ Object
89 90 91 |
# File 'lib/recordx.rb', line 89 def to_h() @h.clone end |
#to_html(xslt: '') ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/recordx.rb', line 93 def to_html(xslt: '') # This method is expected to be used within Dynarex kvx = self.to_kvx xsl_buffer = RXFReader.read(xslt).first #jr100316 xslt = Nokogiri::XSLT(xsl_buffer) #jr100316 xslt.transform(Nokogiri::XML(kvx.to_xml)).to_s Rexslt.new(xsl_buffer, kvx.to_xml).to_s end |
#to_kvx ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/recordx.rb', line 106 def to_kvx() puts 'inside to_kvx'.info if @debug kvx = Kvx.new(@h.to_h, debug: @debug) if @callerx and @callerx.summary[:recordx_type] then summary_fields = @callerx.summary.keys - [:recordx_type, \ :format_mask, :schema, :default_key] summary_fields.each {|field| kvx.summary[field] = @callerx.summary[field] } end kvx end |
#to_s ⇒ Object
121 122 123 |
# File 'lib/recordx.rb', line 121 def to_s() self.to_kvx.to_s end |
#to_xml ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/recordx.rb', line 125 def to_xml() def build(xml, h) h.each_pair {|key, value| xml.send(key.to_sym, value) } end xml = RexleBuilder.new Rexle.new(xml.root { build xml, h }).xml pretty: true end |
#update(h) ⇒ Object
136 137 138 |
# File 'lib/recordx.rb', line 136 def update(h) h.each {|name,value| self.method((name.to_s + '=').to_sym).call(value) } end |
#values ⇒ Object
73 |
# File 'lib/recordx.rb', line 73 def values() @h.values end |