Class: PageSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/acceptance_test/page_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, smart_completion = true) ⇒ PageSet

Returns a new instance of PageSet.



12
13
14
15
16
# File 'lib/acceptance_test/page_set.rb', line 12

def initialize context, smart_completion=true
  @context = context
  @smart_completion = smart_completion
  @pages = []
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



9
10
11
# File 'lib/acceptance_test/page_set.rb', line 9

def context
  @context
end

#pagesObject (readonly)

Returns the value of attribute pages.



10
11
12
# File 'lib/acceptance_test/page_set.rb', line 10

def pages
  @pages
end

Instance Method Details

#delegate_to_page(page) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/acceptance_test/page_set.rb', line 34

def delegate_to_page page
  pages << page

  self.class.send :attr_reader, page

  self.class.def_delegators page, *page_methods(page)
end

#delegate_to_pages(*pages) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/acceptance_test/page_set.rb', line 26

def delegate_to_pages *pages
  pages.each do |page|
    delegate_to_page page
  end

  enable_smart_completion if @smart_completion
end

#detect_pages(scope) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/acceptance_test/page_set.rb', line 78

def detect_pages scope
  pages_classes = scope.constants.select do |class_name|
    clazz = scope.const_get(class_name)

    clazz.ancestors.include? Page
  end

  pages_classes.collect do |class_name|
    clazz = scope.const_get(class_name)

    object = clazz.new self

    name = underscore(class_name.to_s)

    self.instance_variable_set("@#{name}", object)

    name
  end
end

#enable_smart_completionObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/acceptance_test/page_set.rb', line 48

def enable_smart_completion
  context.class.send(:define_method, :method_missing) do |method_name, *args, &block|
    page_set.send method_name, *args, &block
  end

  page_set = self

  PageSet.class_eval do
    @page_set = page_set # class instance variable

    def self.page_set # access to class instance variable
      @page_set
    end
  end

  pages.each do |page|
    page_methods(page).each do |method_name|
      method = page_set.method(method_name)

      context.class.step "I #{method_name.to_s.gsub('_', ' ')}" do |*args|
        page_set.send method_name, *args
      end

      context.class.step "#{method.to_s.gsub('_', ' ')}" do
        page_set.send method_name, *args
      end
    end
  end
end

#execute(&code) ⇒ Object



22
23
24
# File 'lib/acceptance_test/page_set.rb', line 22

def execute &code
  MetaMethods::DslBuilder.instance.evaluate_dsl(self, nil, code)
end

#page_methods(page) ⇒ Object



42
43
44
45
46
# File 'lib/acceptance_test/page_set.rb', line 42

def page_methods page
  clazz = self.send(page).class

  clazz.instance_methods - Page.instance_methods
end

#sessionObject



18
19
20
# File 'lib/acceptance_test/page_set.rb', line 18

def session
  context.page
end