Class: PageRecord::Base

Inherits:
Object
  • Object
show all
Includes:
Actions, Attributes, Finders, Inspector, Validations
Defined in:
lib/page_record/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

#errors, #invalid?, #valid?

Methods included from Attributes

#read_attribute, #read_attribute?, #write_attribute

Methods included from Inspector

#inspect

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

#idObject (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

Parameters:

  • extra_attributes (Array)

    The additional attributes the page regognises



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

Parameters:

  • new_attributes (Array) (defaults to: nil)

    The attributes the page regognises

Returns:

  • (Array)

    returns the array of attributes the page recognises



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

Parameters:

  • new_filter (String) (defaults to: nil)

    The default filter to be used for all finders



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

Parameters:

  • new_host_class (defaults to: nil)

    an ActiveRecord like class

Returns:

  • (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:

Parameters:

  • new_page (Cabybara::Session) (defaults to: nil)

    The Capybara page

Returns:

  • (Capybara::Session)


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

Parameters:

  • new_selector (String) (defaults to: nil)

    The default selector to be used for all finders



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

Parameters:

  • new_type (Symbol) (defaults to: nil)

    The default type to be used for all finders. If type is nil just return the current type

Returns:

  • (Symbol)

    the type set.



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

Returns:

  • (Boolean)


31
32
33
# File 'lib/page_record/base.rb', line 31

def element?
  @record
end