Class: AlPersist

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

Overview

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

Direct Known Subclasses

AlPersistFile, AlPersistRDB, AlPersistSh

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, keys = nil) ⇒ AlPersist

constructor

Parameters:

  • base (AlRdbw)

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

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

    プライマリキー



40
41
42
43
44
45
46
# File 'lib/al_persist.rb', line 40

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

Instance Attribute Details

#persist_baseAlRdbw (readonly)

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

Returns:

  • (AlRdbw)

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



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

def persist_base
  @persist_base
end

#pkeysArray<Symbol> (readonly)

Returns プライマリキーの配列.

Returns:

  • (Array<Symbol>)

    プライマリキーの配列



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

def pkeys
  @pkeys
end

#resultHash (readonly)

Returns CxUDメソッドの実行結果(DBwrapperからの結果保存).

Returns:

  • (Hash)

    CxUDメソッドの実行結果(DBwrapperからの結果保存)



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

def result
  @result
end

#search_conditionHash (readonly)

Returns 検索条件のキャッシュ.

Returns:

  • (Hash)

    検索条件のキャッシュ



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

def search_condition
  @search_condition
end

#valuesHash

Returns 管理するアトリビュート.

Returns:

  • (Hash)

    管理するアトリビュート



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

def values
  @values
end

Instance Method Details

#[](k) ⇒ String

attribute getter

Parameters:

  • k (Symbol)

    キー

Returns:

  • (String)



81
82
83
# File 'lib/al_persist.rb', line 81

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

#[]=(k, v) ⇒ Object

attribute setter

Parameters:

  • k (Symbol)

    キー

  • v (String)



92
93
94
# File 'lib/al_persist.rb', line 92

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

#get_next_offsetInteger, Nil

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

Returns:

  • (Integer, Nil)

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



102
103
104
105
# File 'lib/al_persist.rb', line 102

def get_next_offset()
  return nil  if ! @search_condition[:has_next_page]
  return @search_condition[:offset].to_i + @search_condition[:limit].to_i
end

#get_previous_offsetInteger, Nil

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

Returns:

  • (Integer, Nil)

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



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

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

  return nil  if offset <= 0
  return prev < 0 ? 0 : prev
end

#pkey(*keys) ⇒ self

primary key setter

Parameters:

  • keys (Symbol, String)

    プライマリキー

Returns:

  • (self)

    selfオブジェクト



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/al_persist.rb', line 55

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