class Spodunk::Row

Attributes

connection[R]
mash[R]
original[R]
table[R]
timestamp[R]

Public Class Methods

new(vals, headers, opts={}) click to toggle source
# File lib/spodunk/row.rb, line 13
def initialize(vals, headers, opts={})
  @table = opts[:table]

  slugged_headers = headers.map{|h| h.slugify}
  @mash = Hashie::Mash.new(Hash[slugged_headers.zip(vals)])
  @original = @mash.dup.freeze
  @timestamp = Time.now 
end

Public Instance Methods

[](key) click to toggle source
# File lib/spodunk/row.rb, line 53
def [](key)
  @mash[mash_key(key)]
end
[]=(key, val) click to toggle source
# File lib/spodunk/row.rb, line 57
def []=(key, val)
  @mash[mash_key(key)] = val
end
assign_attributes(hsh) click to toggle source
# File lib/spodunk/row.rb, line 86
def assign_attributes(hsh)
  new_atts = hsh.inject({}) do |h, (k, v)|
    if has_attribute?(k)
      # ignore attributes that aren't part of the model
      h[mash_key(k)] = v
    end

    h
  end

  @mash.merge!( new_atts )
end
attributes() click to toggle source
# File lib/spodunk/row.rb, line 82
def attributes
  @mash.stringify_keys
end
changes(opts={}) click to toggle source
# File lib/spodunk/row.rb, line 23
def changes(opts={})
  offset = opts[:col_offset]
  diff.inject({}) do |h, (k,v)|
    x = offset ? mash_index(k) + offset : k
    h[x] = v.last
 
    h
  end
end
clean?() click to toggle source
# File lib/spodunk/row.rb, line 44
def clean?
  diff.empty?
end
diff() click to toggle source

finds difference between @originals and @mash

# File lib/spodunk/row.rb, line 40
def diff
  dirty_diff(@original, @mash)
end
dirty?() click to toggle source
# File lib/spodunk/row.rb, line 48
def dirty?
  !clean?
end
has_attribute?(k) click to toggle source
# File lib/spodunk/row.rb, line 100
def has_attribute?(k)
  attributes.keys.include?(mash_key(k))
end
itemized_changes() click to toggle source

by default, spreadsheets are assumed to be numbered with columns starting from 1

# File lib/spodunk/row.rb, line 35
def itemized_changes
  changes(col_offset: 1)
end
method_missing(foo, *args, &blk) click to toggle source
Calls superclass method
# File lib/spodunk/row.rb, line 61
def method_missing(foo, *args, &blk)
  foop = foo.to_s.match(/\w+(?==)?/).to_s
  if @mash.keys.include?(foop)
    @mash.send(foo, *args)
  else
    super
  end
end
respond_to?(foo, inc_private=false) click to toggle source
Calls superclass method
# File lib/spodunk/row.rb, line 70
def respond_to?(foo, inc_private=false)
  foop = foo.to_s.match(/\w+(?=$|=)/).to_s
  if @mash.keys.include?(foop)
    true
  else
    super
  end
end
save() click to toggle source

this could be dangerous

# File lib/spodunk/row.rb, line 147
def save
  connection.save_row(table, self)
end

Private Instance Methods

dirty_diff(h1, h2) click to toggle source

Since Rails diff is being deprecated this is a simple hack that DOES NOT do nested hashes and expects m1 and m2 to have EXACTLY the same stringified keys

returns a hash with an array of differences {

a: ['before', 'after'],
b: [nil, 'now']

}

# File lib/spodunk/row.rb, line 116
def dirty_diff(h1, h2)
  h1.stringify_keys.inject({}) do |h, (k, v1)|
    v2 = h2[k]
    h[k] = [v1, v2] if v1 != v2
    
    h  
  end
end
mash_index(key) click to toggle source
# File lib/spodunk/row.rb, line 135
def mash_index(key)
  if key.is_a?(Fixnum)
    return key
  else
    return keys.index(key.to_s)
  end
end
mash_key(idx) click to toggle source

a method that converts integer vals or Strings to proper slugified keys

# File lib/spodunk/row.rb, line 127
def mash_key(idx)
  if idx.is_a?(Fixnum)
    return keys[idx]
  else
    return idx.to_s.slugify
  end
end