Class: Shameless::Cell

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

Constant Summary collapse

BASE =
'base'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, values = nil) ⇒ Cell

Returns a new instance of Cell.



18
19
20
21
22
23
# File 'lib/shameless/cell.rb', line 18

def initialize(model, name, values = nil)
  @model = model
  @name = name
  reload
  initialize_from_values(values)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'lib/shameless/cell.rb', line 16

def id
  @id
end

#modelObject (readonly)

Returns the value of attribute model.



16
17
18
# File 'lib/shameless/cell.rb', line 16

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/shameless/cell.rb', line 16

def name
  @name
end

Class Method Details

.base(model, body) ⇒ Object



7
8
9
10
# File 'lib/shameless/cell.rb', line 7

def self.base(model, body)
  serialized_body = serialize_body(body)
  new(model, BASE, body: serialized_body)
end

.serialize_body(body) ⇒ Object



12
13
14
# File 'lib/shameless/cell.rb', line 12

def self.serialize_body(body)
  MessagePack.pack(body)
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  body[key.to_s]
end

#[]=(key, value) ⇒ Object



29
30
31
32
# File 'lib/shameless/cell.rb', line 29

def []=(key, value)
  @model.prevent_readonly_attribute_mutation!(key)
  body[key.to_s] = value
end

#as_jsonObject



89
90
91
# File 'lib/shameless/cell.rb', line 89

def as_json(*)
  cell_values(false).merge(id: id)
end

#bodyObject



61
62
63
64
# File 'lib/shameless/cell.rb', line 61

def body
  load
  @body
end

#created_atObject



56
57
58
59
# File 'lib/shameless/cell.rb', line 56

def created_at
  load
  @created_at
end

#fetch(key, default) ⇒ Object



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

def fetch(key, default)
  body.key?(key.to_s) ? self[key] : default
end

#present?Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/shameless/cell.rb', line 80

def present?
  load
  !@ref_key.nil?
end

#previousObject



66
67
68
69
70
# File 'lib/shameless/cell.rb', line 66

def previous
  if ref_key && previous_cell_values = @model.fetch_cell(@name, ref_key - 1)
    self.class.new(@model, @name, previous_cell_values)
  end
end

#ref_keyObject



51
52
53
54
# File 'lib/shameless/cell.rb', line 51

def ref_key
  load
  @ref_key
end

#reloadObject



72
73
74
# File 'lib/shameless/cell.rb', line 72

def reload
  @id = @body = @ref_key = @created_at = nil
end

#saveObject



34
35
36
37
38
39
40
41
# File 'lib/shameless/cell.rb', line 34

def save
  load
  @created_at = Time.now
  @created_at = (@created_at.to_f * 1000).to_i if @model.class.store.configuration.legacy_created_at_is_bigint
  @ref_key ||= -1
  @ref_key += 1
  @id = @model.put_cell(cell_values(true))
end

#update(values) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/shameless/cell.rb', line 43

def update(values)
  values.each do |key, value|
    self[key] = value
  end

  save
end

#uuidObject



85
86
87
# File 'lib/shameless/cell.rb', line 85

def uuid
  @model.uuid
end