Class: RecordX

Inherits:
Object
  • Object
show all
Defined in:
lib/recordx.rb

Defined Under Namespace

Classes: RXHash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = nil, callerx = nil, id = nil, created = nil, last_modified = nil) ⇒ RecordX

Returns a new instance of RecordX.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/recordx.rb', line 25

def initialize(x=nil, callerx=nil, id=nil, created=nil, last_modified=nil)

  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)



77
78
79
80
81
82
# File 'lib/recordx.rb', line 77

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

#createdObject (readonly)

Returns the value of attribute created.



8
9
10
# File 'lib/recordx.rb', line 8

def created
  @created
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/recordx.rb', line 8

def id
  @id
end

#last_modifiedObject (readonly)

Returns the value of attribute last_modified.



8
9
10
# File 'lib/recordx.rb', line 8

def last_modified
  @last_modified
end

Instance Method Details

#[](k) ⇒ Object



50
# File 'lib/recordx.rb', line 50

def [](k)      @h[k]         end

#[]=(k, v) ⇒ Object



51
# File 'lib/recordx.rb', line 51

def []=(k,v)   @h[k] = v     end

#deleteObject



57
58
59
# File 'lib/recordx.rb', line 57

def delete()
  @callerx.delete @id
end

#each(&blk) ⇒ Object Also known as: each_pair



54
# File 'lib/recordx.rb', line 54

def each(&blk) @h.each(&blk) end

#hObject Also known as: to_h



61
62
63
# File 'lib/recordx.rb', line 61

def h()
  @h
end

#inspectObject



65
66
67
# File 'lib/recordx.rb', line 65

def inspect()
  "#<RecordX:%s @h=%s>" % [self.object_id, @h]
end

#keysObject



52
# File 'lib/recordx.rb', line 52

def keys()     @h.keys       end

#update(h) ⇒ Object



71
72
73
# File 'lib/recordx.rb', line 71

def update(h)
  h.each {|name,value| self.method((name.to_s + '=').to_sym).call(value) }
end

#valuesObject



53
# File 'lib/recordx.rb', line 53

def values()   @h.keys       end