Class: AlPersist

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

Overview

データ永続化 ベースクラス

Direct Known Subclasses

AlPersistFile, AlPersistRDB

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (AlPersist) initialize(base, keys = nil)

constructor

Parameters:

  • (AlRdbw) base

    使用する RDB wrapper オブジェクト

  • (Array<String,Symbol>, String, Symbol) keys (defaults to: nil)

    プライマリキー



36
37
38
39
40
41
# File 'lib/al_persist.rb', line 36

def initialize( base, keys = nil )
  @persist_base = base
  @values = {}
  pkey( keys )
  @search_condition = {}
end

Instance Attribute Details

- (AlRdbw) persist_base (readonly)

使用する RDB wrapper オブジェクト

Returns:

  • (AlRdbw)

    使用する RDB wrapper オブジェクト



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

def persist_base
  @persist_base
end

- (Array<Symbol>) pkeys (readonly)

プライマリキーの配列

Returns:

  • (Array<Symbol>)

    プライマリキーの配列



24
25
26
# File 'lib/al_persist.rb', line 24

def pkeys
  @pkeys
end

- (Hash) search_condition (readonly)

検索条件のキャッシュ

Returns:

  • (Hash)

    検索条件のキャッシュ



27
28
29
# File 'lib/al_persist.rb', line 27

def search_condition
  @search_condition
end

- (Hash) values

管理するアトリビュート

Returns:

  • (Hash)

    管理するアトリビュート



21
22
23
# File 'lib/al_persist.rb', line 21

def values
  @values
end

Instance Method Details

- (String) [](k)

attribute getter

Parameters:

  • (Symbol) k

    キー

Returns:

  • (String)



76
77
78
# File 'lib/al_persist.rb', line 76

def []( k )
  return @values[k.to_sym]
end

- (Object) []=(k, v)

attribute setter

Parameters:

  • (Symbol) k

    キー

  • (String) v



87
88
89
# File 'lib/al_persist.rb', line 87

def []=( k, v )
  @values[k.to_sym] = v
end

- (Integer, Nil) get_next_offset

検索時、次ページのオフセット値を得る

Returns:

  • (Integer, Nil)

    オフセット値。次ページがなければnil。



97
98
99
100
101
102
103
104
# File 'lib/al_persist.rb', line 97

def get_next_offset()
  total_rows = @search_condition[:total_rows].to_i
  num_rows = @search_condition[:num_rows].to_i
  limit = @search_condition[:limit].to_i
  offset = @search_condition[:offset].to_i

  return (offset + num_rows) < total_rows ? (offset + limit) : nil
end

- (Integer, Nil) get_previous_offset

検索時、前ページのオフセット値を得る

Returns:

  • (Integer, Nil)

    オフセット値。前ページがなければnil。



112
113
114
115
116
117
# File 'lib/al_persist.rb', line 112

def get_previous_offset()
  limit = @search_condition[:limit].to_i
  offset = @search_condition[:offset].to_i

  return offset > 0 ? (offset - limit) : nil
end

- (self) pkey(*keys)

primary key setter

Parameters:

  • (Symbol, String) keys

    プライマリキー

Returns:

  • (self)

    selfオブジェクト



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/al_persist.rb', line 50

def pkey( *keys )
  @pkeys = []

  keys.flatten.each do |k|
    case k
    when String
      @pkeys << k.to_sym
    when Symbol
      @pkeys << k
    when NilClass
      # nothing
    else
      raise 'Needs key by String or Symbol'
    end
  end

  return self
end