module Airmodel::Utils

Public Instance Methods

-(first_hash, second_hash) click to toggle source
# File lib/airmodel/utils.rb, line 81
def -(first_hash, second_hash)
  hash_diff(first_hash, second_hash)
end
airtable_formatted(hash) click to toggle source

convert blank strings to nil, [“”] to [], and “true” to a boolean

# File lib/airmodel/utils.rb, line 39
def airtable_formatted(hash)
  h = hash.dup
  h.each{|k,v|
    if v == [""]
      h[k] = []
    elsif v == ""
      h[k] = nil
    elsif v == "true"
      h[k] = true
    elsif v == "false"
      h[k] = false
    end
  }
end
at(base_id, table_name) click to toggle source
# File lib/airmodel/utils.rb, line 22
def at(base_id, table_name)
  Airmodel.client.table(base_id, table_name)
end
base_config() click to toggle source
# File lib/airmodel/utils.rb, line 8
def base_config
  if @base_id
    { :base_id => @base_id, :table_name => table_name }
  else
    Airmodel.bases[table_name] || raise(NoSuchBase.new("Could not find base '#{table_name}' in config file"))
  end
end
classify(obj=[]) click to toggle source
converts array of generic airtable records to the instances

of the appropriate class

# File lib/airmodel/utils.rb, line 28
def classify(obj=[])
  if obj.is_a? Airtable::Record
    [self.new(obj.fields)]
  elsif obj.respond_to? :map
    obj.map{|r| self.new(r.fields) }
  else
    raise AlienObject.new("Object is neither an array nor an Airtable::Model")
  end
end
hash_diff(first_hash, second_hash) click to toggle source
# File lib/airmodel/utils.rb, line 77
def hash_diff(first_hash, second_hash)
  hash_diff!(first_hash.dup, second_hash)
end
table() click to toggle source

return an Airtable::Table object, backed by a base defined in DB YAML file

# File lib/airmodel/utils.rb, line 18
def table
  Airmodel.client.table base_config[:base_id], base_config[:table_name]
end
table_name() click to toggle source
# File lib/airmodel/utils.rb, line 4
def table_name
  @table_name || self.name.tableize.to_sym
end