Class: Spodunk::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/spodunk/row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vals, headers, opts = {}) ⇒ Row

Returns a new instance of Row.



13
14
15
16
17
18
19
20
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



61
62
63
64
65
66
67
68
# 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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/spodunk/row.rb', line 5

def connection
  @connection
end

#mashObject (readonly)

Returns the value of attribute mash.



5
6
7
# File 'lib/spodunk/row.rb', line 5

def mash
  @mash
end

#originalObject (readonly)

Returns the value of attribute original.



5
6
7
# File 'lib/spodunk/row.rb', line 5

def original
  @original
end

#tableObject (readonly)

Returns the value of attribute table.



5
6
7
# File 'lib/spodunk/row.rb', line 5

def table
  @table
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



5
6
7
# File 'lib/spodunk/row.rb', line 5

def timestamp
  @timestamp
end

Instance Method Details

#[](key) ⇒ Object



53
54
55
# File 'lib/spodunk/row.rb', line 53

def [](key)
  @mash[mash_key(key)]
end

#[]=(key, val) ⇒ Object



57
58
59
# File 'lib/spodunk/row.rb', line 57

def []=(key, val)
  @mash[mash_key(key)] = val
end

#assign_attributes(hsh) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# 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

#attributesObject



82
83
84
# File 'lib/spodunk/row.rb', line 82

def attributes
  @mash.stringify_keys
end

#changes(opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
# 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?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/spodunk/row.rb', line 44

def clean?
  diff.empty?
end

#diffObject

finds difference between @originals and @mash



40
41
42
# File 'lib/spodunk/row.rb', line 40

def diff
  dirty_diff(@original, @mash)
end

#dirty?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/spodunk/row.rb', line 48

def dirty?
  !clean?
end

#has_attribute?(k) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/spodunk/row.rb', line 100

def has_attribute?(k)
  attributes.keys.include?(mash_key(k))
end

#itemized_changesObject

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



35
36
37
# File 'lib/spodunk/row.rb', line 35

def itemized_changes
  changes(col_offset: 1)
end

#respond_to?(foo, inc_private = false) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
# 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

#saveObject

this could be dangerous



147
148
149
# File 'lib/spodunk/row.rb', line 147

def save
  connection.save_row(table, self)
end