Class: Miby::PageStruct

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

Overview

ページ整形情報データベース。取得可能なページの生成方法を管理します。

ページ整形情報

Miby::Pageでページを整形し必要な値を取得するための設定が定義されています。以下のパラメータがあります(YAML形式)。

name:               # 定義名。Miby::Pageなどからはこの名前でアクセスする
  path: hoge.html   # Miby::HTTP の http_address 以下のパス。
  params:           # URLの必須パラメータ
    - id
    - owner_id
  params_opt:       # 必須でないURLパラメータ
    - page
  css_selectors:          # HTMLパーサ Hpricot を使ってブロックを絞り込むタグ
    - div.viewDiary
    - du.diary
  regexp: dt>(.*?)<.*dd>(.*?)<\/dd>.*?>(.*?)<\/ # 値を取得する為の正規表現。後方参照(.*?)を取得する。
  variable:        # regexpで取得した値を順番に代入する
    - title
    - date
    - body

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(struct_file = PAGESTRUCT_FILE) ⇒ PageStruct

Returns a new instance of PageStruct.



31
32
33
34
# File 'lib/pagestruct.rb', line 31

def initialize(struct_file = PAGESTRUCT_FILE)
  @struct_file = struct_file
  @struct = load
end

Instance Attribute Details

#structObject (readonly)

読み込んだページ情報定義ファイルの内容。



40
41
42
# File 'lib/pagestruct.rb', line 40

def struct
  @struct
end

#struct_fileObject (readonly)

ページ情報を定義しているファイル。通常はYAMLファイル。定数 PAGESTRUCT_FILE で指定されている。



37
38
39
# File 'lib/pagestruct.rb', line 37

def struct_file
  @struct_file
end

Class Method Details

.find(name) ⇒ Object

name で指定されたページ整形情報を返します。取得できない場合は例外が発生します。



43
44
45
# File 'lib/pagestruct.rb', line 43

def PageStruct.find(name)
  self.new.find(name)
end

Instance Method Details

#find_by_name(name) ⇒ Object Also known as: find

name で指定されたページ整形情報を返します。取得できない場合は例外が発生します。



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

def find_by_name(name)
  begin
    @struct.fetch name.to_s
  rescue IndexError
    raise PageStructNotFoundError, "Not found struct name: #{name}."
  end
end

#inspectObject

ページ情報定義ファイルの内容を返します。



57
58
59
# File 'lib/pagestruct.rb', line 57

def inspect
  @struct
end