Method: Insite::DefinedPage#initialize

Defined in:
lib/insite/page/defined_page.rb

#initialize(site, args = nil) ⇒ DefinedPage

Initializes a new page object. There’s no need to ever call this method directly. Your site class (the one that includes the Insite module) will handle this for you



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/insite/page/defined_page.rb', line 242

def initialize(site, args = nil)
  @site               = site
  @browser            = process_browser

  @component_elements = self.class.component_elements ||= []
  @browser            = @site.browser
  @page_attributes    = self.class.page_attributes
  @page_url           = self.class.page_url
  @page_elements      = self.class.page_elements
  # TODO: Clean this up
  @page_features      = self.class.instance_variable_get(:@page_features)
  @all_arguments      = self.class.arguments
  @required_arguments = self.class.required_arguments
  @url_matcher        = self.class.url_matcher
  @url_template       = self.class.url_template
  @has_fragment       = self.class.has_fragment

  # Try to expand the URL template if the URL has parameters. Stores the
  # param list that will expand the url_template after examining the
  # arguments used to initialize the page. (Start with an empty hash and
  # then build it out.)
  @arguments = {}.with_indifferent_access

  match = @url_template.match(@browser.url)

  if @all_arguments.present? && !args
    @all_arguments.each do |arg|
      if match && match.keys.include?(arg.to_s)
        @arguments[arg] = match[arg.to_s]
      elsif @site.arguments[arg]
        @arguments[arg] = @site.arguments[arg]
      elsif @site.respond_to?(arg) && @site.public_send(arg)
        @arguments[arg] = @site.public_send(arg)
      elsif !@required_arguments.include?(arg)
        @arguments[arg] = nil
      else
        raise(
          Insite::Errors::PageInitError,
          "An error occurred when attempting to build a URL for the #{self.class} page.\n" \
          "URL template:\t#{@url_template.pattern}\n" \
          "URL arguments:\t#{@arguments}\n" \
          "Required args:\t#{@required_arguments.join(', :')}.\n" \
          "Generated URL:\t#{@url_template.expand(@arguments)}\n"
        )
      end
    end
  elsif @all_arguments.present?
    @all_arguments.each do |arg| # Try to extract each URL argument from the hash or object provided, OR from the site object.
      if args.is_a?(Hash) && args.present?
        if @arguments[arg] #The hash has the required argument.
          @arguments[arg]= args[arg]
        elsif match && match.keys.include?(arg.to_s)
          @arguments[arg] = match[arg.to_s]
        elsif @site.respond_to?(arg)
          @arguments[arg] = site.public_send(arg)
        elsif @site.arguments[arg]
          @arguments[arg] = @site.arguments[arg]
        elsif !@required_arguments.include?(arg)
          @arguments[arg] = nil
        else
          raise(
            Insite::Errors::PageInitError,
            "An error occurred when attempting to build a URL for the #{self.class} page.\n" \
            "URL template:\t#{@url_template.pattern}\n" \
            "URL arguments:\t#{@arguments}\n" \
            "Required args:\t#{@required_arguments.join(', :')}.\n" \
            "Generated URL:\t#{@url_template.expand(@arguments)}\n"
          )
        end
      elsif args # Some non-hash object was provided.
        if args.respond_to?(arg) #The hash has the required argument.
          @arguments[arg]= args.public_send(arg)
        elsif @site.respond_to?(arg)
          @arguments[arg]= site.public_send(arg)
        elsif @site.arguments[arg]
          @arguments[arg] = @site.arguments[arg]
        elsif !@required_arguments.include?(arg)
          @arguments[arg] = nil
        else
          raise(
            Insite::Errors::PageInitError,
            "An error occurred when attempting to build a URL for the #{self.class} page.\n" \
            "URL template:\t#{@url_template.pattern}\n" \
            "URL arguments:\t#{@arguments}\n" \
            "Required args:\t#{@required_arguments.join(', :')}.\n" \
            "Generated URL:\t#{@url_template.expand(@arguments)}\n"
          )
        end
      else
        # Do nothing here yet.
      end
    end
  else
    # Do nothing here yet.
  end

  @url = @url_template.expand(@arguments).to_s
  @page_features ||= []
  @page_features.each do |fname|
    begin
      klass = fname.to_s.camelize.constantize
    rescue NameError => e
      klass = self.class.const_get fname.to_s.camelize
    end

    self.class_eval do
      if klass.alias
        define_method(klass.alias) do
          klass.new(self)
        end
      else
        define_method(fname) do
          klass.new(self)
        end
      end
    end
  end

  @site.most_recent_page = self
  unless on_page?
    if navigation_disabled?
      raise(
        Insite::Errors::PageNavigationNotAllowedError,
        "Navigation is intentionally disabled for the #{self.class.name} page. " \
        "You can only call the accessor method for this page when it's already " \
        "being displayed in the browser.\n\nCurrent URL:" \
        "\n------------\n#{@site.browser.url}\n\n#{caller.join("\n")}"
      )
    end

    visit
  end
end