Class: Sheng::CheckBox

Inherits:
Object
  • Object
show all
Defined in:
lib/sheng/check_box.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element = nil) ⇒ CheckBox

Returns a new instance of CheckBox.



11
12
13
14
15
# File 'lib/sheng/check_box.rb', line 11

def initialize(element = nil)
  @element = element
  @xml_document = element.document
  @errors = []
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



3
4
5
# File 'lib/sheng/check_box.rb', line 3

def element
  @element
end

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/sheng/check_box.rb', line 3

def errors
  @errors
end

#xml_documentObject (readonly)

Returns the value of attribute xml_document.



3
4
5
# File 'lib/sheng/check_box.rb', line 3

def xml_document
  @xml_document
end

Class Method Details

.from_element(element) ⇒ Object



6
7
8
# File 'lib/sheng/check_box.rb', line 6

def from_element(element)
  new(element)
end

Instance Method Details

#==(other) ⇒ Object



17
18
19
# File 'lib/sheng/check_box.rb', line 17

def ==(other)
  other.is_a?(self.class) && other.element == element
end

#interpolate(data_set) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sheng/check_box.rb', line 29

def interpolate(data_set)
  value = data_set.fetch(key)
  checked_attribute = @element.search('.//w:default').first.attribute('val')
  checked_attribute.value = value_is_truthy?(value) ? '1' : '0'
rescue DataSet::KeyNotFound => e
  @errors << e
  # Ignore this error; if the key for this checkbox is not found in the
  # data set, we don't want to uncheck the checkbox; we just want to leave
  # it alone.
  nil
end

#keyObject



21
22
23
# File 'lib/sheng/check_box.rb', line 21

def key
  @element.xpath('.//w:name').first['w:val']
end

#raw_keyObject



25
26
27
# File 'lib/sheng/check_box.rb', line 25

def raw_key
  key
end

#value_is_truthy?(value) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/sheng/check_box.rb', line 41

def value_is_truthy?(value)
  ['true', '1', 'yes'].include? value.to_s.downcase
end