Class: Rfm::Record

Inherits:
CaseInsensitiveHash show all
Defined in:
lib/rfm/record.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

#_create_accessor, #_merge_object!, #rfm_filter, #rfm_only, #to_cih

Constructor Details

#initialize(*args) ⇒ Record

resultset, attributes


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rfm/record.rb', line 137

def initialize(*args) # resultset, attributes
  @mods						||= {}
  @portals        ||= Rfm::CaseInsensitiveHash.new
			options = args.rfm_extract_options!
			if args[0].is_a?(Resultset)
@layout = args[0].layout
			elsif self.is_a?(Base)
@layout = self.class.layout
    @layout.field_keys.each do |field|
      self[field] = nil
    end
    self.update_attributes(options) unless options == {}
    self.merge!(@mods) unless @mods == {}
    @loaded = true
  end
  _attach_as_instance_variables(args[1]) if args[1].is_a? Hash
			#@loaded = true
			self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


247
248
249
250
251
252
253
254
255
256
# File 'lib/rfm/record.rb', line 247

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

, :resultset


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

Class Method Details

.new(*args) ⇒ Object

This is called during the parsing process, but only to allow creation of the correct type of model instance. This is also called by the end-user when constructing a new empty record, but it is called from the model subclass.


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rfm/record.rb', line 117

def self.new(*args) # resultset
	record = case
	
	# Get model from layout, then allocate record.
	# This should only use model class if the class already exists,
	# since we don't want to create classes that aren't defined by the user - they won't be persistant.
	when args[0].is_a?(Resultset) && args[0].layout && args[0].layout.model
		args[0].layout.modelize.allocate

	# Allocate instance of Rfm::Record.	
	else
		self.allocate
	end
	record.send(:initialize, *args)
	record
	# rescue
	# 	puts "Record.new bombed and is defaulting to super.new. Error: #{$!}"
	# 	super
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.


198
199
200
201
202
203
204
# File 'lib/rfm/record.rb', line 198

def [](key)
	# Added by wbr, 2013-03-31
	return super unless @loaded
	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, val) ⇒ Object


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/rfm/record.rb', line 211

def []=(key, val)
  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] = val
  # TODO: This needs cleaning up.
  # TODO: can we get field_type from record instead?
			@mods[key_string] = if [Date, Time, DateTime].member?(val.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 val
end
			else val
			end
  super(key, val)
end

#field_namesObject


231
232
233
# File 'lib/rfm/record.rb', line 231

def field_names
	layout.field_names
end

#replace_with_fresh_data(record) ⇒ Object


235
236
237
238
239
240
241
# File 'lib/rfm/record.rb', line 235

def replace_with_fresh_data(record)
	self.replace record
	[:@mod_id, :@record_id, :@portals, :@mods].each do |var|
		self.instance_variable_set var, record.instance_variable_get(var) || {}
	end
	self
end

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

Returns:

  • (Boolean)

206
207
208
209
# File 'lib/rfm/record.rb', line 206

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.


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

def save
  # self.merge!(layout.edit(self.record_id, @mods)[0]) if @mods.size > 0
  self.replace_with_fresh_data(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.


179
180
181
182
183
# File 'lib/rfm/record.rb', line 179

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