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.
33 34 35 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 |
# File 'lib/recordx.rb', line 33 def initialize(x=nil, callerx=nil, id=nil, created=nil, last_modified=nil, debug: false) @debug = debug puts 'x: ' + x.inspect 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 else x end @callerx, @id, @created, @last_modified = callerx, id, \ created, last_modified @h = RXHash.new(self).merge h h.each {|name,val| attr_accessor2(name.to_s, val) } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *raw_args) ⇒ Object (private)
135 136 137 138 139 140 141 |
# File 'lib/recordx.rb', line 135 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.
12 13 14 |
# File 'lib/recordx.rb', line 12 def created @created end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
12 13 14 |
# File 'lib/recordx.rb', line 12 def id @id end |
#last_modified ⇒ Object (readonly)
Returns the value of attribute last_modified.
12 13 14 |
# File 'lib/recordx.rb', line 12 def last_modified @last_modified end |
Instance Method Details
#[](k) ⇒ Object
63 |
# File 'lib/recordx.rb', line 63 def [](k) @h[k] end |
#[]=(k, v) ⇒ Object
64 |
# File 'lib/recordx.rb', line 64 def []=(k,v) @h[k] = v end |
#build(xml, h) ⇒ Object
120 121 122 |
# File 'lib/recordx.rb', line 120 def build(xml, h) h.each_pair {|key, value| xml.send(key.to_sym, value) } end |
#delete ⇒ Object
70 71 72 |
# File 'lib/recordx.rb', line 70 def delete() @callerx.delete @id end |
#each(&blk) ⇒ Object Also known as: each_pair
67 |
# File 'lib/recordx.rb', line 67 def each(&blk) @h.each(&blk) end |
#h ⇒ Object
74 75 76 |
# File 'lib/recordx.rb', line 74 def h() @h end |
#inspect ⇒ Object
78 79 80 |
# File 'lib/recordx.rb', line 78 def inspect() "#<RecordX:%s @h=%s>" % [self.object_id, @h] end |
#keys ⇒ Object
65 |
# File 'lib/recordx.rb', line 65 def keys() @h.keys end |
#to_h ⇒ Object
82 83 84 |
# File 'lib/recordx.rb', line 82 def to_h() @h.clone end |
#to_html(xslt: '') ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/recordx.rb', line 86 def to_html(xslt: '') # This method is expected to be used within Dynarex kvx = self.to_kvx xsl_buffer = RXFHelper.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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/recordx.rb', line 99 def to_kvx() puts 'inside to_kvx' if @debug kvx = Kvx.new(@h.to_h) 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
114 115 116 |
# File 'lib/recordx.rb', line 114 def to_s() self.to_kvx.to_s end |
#to_xml ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/recordx.rb', line 118 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
129 130 131 |
# File 'lib/recordx.rb', line 129 def update(h) h.each {|name,value| self.method((name.to_s + '=').to_sym).call(value) } end |
#values ⇒ Object
66 |
# File 'lib/recordx.rb', line 66 def values() @h.values end |