Module: Airmodel::Utils
- Included in:
- Model
- Defined in:
- lib/airmodel/utils.rb
Defined Under Namespace
Classes: AlienObject
Instance Method Summary collapse
- #-(first_hash, second_hash) ⇒ Object
-
#airtable_formatted(hash) ⇒ Object
convert blank strings to nil, [“”] to [], and “true” to a boolean.
- #at(base_id, table_name) ⇒ Object
- #base_config ⇒ Object
-
#classify(obj = []) ⇒ Object
converts array of generic airtable records to the instances of the appropriate class.
- #hash_diff(first_hash, second_hash) ⇒ Object
-
#hash_diff!(first_hash, second_hash) ⇒ Object
Returns a hash that removes any matches with the other hash.
-
#table ⇒ Object
return an Airtable::Table object, backed by a base defined in DB YAML file.
- #table_name ⇒ Object
Instance Method Details
#-(first_hash, second_hash) ⇒ Object
81 82 83 |
# File 'lib/airmodel/utils.rb', line 81 def -(first_hash, second_hash) hash_diff(first_hash, second_hash) end |
#airtable_formatted(hash) ⇒ Object
convert blank strings to nil, [“”] to [], and “true” to a boolean
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# 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) ⇒ Object
22 23 24 |
# File 'lib/airmodel/utils.rb', line 22 def at(base_id, table_name) Airmodel.client.table(base_id, table_name) end |
#base_config ⇒ Object
8 9 10 11 12 13 14 |
# 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 = []) ⇒ Object
converts array of generic airtable records to the instances of the appropriate class
28 29 30 31 32 33 34 35 36 |
# 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) ⇒ Object
77 78 79 |
# File 'lib/airmodel/utils.rb', line 77 def hash_diff(first_hash, second_hash) hash_diff!(first_hash.dup, second_hash) end |
#hash_diff!(first_hash, second_hash) ⇒ Object
Returns a hash that removes any matches with the other hash
{b:“c”} - :a=>{:b=>“c”} # => {} [{c:“d”,b:“c”]} - => [{c:“d”, b:“d”]} # => href="{:b=>"c"">a=>}
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/airmodel/utils.rb', line 59 def hash_diff!(first_hash, second_hash) second_hash.each_pair do |k,v| tv = first_hash[k] if tv.is_a?(Hash) && v.is_a?(Hash) && v.present? && tv.present? tv.diff!(v) elsif v.is_a?(Array) && tv.is_a?(Array) && v.present? && tv.present? v.each_with_index do |x, i| tv[i].diff!(x) end first_hash[k] = tv - [{}] else first_hash.delete(k) if first_hash.has_key?(k) && tv == v end first_hash.delete(k) if first_hash.has_key?(k) && first_hash[k].blank? end first_hash end |
#table ⇒ Object
return an Airtable::Table object, backed by a base defined in DB YAML file
18 19 20 |
# File 'lib/airmodel/utils.rb', line 18 def table Airmodel.client.table base_config[:base_id], base_config[:table_name] end |
#table_name ⇒ Object
4 5 6 |
# File 'lib/airmodel/utils.rb', line 4 def table_name @table_name || self.name.tableize.to_sym end |