Class: Sheety::Row

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

Constant Summary

Constants inherited from Feed

Feed::LINK_ALT, Feed::LINK_EDIT, Feed::LINK_SELF

Instance Attribute Summary

Attributes inherited from Feed

#content, #id, #parent, #title, #updated

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Feed

#link

Methods included from Children

#_get_i_val, #_passes_constraint, included

Constructor Details

#initialize(parent, entry = nil) ⇒ Row

Returns a new instance of Row.



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

def initialize(parent, entry=nil)
  @attrs = {}
  super(parent, entry)
end

Class Method Details

.normalize_key(key) ⇒ Object



84
85
86
# File 'lib/sheety/row.rb', line 84

def self.normalize_key(key)
  key.to_s.gsub(/[^a-zA-Z0-9]/, '')
end

Instance Method Details

#[]=(key, val) ⇒ Object



24
25
26
# File 'lib/sheety/row.rb', line 24

def []=(key, val)
  @attrs['gsx:' + key.to_s]=val
end

#as_xmlObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sheety/row.rb', line 32

def as_xml
  return [
      '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">',
      if !@id then
        ''
      else
        "<id>#{@id}</id>"
      end,
      if !@id then
        ''
      else
        "<updated>#{DateTime.now}</updated>"
      end,
      if !@id then
        ''
      else
        '<category scheme="http://schemas.google.com/spreadsheets/2006" term="http://schemas.google.com/spreadsheets/2006#list"/>'
      end,
      if !@id then
        ''
      else
        "<link rel=\"edit\" type=\"application/atom+xml\" href=\"#{@links[LINK_EDIT]}\" />"
      end,
      *(@attrs.map { |pair| "<#{pair[0]}>#{pair[1]}</#{pair[0]}>" }),
      '</entry>',
  ].join(Sheety::NEWLINE)
end

#deleteObject

ProTip: Delete Rows in Reverse, otherwise the shift of deletion will cause unexpected behaviors



72
73
74
# File 'lib/sheety/row.rb', line 72

def delete
  return Sheety::Api.inst.delete_feed(link(LINK_EDIT))
end

#inspectObject



80
81
82
# File 'lib/sheety/row.rb', line 80

def inspect
  to_s
end

#parse(entry) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/sheety/row.rb', line 9

def parse(entry)
  super(entry)
  entry.keys.each do |k|
    if /\Agsx:/i =~ k
      @attrs[k] = entry[k][0]
    end
  end
end

#put(hash) ⇒ Object



28
29
30
# File 'lib/sheety/row.rb', line 28

def put(hash)
  hash.each { |kv| self[Sheety::Row.normalize_key(kv[0])] = kv[1] }
end

#saveObject Also known as: update



60
61
62
63
64
65
66
67
# File 'lib/sheety/row.rb', line 60

def save
  uri = link(LINK_EDIT)
  if uri
    return Sheety::Api.inst.put_feed(uri, as_xml)
  else
    return Sheety::Api.inst.post_feed(@parent.link(Sheety::Worksheet::LINK_POST), as_xml)
  end
end

#to_sObject



76
77
78
# File 'lib/sheety/row.rb', line 76

def to_s
  "<#{self.class}::#{object_id} #{(@attrs.map { |kv| "#{kv[0].gsub('gsx:', '')}:#{kv[1]}" }).join(', ')}>"
end

#value(key) ⇒ Object Also known as: []



18
19
20
# File 'lib/sheety/row.rb', line 18

def value(key)
  return @attrs['gsx:' + key.to_s]
end