class RecordX

Attributes

created[R]
id[R]
last_modified[R]

Public Class Methods

new(x=nil, callerx=nil, id=nil, created=nil, last_modified=nil, debug: false) click to toggle source
# 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

Public Instance Methods

[](k) click to toggle source
# File lib/recordx.rb, line 70
def [](k)      @h[k]         end
[]=(k,v) click to toggle source
# File lib/recordx.rb, line 71
def []=(k,v)   @h[k] = v     end
build(xml, h) click to toggle source
# File lib/recordx.rb, line 127
def build(xml, h)
  h.each_pair {|key, value| xml.send(key.to_sym, value) }
end
delete() click to toggle source
# File lib/recordx.rb, line 77
def delete()
  @callerx.delete @id
end
each(&blk) click to toggle source
# File lib/recordx.rb, line 74
def each(&blk) @h.each(&blk) end
Also aliased as: each_pair
each_pair(&blk)
Alias for: each
h() click to toggle source
# File lib/recordx.rb, line 81
def h()
  @h
end
inspect() click to toggle source
# File lib/recordx.rb, line 85
def inspect()
  "#<RecordX:%s @h=%s>" % [self.object_id, @h]
end
keys() click to toggle source
# File lib/recordx.rb, line 72
def keys()     @h.keys       end
to_h() click to toggle source
# File lib/recordx.rb, line 89
def to_h()
  @h.clone
end
to_html(xslt: '') click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/recordx.rb, line 121
def to_s()
  self.to_kvx.to_s
end
to_xml() click to toggle source
# 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) click to toggle source
# File lib/recordx.rb, line 136
def update(h)
  h.each {|name,value| self.method((name.to_s + '=').to_sym).call(value) }
end
values() click to toggle source
# File lib/recordx.rb, line 73
def values()   @h.values     end

Private Instance Methods

attr_accessor2(name,val=nil) click to toggle source
# File lib/recordx.rb, line 150
def attr_accessor2(name,val=nil)

  reserved_keywords = (
                        Object.public_methods | \
                        Kernel.public_methods | \
                        public_methods + [:method_missing]
                      )
  exceptions = [:name, :id]

  if (reserved_keywords - exceptions - @h.keys).include? name.to_sym then
    raise "recordx: reserved keyword *#{name}* can't be used as a field name"
  end

  self.instance_eval %Q{

    def #{name}=(s)

      puts 'inside #{name} assignment' if @debug

      @#{name} = s.to_s
      unless @h[:#{name}] == s.to_s then
        @h[:#{name}] =  s.to_s
        @callerx.update(@id, #{name}: s.to_s) if @callerx
      end
    end

  }

  # If this method has been monkey patched don't attempt to overwrite it

  if not self.public_methods.include? name.to_sym then
    self.instance_eval %Q{
      def #{name}()
        @#{name}
      end
    }
  end

  self.method((name + '=').to_sym).call val if val

end
method_missing(method_name, *raw_args) click to toggle source
# 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