Class: Cequel::Model::Dictionary

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cequel/model/dictionary.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Dictionary

Returns a new instance of Dictionary.



63
64
65
66
# File 'lib/cequel/model/dictionary.rb', line 63

def initialize(key)
  @key = key
  setup
end

Class Attribute Details

.column_familyObject



41
42
43
44
45
# File 'lib/cequel/model/dictionary.rb', line 41

def column_family
  return @column_family if @column_family
  self.column_family_name = name.underscore.to_sym
  @column_family
end

.default_batch_sizeObject



51
52
53
# File 'lib/cequel/model/dictionary.rb', line 51

def default_batch_size
  @default_batch_size || 1000
end

Class Method Details

.[](key) ⇒ Object



55
56
57
# File 'lib/cequel/model/dictionary.rb', line 55

def [](key)
  new(key)
end

.column_family_name=(column_family_name) ⇒ Object



47
48
49
# File 'lib/cequel/model/dictionary.rb', line 47

def column_family_name=(column_family_name)
  self.column_family = Cequel::Model.keyspace[column_family_name]
end

.comparatorObject



19
20
21
# File 'lib/cequel/model/dictionary.rb', line 19

def comparator
  @comparator ||= :text
end

.key(key_alias, type) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/cequel/model/dictionary.rb', line 27

def key(key_alias, type)
  @key_alias, @key_type = key_alias, type

  module_eval(<<-RUBY)
  def #{key_alias.downcase}
    @key
  end
  RUBY
end

.key_aliasObject



11
12
13
# File 'lib/cequel/model/dictionary.rb', line 11

def key_alias
  @key_alias ||= :KEY
end

.key_typeObject



15
16
17
# File 'lib/cequel/model/dictionary.rb', line 15

def key_type
  @key_type ||= :text
end

.maps(options) ⇒ Object



37
38
39
# File 'lib/cequel/model/dictionary.rb', line 37

def maps(options)
  @comparator, @validation = *options.first
end

.validationObject



23
24
25
# File 'lib/cequel/model/dictionary.rb', line 23

def validation
  @validation ||= :text
end

Instance Method Details

#[](column) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/cequel/model/dictionary.rb', line 79

def [](column)
  if @loaded || @changed_columns.include?(column)
    @row[column]
  elsif !@deleted_columns.include?(column)
    value = scope.select(column).first[column]
    deserialize_value(column, value) if value
  end
end

#[]=(column, value) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/cequel/model/dictionary.rb', line 68

def []=(column, value)
  if value.nil?
    @deleted_columns << column
    @changed_columns.delete(column)
  else
    @changed_columns << column
    @deleted_columns.delete(column)
  end
  @row[column] = value
end

#destroyObject



108
109
110
111
# File 'lib/cequel/model/dictionary.rb', line 108

def destroy
  scope.delete
  setup
end

#each(&block) ⇒ Object



145
146
147
# File 'lib/cequel/model/dictionary.rb', line 145

def each(&block)
  each_pair(&block)
end

#each_pair(options = {}, &block) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cequel/model/dictionary.rb', line 125

def each_pair(options = {}, &block)
  return Enumerator.new(self, :each_pair, options) unless block
  return @row.each_pair(&block) if @loaded
  new_columns = @changed_columns.dup
  batch_size = options[:batch_size] || self.class.default_batch_size
  each_slice(batch_size) do |batch_results|
    batch_results.each_pair do |key, value|
      if @changed_columns.include?(key)
        new_columns.delete(key)
        yield key, @row[key]
      elsif !@deleted_columns.include?(key)
        yield key, value
      end
    end
  end
  new_columns.each do |key|
    yield key, @row[key]
  end
end

#each_slice(batch_size) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cequel/model/dictionary.rb', line 149

def each_slice(batch_size)
  batch_scope = scope.select(:first => batch_size)
  key_alias = self.class.key_alias
  last_key = nil
  begin
    batch_results = batch_scope.first
    batch_results.delete(key_alias)
    result_length = batch_results.length
    batch_results.delete(last_key) unless last_key.nil?
    yield deserialize_row(batch_results)
    last_key = batch_results.keys.last
    batch_scope = batch_scope.select(:from => last_key)
  end while result_length == batch_size
end

#keysObject



88
89
90
# File 'lib/cequel/model/dictionary.rb', line 88

def keys
  @loaded ? @row.keys : each_pair.map { |key, value| key }
end

#loadObject



164
165
166
167
168
169
# File 'lib/cequel/model/dictionary.rb', line 164

def load
  @row = {}
  each_pair { |column, value| @row[column] = value }
  @loaded = true
  self
end

#loaded?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/cequel/model/dictionary.rb', line 171

def loaded?
  !!@loaded
end

#saveObject



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cequel/model/dictionary.rb', line 113

def save
  updates = {}
  @changed_columns.each do |column|
    updates[column] = serialize_value(@row[column])
  end
  scope.update(updates) if updates.any?
  scope.delete(*@deleted_columns.to_a) if @deleted_columns.any?
  @changed_columns.clear
  @deleted_columns.clear
  self
end

#slice(*columns) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cequel/model/dictionary.rb', line 96

def slice(*columns)
  if @loaded
    @row.slice(*columns)
  else
    row = scope.select(*columns).first.except(self.class.key_alias)
    deserialize_row(row).tap do |slice|
      slice.merge!(@row.slice(*columns))
      @deleted_columns.each { |column| slice.delete(column) }
    end
  end
end

#valuesObject



92
93
94
# File 'lib/cequel/model/dictionary.rb', line 92

def values
  @loaded ? @row.values : each_pair.map { |key, value| value }
end