Module: AePageObjects::Dsl

Includes:
InternalHelpers
Included in:
Node
Defined in:
lib/ae_page_objects/core/dsl.rb

Instance Method Summary collapse

Methods included from InternalHelpers

#ensure_class_for_param!

Instance Method Details

#collection(name, options = {}, &block) ⇒ Object

Defines a collection of elements. Blocks are evaluated on the item class used by the collection. collection() defines a method on the class that returns an instance of a collection class which contains instances of the collection's item class.

Supported signatures are described below.


Signature: (no :is, no :contains, no block)

collection :addresses

Collection class: Collection
Item class:       Element

Signature: (no :is, no :contains, block)

collection :addresses do
  element :city
  element :state
end

Collection class: one-off subclass of Collection
Item class:       one-off subclass of Element
Methods defined on item class:
city()  # -> instance of Element
state() # -> instance of Element

Signature: (no :is, :contains, no block)

collection :addresses, :contains => Address

Collection class: one-off subclass of Collection
Item class:       Address

Signature: (no :is, :contains, block)

collection :addresses, :contains => Address do
element :longitude
element :latitude
end

Collection class: one-off subclass of Collection  element
Item class:       one-off subclass of Address
Methods defined on item class:
longitude()  # -> instance of Element
latitude() # -> instance of Element

Signature: (:is, no :contains, no block)

collection :addresses, :is => AddressList

Collection class: AddressList
Item class:       AddressList.item_class

Signature: (:is, no :contains, block)

collection :addresses, :is => AddressList do
element :longitude
element :latitude
end

Collection class: one-off subclass of AddressList
Item class:       one-off subclass of AddressList.item_class
Methods defined on item class:
longitude()  # -> instance of Element
latitude() # -> instance of Element

Signature: (:is, :contains, no block)

collection :addresses, :is => AddressList, :contains => ExtendedAddress

Collection class: one-off subclass ofAddressList
Item class:       ExtendedAddress

Signature: (:is, :contains, block)

collection :addresses, :is => AddressList, :contains => Address do
element :longitude
element :latitude
end

Collection class: one-off subclass of AddressList
Item class:       one-off subclass of Address
Methods defined on item class:
longitude()  # -> instance of Element
latitude() # -> instance of Element


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ae_page_objects/core/dsl.rb', line 132

def collection(name, options = {}, &block)
  options ||= {}

  # only a collection class is specified or the item class
  # specified matches the collection's item class
  if ! block_given? && options[:is] && (
  options[:contains].nil? || options[:is].item_class == options[:contains]
  )
    return element(name, options)
  end

  options = options.dup

  # create/get the collection class
  if options[:is]
    ensure_class_for_param!(:is, options[:is], Collection)
  else
    options[:is] = Collection
  end

  item_class = options.delete(:contains) || options[:is].item_class
  if block_given?
    item_class = Class.new(item_class, &block).tap do |new_item_class|
      new_item_class.element_attributes.merge!(item_class.element_attributes)
    end
  end

  # since we are creating a new item class, we need to subclass the collection class
  # so we can parameterize the collection class with an item class
  options[:is] = Class.new(options[:is])
  options[:is].item_class = item_class

  element(name, options)
end

#element(name, options = {}, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ae_page_objects/core/dsl.rb', line 18

def element(name, options = {}, &block)
  options        = options.dup
  options[:name] ||= name
  options[:is]   ||= Element

  if block_given?
    options[:is] = Class.new(options[:is], &block)
  end

  element_class = options[:is]

  self.element_attributes[name.to_sym] = element_class

  define_method name do
    self.element(options)
  end

  element_class
end

#form_for(form_name, options = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ae_page_objects/core/dsl.rb', line 167

def form_for(form_name, options = {}, &block)
  options ||= {}

  raise ArgumentError, ":is option not supported" if options[:is]
  raise ArgumentError, "Block required." if block.nil?

  klass = Class.new(Form, &block)

  options      = options.dup
  options[:is] = klass

  element(form_name, options)

  klass.element_attributes.each do |element_name, element_klazz|
    class_eval <<-RUBY
      def #{element_name}(*args, &block)
        #{form_name}.#{element_name}(*args, &block)
      end
    RUBY

    self.element_attributes[element_name] = element_klazz
  end
end

#inherited(subclass) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/ae_page_objects/core/dsl.rb', line 8

def inherited(subclass)
  subclass.class_eval do
    class << self
      def element_attributes
        @element_attributes ||= {}
      end
    end
  end
end