Class: Basket::Element

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

Defined Under Namespace

Classes: InvalidElement

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, id = SecureRandom.uuid) ⇒ Element

Returns a new instance of Element.

Raises:



19
20
21
22
23
24
# File 'lib/basket/element.rb', line 19

def initialize(data, id = SecureRandom.uuid)
  raise InvalidElement, "both data and id must be present" unless data && id

  @data = data
  @id = id
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/basket/element.rb', line 7

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/basket/element.rb', line 7

def id
  @id
end

Class Method Details

.from_queue(element) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/basket/element.rb', line 9

def self.from_queue(element)
  if element.is_a?(Element)
    element
  elsif element.is_a?(Hash)
    new(element["data"], element["id"])
  else
    raise InvalidElement, "element must be a hash or a Basket::Element"
  end
end

Instance Method Details

#==(other) ⇒ Object



34
35
36
# File 'lib/basket/element.rb', line 34

def ==(other)
  to_h == other.to_h
end

#to_hObject



26
27
28
# File 'lib/basket/element.rb', line 26

def to_h
  {data: data, id: id}
end

#to_jsonObject



30
31
32
# File 'lib/basket/element.rb', line 30

def to_json(*)
  to_h.to_json
end