Class: Rusk::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/rusk/cell.rb

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Cell

Returns a new instance of Cell.



6
7
8
# File 'lib/rusk/cell.rb', line 6

def initialize(content)
  @content = content
end

Instance Method Details

#to_sObject



53
54
55
# File 'lib/rusk/cell.rb', line 53

def to_s
  @content.xpath("text:p").text
end

#valueObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rusk/cell.rb', line 10

def value
  case value_type
  when "date"
    if @content["office:date-value"] =~ /^\d{4}-\d{2}-\d{2}$/
      Date.strptime(@content["office:date-value"], "%Y-%m-%d")
    else
      DateTime.strptime(@content["office:date-value"], "%Y-%m-%dT%H:%M:%S")
    end
  when "float", "currency", "percentage"
    @content["office:value"].to_f
  when "boolean"
    @content["office:boolean-value"] == 'true'
  else
    @content.xpath("text:p").text
  end
end

#value=(value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rusk/cell.rb', line 27

def value=(value)
  if value.is_a? Numeric
    @content["office:value"] = value.to_s
    @content["office:value-type"] = "float"
  elsif value.is_a? Date
    @content["office:date-value"] = value.strftime("%F")
    @content["office:value-type"] = 'date'
  else
    @content["office:value-type"] = 'string'
  end

  if @content.xpath("text:p").children.empty?
    Nokogiri::XML::Text.new("\n", @content).parent = @content
    textp = Nokogiri::XML::Node.new("text:p", @content)
    Nokogiri::XML::Text.new("", @content).parent = textp
    textp.parent = @content
    Nokogiri::XML::Text.new("\n", @content).parent = @content
  end

  @content.xpath("text:p").children.first.content = value
end

#value_typeObject



49
50
51
# File 'lib/rusk/cell.rb', line 49

def value_type
  @content["office:value-type"]
end