Class: Rfm::Result::Record

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row_element, resultset, fields, layout, portal = nil) ⇒ Record

Returns a new instance of Record.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rfm_result.rb', line 49

def initialize(row_element, resultset, fields, layout, portal=nil)
  @record_id = row_element.attributes['record-id']
  @mod_id = row_element.attributes['mod-id']
  @mods = {}
  @resultset = resultset
  @layout = layout
  
  @loaded = false
  
  row_element.each_element('field') { |field| 
    field_name = field.attributes['name']
    field_name.sub!(Regexp.new(portal + '::'), '') if portal
    datum = []
    field.each_element('data') {|x| datum.push(fields[field_name].coerce(x.text))}
    if datum.length == 1
      self[field_name] = datum[0]
    elsif datum.length == 0
      self[field_name] = nil
    else
      self[field_name] = datum
    end
  }
  
  @portals = {}
  row_element.each_element('relatedset') { |relatedset|
    table = relatedset.attributes['table']
    records = []
    relatedset.each_element('record') { |record|
      records << Record.new(record, @resultset, @resultset.portals[table], @layout, table)
    }
    @portals[table] = records
  }
  
  @loaded = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *attrs) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rfm_result.rb', line 106

def method_missing (symbol, *attrs)
  # check for simple getter
  val = self[symbol.to_s]
  return val if val != nil

  # check for setter
  symbol_name = symbol.to_s
  if symbol_name[-1..-1] == '=' && self.has_key?(symbol_name[0..-2])
    return @mods[symbol_name[0..-2]] = attrs[0]
  end
  super
end

Instance Attribute Details

#mod_idObject (readonly)

Returns the value of attribute mod_id.



85
86
87
# File 'lib/rfm_result.rb', line 85

def mod_id
  @mod_id
end

#portalsObject (readonly)

Returns the value of attribute portals.



85
86
87
# File 'lib/rfm_result.rb', line 85

def portals
  @portals
end

#record_idObject (readonly)

Returns the value of attribute record_id.



85
86
87
# File 'lib/rfm_result.rb', line 85

def record_id
  @record_id
end

Instance Method Details

#[]=(name, value) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/rfm_result.rb', line 97

def []=(name, value)
  return super if !@loaded
  if self[name] != nil
    @mods[name] = val
  else
    raise "No such field: #{name}"
  end
end

#respond_to?(symbol, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/rfm_result.rb', line 119

def respond_to?(symbol, include_private = false)
  return true if self[symbol.to_s] != nil
  super
end

#saveObject



87
88
89
90
# File 'lib/rfm_result.rb', line 87

def save
  self.merge(@layout.edit(self.record_id, @mods)[0]) if @mods.size > 0
  @mods.clear
end

#save_if_not_modifiedObject



92
93
94
95
# File 'lib/rfm_result.rb', line 92

def save_if_not_modified
  self.merge(@layout.edit(@record_id, @mods, {'-modid' => @mod_id})[0]) if @mods.size > 0
  @mods.clear
end