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



42
43
44
45
46
47
48
49
# File 'lib/airtable/record.rb', line 42

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
# File 'lib/airtable/record.rb', line 17

def []=(name, value)
  @attrs[to_key(name)] = value
  define_accessor(name) unless respond_to?(name)
end

#attributesObject

Hash of attributes with underscored column names



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

def attributes; @attrs; end

#fieldsObject

Hash with keys based on airtable original column names



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

def fields; HashWithIndifferentAccess.new(Hash[@columns_map.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.



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

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



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

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



30
31
32
33
34
# File 'lib/airtable/record.rb', line 30

def override_attributes!(attrs={})
  @columns_map = 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)


51
52
53
# File 'lib/airtable/record.rb', line 51

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