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) ⇒ Row

Returns a new instance of Row.



12
13
14
15
16
17
# File 'lib/spodunk/row.rb', line 12

def initialize(vals, headers)
  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



58
59
60
61
62
63
64
65
# File 'lib/spodunk/row.rb', line 58

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

#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

#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



50
51
52
# File 'lib/spodunk/row.rb', line 50

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

#[]=(key, val) ⇒ Object



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

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

#assign_attributes(hsh) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/spodunk/row.rb', line 81

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



77
78
79
# File 'lib/spodunk/row.rb', line 77

def attributes
  @mash.stringify_keys
end

#changes(opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/spodunk/row.rb', line 20

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)


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

def clean?
  diff.empty?
end

#diffObject

finds difference between @originals and @mash



37
38
39
# File 'lib/spodunk/row.rb', line 37

def diff
  dirty_diff(@original, @mash)
end

#dirty?Boolean

Returns:

  • (Boolean)


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

def dirty?
  !clean?
end

#has_attribute?(k) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/spodunk/row.rb', line 95

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



32
33
34
# File 'lib/spodunk/row.rb', line 32

def itemized_changes
  changes(col_offset: 1)
end

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

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
# File 'lib/spodunk/row.rb', line 67

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