Class: Spodunk::Row
- Inherits:
-
Object
- Object
- Spodunk::Row
- Defined in:
- lib/spodunk/row.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#mash ⇒ Object
readonly
Returns the value of attribute mash.
-
#original ⇒ Object
readonly
Returns the value of attribute original.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #assign_attributes(hsh) ⇒ Object
- #attributes ⇒ Object
- #changes(opts = {}) ⇒ Object
- #clean? ⇒ Boolean
-
#diff ⇒ Object
finds difference between @originals and @mash.
- #dirty? ⇒ Boolean
- #has_attribute?(k) ⇒ Boolean
-
#initialize(vals, headers, opts = {}) ⇒ Row
constructor
A new instance of Row.
-
#itemized_changes ⇒ Object
by default, spreadsheets are assumed to be numbered with columns starting from 1.
- #method_missing(foo, *args, &blk) ⇒ Object
- #respond_to?(foo, inc_private = false) ⇒ Boolean
-
#save ⇒ Object
this could be dangerous.
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 = 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
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
5 6 7 |
# File 'lib/spodunk/row.rb', line 5 def connection @connection end |
#mash ⇒ Object (readonly)
Returns the value of attribute mash.
5 6 7 |
# File 'lib/spodunk/row.rb', line 5 def mash @mash end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
5 6 7 |
# File 'lib/spodunk/row.rb', line 5 def original @original end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
5 6 7 |
# File 'lib/spodunk/row.rb', line 5 def table @table end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
5 6 7 |
# File 'lib/spodunk/row.rb', line 5 def 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 |
#attributes ⇒ Object
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
44 45 46 |
# File 'lib/spodunk/row.rb', line 44 def clean? diff.empty? end |
#diff ⇒ Object
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
48 49 50 |
# File 'lib/spodunk/row.rb', line 48 def dirty? !clean? end |
#has_attribute?(k) ⇒ Boolean
100 101 102 |
# File 'lib/spodunk/row.rb', line 100 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
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
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 |
#save ⇒ Object
this could be dangerous
147 148 149 |
# File 'lib/spodunk/row.rb', line 147 def save connection.save_row(table, self) end |