Class: Pixiv::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/pixiv/page.rb

Direct Known Subclasses

Illust, IllustList, Member

Defined Under Namespace

Modules: WithClient

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc, attrs = {}) ⇒ Page #initialize(doc_creator, attrs = {}) ⇒ Page

Returns a new instance of Page.

Overloads:

  • #initialize(doc, attrs = {}) ⇒ Page

    Parameters:

    • doc (Nokogiri::XML::Document)
    • attrs (Hash{Symbol=>Object}) (defaults to: {})
  • #initialize(doc_creator, attrs = {}) ⇒ Page

    Parameters:

    • doc_creator (#call)
    • attrs (Hash{Symbol=>Object}) (defaults to: {})

19
20
21
22
23
24
25
26
27
# File 'lib/pixiv/page.rb', line 19

def initialize(doc_or_doc_creator, attrs = {})
  x = doc_or_doc_creator
  if x.respond_to?(:call)
    @doc_creator = x
  else
    @doc = x
  end
  set_attrs!(attrs)
end

Class Method Details

.lazy_new(attrs = {}, &doc_creator) ⇒ Object

A new Page

Parameters:

  • attrs (Hash{Symbol=>Object}) (defaults to: {})

Yield Returns:

  • (Nokogiri::XML::Document)

9
10
11
# File 'lib/pixiv/page.rb', line 9

def self.lazy_new(attrs = {}, &doc_creator)
  self.new(doc_creator, attrs)
end

Instance Method Details

#bind(client) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Bind self to client

Returns:

  • self


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pixiv/page.rb', line 58

def bind(client)
  if self.class.const_defined?(:WithClient)
    mod = self.class.const_get(:WithClient)
  else
    mod = Page::WithClient
  end
  unless singleton_class.include?(mod)
    extend(mod)
  end
  self.client = client
  self
end

#docNokogiri::XML::Document

Returns:

  • (Nokogiri::XML::Document)

37
38
39
40
41
42
43
# File 'lib/pixiv/page.rb', line 37

def doc
  @doc ||= begin
             doc = @doc_creator.call
             @doc_creator = nil
             doc
           end
end

#fetched?(attr_name = :doc) ⇒ Boolean

Whether attr_name is fetched or not

Parameters:

  • attr_name (String, Symbol) (defaults to: :doc)

Returns:

  • (Boolean)

32
33
34
# File 'lib/pixiv/page.rb', line 32

def fetched?(attr_name = :doc)
  instance_variable_defined?(:"@#{attr_name}")
end

#forceself

Fetch #doc and lazy attrs

Returns:

  • (self)

47
48
49
50
51
52
53
# File 'lib/pixiv/page.rb', line 47

def force
  doc
  (@@lazy_attr_readers || []).each do |reader|
    __send__(reader) if respond_to?(reader)
  end
  self
end