Class: PageRecord::Base
- Inherits:
-
Object
- Object
- PageRecord::Base
- Includes:
- Actions, Attributes, Finders, Inspector, Validations
- Defined in:
- lib/page_record/base.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
(also: #id?)
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.add_attributes(extra_attributes) ⇒ Object
Add some new attributes to the already availabe attributes.
-
.attributes(new_attributes = nil) ⇒ Array
Set's the attributes this page recognises.
-
.filter(new_filter = nil) ⇒ Object
Set's the default filter for this class.
-
.host_class(new_host_class = nil) ⇒ Class
Set's the page host class.
-
.page(new_page = nil) ⇒ Capybara::Session
Set's the page Base uses for all page operations.
-
.selector(new_selector = nil) ⇒ Object
Set's the default selector for this class.
-
.type(new_type = nil) ⇒ Symbol
Set's the default type for this class.
Instance Method Summary collapse
-
#element? ⇒ Boolean
Return the Capybara element containg the record.
-
#initialize(id = nil, selector = nil, filter = nil) ⇒ Base
constructor
A new instance of Base.
Methods included from Validations
Methods included from Attributes
#read_attribute, #read_attribute?, #write_attribute
Methods included from Inspector
Constructor Details
#initialize(id = nil, selector = nil, filter = nil) ⇒ Base
Returns a new instance of Base.
13 14 15 16 17 18 |
# File 'lib/page_record/base.rb', line 13 def initialize(id = nil, selector = nil, filter = nil) @page = self.class.page @type = self.class.instance_variable_get('@type') @id = id.to_i if id find_record(selector, filter) end |
Instance Attribute Details
#id ⇒ Object (readonly) Also known as: id?
Returns the value of attribute id.
10 11 12 |
# File 'lib/page_record/base.rb', line 10 def id @id end |
Class Method Details
.add_attributes(extra_attributes) ⇒ Object
Add some new attributes to the already availabe attributes
Example:
class TopDivisonPage < PageRecord::Base
add_attributes [:full_name, :address_line]
end
178 179 180 181 182 183 184 |
# File 'lib/page_record/base.rb', line 178 def self.add_attributes(extra_attributes) @attributes.concat(extra_attributes) # TODO: check if we can optimise this to only add the new methods define_class_methods(self) define_instance_methods(self) @attributes end |
.attributes(new_attributes = nil) ⇒ Array
Set's the attributes this page recognises. This will override any types inherited from the host class. When you don't specify a parameter, or a nil parameter .attributes will return the current set of attributes
Example:
class TopDivisonPage < PageRecord::Base
attributes [:name, :position, :ranking]
end
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/page_record/base.rb', line 153 def self.attributes(new_attributes = nil) if new_attributes undefine_class_methods(self) undefine_instance_methods(self) @attributes = new_attributes define_class_methods(self) define_instance_methods(self) end @attributes end |
.filter(new_filter = nil) ⇒ Object
Set's the default filter for this class
Example:
class TeamPage < PageRecord::Base
filter ".champions-league"
end
66 67 68 69 |
# File 'lib/page_record/base.rb', line 66 def self.filter(new_filter = nil) @filter = new_filter if new_filter @filter end |
.host_class(new_host_class = nil) ⇒ Class
Set's the page host class
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/page_record/base.rb', line 102 def self.host_class (new_host_class = nil) if new_host_class @host_class = new_host_class @host_name = new_host_class.to_s @type = @host_name.underscore get_attribute_names define_class_methods(self) define_instance_methods(self) end @host_class end |
.page(new_page = nil) ⇒ Capybara::Session
Set's the page PageRecord::Base uses for all page operations. when no parameter is given or the parameter is nil, just return the current value
rubocop:disable AvoidClassVars:
90 91 92 |
# File 'lib/page_record/base.rb', line 90 def self.page (new_page = nil) new_page ? @@page = new_page : @@page end |
.selector(new_selector = nil) ⇒ Object
Set's the default selector for this class
Example:
class TeamPage < PageRecord::Base
selector "#first-table"
end
47 48 49 50 |
# File 'lib/page_record/base.rb', line 47 def self.selector(new_selector = nil) @selector = new_selector if new_selector @selector end |
.type(new_type = nil) ⇒ Symbol
Set's the default type for this class
Example:
class TopDivisonPage < PageRecord::Base
type :team
end
TopDivisonPage.type # returns :team
132 133 134 |
# File 'lib/page_record/base.rb', line 132 def self.type(new_type = nil) new_type ? @type = new_type : @type end |
Instance Method Details
#element? ⇒ Boolean
Return the Capybara element containg the record
Example:
team_1 = TeamPage.find(1) # Get the first team
team_1.element? # access the Capybara context
end
31 32 33 |
# File 'lib/page_record/base.rb', line 31 def element? @record end |