Class: Airtable::Record

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

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Record

Returns a new instance of Record.



4
5
6
# File 'lib/airtable/record.rb', line 4

def initialize(attrs={})
  override_attributes!(attrs)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object



45
46
47
48
49
50
51
52
# 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

Instance Method Details

#[](name) ⇒ Object

Return given attribute based on name or blank otherwise



12
13
14
# File 'lib/airtable/record.rb', line 12

def [](name)
  @attrs.has_key?(to_key(name)) ? @attrs[to_key(name)] : ""
end

#[]=(name, value) ⇒ Object

Set the given attribute to value



17
18
19
20
21
# 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

#attributesObject

Hash of attributes with underscored column names



28
# File 'lib/airtable/record.rb', line 28

def attributes; @attrs; end

#fieldsObject

Hash with keys based on airtable original column names



38
39
40
# File 'lib/airtable/record.rb', line 38

def fields
  HashWithIndifferentAccess.new(Hash[@column_keys.map { |k| [ k, @attrs[to_key(k)] ] }])
end

#fields_for_updateObject

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



43
# File 'lib/airtable/record.rb', line 43

def fields_for_update; fields.except(:id); end

#idObject



8
# File 'lib/airtable/record.rb', line 8

def id; @attrs["id"]; end

#id=(val) ⇒ Object



9
# File 'lib/airtable/record.rb', line 9

def id=(val); @attrs["id"] = val; end

#inspectObject



23
24
25
# File 'lib/airtable/record.rb', line 23

def inspect
  "#<Airtable::Record #{attributes.map { |a, v| ":#{a}=>#{v.inspect}" }.join(", ")}>"
end

#override_attributes!(attrs = {}) ⇒ Object

Removes old and add new attributes for the record



31
32
33
34
35
# 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) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/airtable/record.rb', line 54

def respond_to?(name, include_private = false)
  @attrs.has_key?(name) || super
end