class Airtable::Record

Public Class Methods

new(attrs={}) click to toggle source
# File lib/airtable/record.rb, line 4
def initialize(attrs={})
  override_attributes!(attrs)
end

Public Instance Methods

[](name) click to toggle source

Return given attribute based on name or blank otherwise

# File lib/airtable/record.rb, line 12
def [](name)
  @attrs.has_key?(to_key(name)) ? @attrs[to_key(name)] : ""
end
[]=(name, value) click to toggle source

Set the given attribute to value

# File lib/airtable/record.rb, line 17
def []=(name, value)
  @column_keys << name
  @attrs[to_key(name)] = value
  define_accessor(name) unless respond_to?(name)
end
attributes() click to toggle source

Hash of attributes with underscored column names

# File lib/airtable/record.rb, line 28
def attributes; @attrs; end
fields() click to toggle source

Hash with keys based on airtable original column names

# File lib/airtable/record.rb, line 38
def fields
  HashWithIndifferentAccess.new(Hash[@column_keys.map { |k| [ k, @attrs[to_key(k)] ] }])
end
fields_for_update() click to toggle source

Airtable will complain if we pass an 'id' as part of the request body.

# File lib/airtable/record.rb, line 43
def fields_for_update; fields.except(:id); end
id() click to toggle source
# File lib/airtable/record.rb, line 8
def id; @attrs["id"]; end
id=(val) click to toggle source
# File lib/airtable/record.rb, line 9
def id=(val); @attrs["id"] = val; end
inspect() click to toggle source
# File lib/airtable/record.rb, line 23
def inspect
  "#<Airtable::Record #{attributes.map { |a, v| ":#{a}=>#{v.inspect}" }.join(", ")}>"
end
method_missing(name, *args, &blk) click to toggle source
Calls superclass method
# File lib/airtable/record.rb, line 45
def method_missing(name, *args, &blk)
  # Accessor for attributes
  if args.empty? && blk.nil? && @attrs.has_key?(name)
    @attrs[name]
  else
    super
  end
end
override_attributes!(attrs={}) click to toggle source

Removes old and add new attributes for the record

# File lib/airtable/record.rb, line 31
def override_attributes!(attrs={})
  @column_keys = attrs.keys
  @attrs = HashWithIndifferentAccess.new(Hash[attrs.map { |k, v| [ to_key(k), v ] }])
  @attrs.map { |k, v| define_accessor(k) }
end
respond_to?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/airtable/record.rb, line 54
def respond_to?(name, include_private = false)
  @attrs.has_key?(name) || super
end

Protected Instance Methods

define_accessor(name) click to toggle source
# File lib/airtable/record.rb, line 71
def define_accessor(name)
  self.class.send(:define_method, name) { @attrs[name] }
end
to_key(string) click to toggle source
# File lib/airtable/record.rb, line 60
def to_key(string)
  string.is_a?(Symbol) ? string : underscore(string).to_sym
end
underscore(string) click to toggle source
# File lib/airtable/record.rb, line 64
def underscore(string)
  string.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    gsub(/\s/, '_').tr("-", "_").downcase
end