Class: Rfm::Record

Inherits:
CaseInsensitiveHash show all
Defined in:
lib/rfm/record.rb,
lib/rfm/base.rb

Overview

Field Types and Ruby Types

RFM automatically converts data from FileMaker into a Ruby object with the most reasonable type possible. The type are mapped thusly:

  • Text fields are converted to Ruby String objects

  • Number fields are converted to Ruby BigDecimal objects (the basic Ruby numeric types have much less precision and range than FileMaker number fields)

  • Date fields are converted to Ruby Date objects

  • Time fields are converted to Ruby DateTime objects (you can ignore the date component)

  • Timestamp fields are converted to Ruby DateTime objects

  • Container fields are converted to Ruby URI objects

Attributes

In addition to portals, the Record object has these useful attributes:

  • record_id is FileMaker’s internal identifier for this record (not any ID field you might have in your table); you need a record_id to edit or delete a record

  • mod_id is the modification identifier for the record; whenever a record is modified, its mod_id changes so you can tell if the Record object you’re looking at is up-to-date as compared to another copy of the same record

Direct Known Subclasses

Base

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#rfm_filter, #rfm_only, #to_cih

Constructor Details

#initialize(record, resultset_obj, field_meta, layout_obj, portal = nil) ⇒ Record

Returns a new instance of Record.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rfm/record.rb', line 116

def initialize(record, resultset_obj, field_meta, layout_obj, portal=nil)

  @layout        = layout_obj
  @resultset     = resultset_obj
  @record_id     = record.record_id rescue nil
  @mod_id        = record.mod_id rescue nil
  @mods          = {}
  @portals     ||= Rfm::CaseInsensitiveHash.new

  relatedsets = !portal && resultset_obj.instance_variable_get(:@include_portals) ? record.portals : []
  
  record.columns.each do |field|
  	next unless field
    field_name = @layout.field_mapping[field.name] || field.name rescue field.name
    field_name.gsub!(Regexp.new(portal + '::'), '') if portal
    datum = []
    data = field.data #['data']; data = data.is_a?(Hash) ? [data] : data
    data.each do |x|
    	next unless field_meta[field_name]
    	begin
       datum.push(field_meta[field_name].coerce(x, resultset_obj)) #(x['__content__'], resultset_obj))
    	rescue StandardError => error
    		self.errors.add(field_name, error) if self.respond_to? :errors
    		raise error unless @layout.ignore_bad_data
    	end
    end if data
  
    if datum.length == 1
      rfm_super[field_name] = datum[0]
    elsif datum.length == 0
      rfm_super[field_name] = nil
    else
      rfm_super[field_name] = datum
    end
  end
  
  unless relatedsets.empty?
    relatedsets.each do |relatedset|
    	next if relatedset.blank?
      tablename, records = relatedset.table, []
  
      relatedset.records.each do |record|
      	next unless record
        records << self.class.new(record, resultset_obj, resultset_obj.portal_meta[tablename], layout_obj, tablename)
      end
  
      @portals[tablename] = records
    end
  end
  
  @loaded = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *attrs, &block) ⇒ Object (private)



265
266
267
268
269
270
271
272
273
274
# File 'lib/rfm/record.rb', line 265

def method_missing (symbol, *attrs, &block)
	  method = symbol.to_s
	  return self[method] if self.key?(method)
	  return @portals[method] if @portals and @portals.key?(method)

	  if method =~ /(=)$/
	    return self[$`] = attrs.first if self.key?($`)
	  end
	  super
end

Instance Attribute Details

#layoutObject

Returns the value of attribute layout.



111
112
113
# File 'lib/rfm/record.rb', line 111

def layout
  @layout
end

#mod_idObject (readonly)

Returns the value of attribute mod_id.



112
113
114
# File 'lib/rfm/record.rb', line 112

def mod_id
  @mod_id
end

#portalsObject (readonly)

Returns the value of attribute portals.



112
113
114
# File 'lib/rfm/record.rb', line 112

def portals
  @portals
end

#record_idObject (readonly)

Returns the value of attribute record_id.



112
113
114
# File 'lib/rfm/record.rb', line 112

def record_id
  @record_id
end

#resultsetObject

Returns the value of attribute resultset.



111
112
113
# File 'lib/rfm/record.rb', line 111

def resultset
  @resultset
end

Class Method Details

.build_records(records, resultset_obj, field_meta, layout_obj, portal = nil) ⇒ Object



169
170
171
172
173
# File 'lib/rfm/record.rb', line 169

def self.build_records(records, resultset_obj, field_meta, layout_obj, portal=nil)
  records.each do |record|
    resultset_obj << self.new(record, resultset_obj, field_meta, layout_obj, portal)
  end
end

.new(*args) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/rfm/base.rb', line 105

def new(*args)
	#puts "Creating new record from RECORD. Layout: #{args[3].class} #{args[3].object_id}"
	args[3].model.new(*args)
rescue
	#puts "RECORD failed to send 'new' to MODEL"
	super
	#allocate.send(:initialize, *args)
end

Instance Method Details

#[](key) ⇒ Object

Gets the value of a field from the record. For example:

first = myRecord["First Name"]
last = myRecord["Last Name"]

This sample puts the first and last name from the record into Ruby variables.

You can also update a field:

myRecord["First Name"] = "Sophia"

When you do, the change is noted, but *the data is not updated in FileMaker*. You must call Record::save or Record::save_if_not_modified to actually save the data.



214
215
216
217
218
# File 'lib/rfm/record.rb', line 214

def [](key)
	return fetch(key.to_s.downcase)
rescue IndexError
 	raise Rfm::ParameterError, "#{key} does not exists as a field in the current Filemaker layout." unless key.to_s == '' #unless (!layout or self.key?(key_string))
end

#[]=(key, value) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/rfm/record.rb', line 225

def []=(key, value)
  key_string = key.to_s.downcase
  return super unless @loaded # is this needed?
  raise Rfm::ParameterError, "You attempted to modify a field (#{key_string}) that does not exist in the current Filemaker layout." unless self.key?(key_string)
  # @mods[key_string] = value
  # TODO: This needs cleaning up.
  # TODO: can we get field_type from record instead?
			@mods[key_string] = if [Date, Time, DateTime].member?(value.class)
field_type = layout.field_meta[key_string.to_sym].result
case field_type
	when 'time'; val.strftime(layout.time_format)
	when 'date'; val.strftime(layout.date_format)
	when 'timestamp'; val.strftime(layout.timestamp_format)
else value
end
			else value
			end
  super(key, value)
end

#field_namesObject

alias_method :old_setter, ‘[]=’ def []=(key,val) old_setter(key,val) return val unless [Date, Time, DateTime].member? val.class field_type = layout.field_meta.result @mods = case field_type when ‘time’; val.strftime(layout.time_format) when ‘date’; val.strftime(layout.date_format) when ‘timestamp’; val.strftime(layout.timestamp_format) else val end end



258
259
260
# File 'lib/rfm/record.rb', line 258

def field_names
	resultset.field_names rescue layout.field_names
end

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

Returns:

  • (Boolean)


220
221
222
223
# File 'lib/rfm/record.rb', line 220

def respond_to?(symbol, include_private = false)
  return true if self.include?(symbol.to_s)
  super
end

#saveObject

Saves local changes to the Record object back to Filemaker. For example:

myLayout.find({"First Name" => "Bill"}).each(|record|
  record["First Name"] = "Steve"
  record.save
)

This code finds every record with Bill in the First Name field, then changes the first name to Steve.

Note: This method is smart enough to not bother saving if nothing has changed. So there’s no need to optimize on your end. Just save, and if you’ve changed the record it will be saved. If not, no server hit is incurred.



188
189
190
191
# File 'lib/rfm/record.rb', line 188

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

#save_if_not_modifiedObject

Like Record::save, except it fails (and raises an error) if the underlying record in FileMaker was modified after the record was fetched but before it was saved. In other words, prevents you from accidentally overwriting changes someone else made to the record.



196
197
198
199
# File 'lib/rfm/record.rb', line 196

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