Class: Spodunk::Row
- Inherits:
-
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, )
= .map{|h| h.slugify}
@mash = Hashie::Mash.new(Hash[.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
#mash ⇒ Object
Returns the value of attribute mash.
5
6
7
|
# File 'lib/spodunk/row.rb', line 5
def mash
@mash
end
|
#original ⇒ Object
Returns the value of attribute original.
5
6
7
|
# File 'lib/spodunk/row.rb', line 5
def original
@original
end
|
#timestamp ⇒ Object
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)
h[mash_key(k)] = v
end
h
end
@mash.merge!( new_atts )
end
|
#attributes ⇒ Object
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
41
42
43
|
# File 'lib/spodunk/row.rb', line 41
def clean?
diff.empty?
end
|
#diff ⇒ Object
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
45
46
47
|
# File 'lib/spodunk/row.rb', line 45
def dirty?
!clean?
end
|
#has_attribute?(k) ⇒ Boolean
95
96
97
|
# File 'lib/spodunk/row.rb', line 95
def has_attribute?(k)
attributes.keys.include?(mash_key(k))
end
|
#itemized_changes ⇒ Object
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
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
|