Skip to content

Commit

Permalink
Add support for setting other attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bert Hajee authored and Bert Hajee committed Jun 13, 2013
1 parent fef90cd commit 7220c87
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 16 deletions.
72 changes: 57 additions & 15 deletions lib/page_record/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ class << self
attr_accessor :page, :type
end

def self.attributes(new_attributes)
undefine_class_methods(self)
undefine_instance_methods(self)
@attributes = new_attributes
define_class_methods(self)
define_instance_methods(self)
end

def self.all(selector = "", filter = "")
records = []
Expand Down Expand Up @@ -34,20 +41,20 @@ def self.find_by_attribute(attribute, value, selector, filter)
end
end

# def self.method_missing(action)
# raw_action = /(.*)\?/.match(action)
# begin
# if raw_action
# raw_action_for(raw_action[0])
# else
# action_for(action)
# end
# rescue Capybara::Ambiguous
# raise MultipleRecords, "Found multiple #{action} tags for #{@type} on page"
# rescue Capybara::ElementNotFound
# super
# end
# end
def self.method_missing(action)
raw_action = /(.*)\?/.match(action)
begin
if raw_action
raw_action_for(raw_action[0])
else
action_for(action)
end
rescue Capybara::Ambiguous
raise MultipleRecords, "Found multiple #{action} tags for #{@type} on page"
rescue Capybara::ElementNotFound
super
end
end


def self.inherited(base)
Expand Down Expand Up @@ -92,24 +99,59 @@ def self.define_accessor_methods(base)
end


def self.undefine_accessor_methods(base)
base.instance_eval do
@attributes.each do | attribute |
remove_method("#{attribute}_raw")
remove_method(attribute)
remove_method("#{attribute}=")
end
end
end

def self.undefine_class_methods(base)
eigenclass = class << base; self; end
attributes = base.instance_variable_get('@attributes')
eigenclass.instance_eval do
attributes.each do | attribute|
remove_method "find_by_#{attribute}"
end
end
end



def self.define_instance_methods(base)
define_accessor_methods(base)
end

def self.undefine_instance_methods(base)
undefine_accessor_methods(base)
end


def self.define_class_methods(base)
eigenclass = class << base; self; end
attributes = base.instance_variable_get('@attributes')
eigenclass.instance_eval do
attributes.each do | attribute|
define_method "find_by_#{attribute}" do | value, selector = "", filter = ""|
debugger
find_by_attribute( attribute, value, selector, filter)
end
end
end
end

def self.undefine_class_methods(base)
eigenclass = class << base; self; end
attributes = base.instance_variable_get('@attributes')
eigenclass.instance_eval do
attributes.each do | attribute|
remove_method "find_by_#{attribute}"
end
end
end


def self.context_for_selector(selector)
if selector.blank?
Expand Down
40 changes: 39 additions & 1 deletion spec/page_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,44 @@ class TeamPage < PageRecord::PageRecord; end
TeamPage.page = page
end


describe ".attributes" do
before do
class TeamPage < PageRecord::PageRecord
attributes ['country', 'stadium']
end
end

after do
Object.send(:remove_const, :TeamPage)
end

subject { TeamPage}

it "clears all old class methods" do
expect(subject).not_to respond_to(:find_by_name)
expect(subject).not_to respond_to(:find_by_ranking)
end

it "adds new class methods to class " do
expect(subject).to respond_to(:find_by_country)
expect(subject).to respond_to(:find_by_stadium)
end

it "clears all old instance methods" do
expect(subject.new(1)).not_to respond_to(:name)
expect(subject.new(1)).not_to respond_to(:ranking)
end

it "adds new class methods to class " do
expect(subject.new(1)).to respond_to(:country)
expect(subject.new(1)).to respond_to(:stadium)
end

end



describe ".page=" do
before do
TeamPage.page = nil # reset for the spec
Expand Down Expand Up @@ -201,7 +239,7 @@ class TeamPage < PageRecord::PageRecord; end

describe "find_by..." do

subject { debugger;TeamPage.find_by_name(name, selector, filter)}
subject { TeamPage.find_by_name(name, selector, filter)}
let(:selector) { ""}
let(:filter) {""}

Expand Down

0 comments on commit 7220c87

Please sign in to comment.