Module: PageRecord::Attributes

Included in:
Base
Defined in:
lib/page_record/attributes.rb

Instance Method Summary collapse

Instance Method Details

#read_attribute(attribute) ⇒ String

Searches the record for the specified attribute and returns the text content. This method is called when you access an attribute of a record

Returns:

  • (String)

    the text content of the specified attribute

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/page_record/attributes.rb', line 27

def read_attribute(attribute)
  if block_given?
    element = yield
  else
    element = send("#{attribute}?")
  end
  tag = element.tag_name
  input_field?(tag) ? element.value : element.text
end

#read_attribute?(attribute) ⇒ Capybara::Result

Searches the record for the specified attribute and returns the Capybara Result. This method is called when you access an attribute with a ? of a record

Returns:

  • (Capybara::Result)

    the text content of the specified attribute

Raises:



12
13
14
15
16
# File 'lib/page_record/attributes.rb', line 12

def read_attribute?(attribute)
  @record.find("[data-attribute-for='#{attribute}']")
  rescue Capybara::ElementNotFound
    raise AttributeNotFound, "#{@type} record with id #{@id} doesn't contain attribute #{attribute}"
end

#write_attribute(attribute, value) ⇒ Capybara::Result

Searches the record for the specified attribute and sets the value of the attribute This method is called when you set an attribute of a record

Returns:

  • (Capybara::Result)

    the text content of the specified attribute

Raises:



46
47
48
49
50
51
52
53
54
55
# File 'lib/page_record/attributes.rb', line 46

def write_attribute(attribute, value)
  element = send("#{attribute}?")
  tag = element.tag_name
  case tag
  when 'textarea', 'input' then element.set(value)
  when 'select'then element.select(value)
  else raise NotInputField
  end
  element
end