Class: Basket::Element
- Inherits:
-
Object
- Object
- Basket::Element
- Defined in:
- lib/basket/element.rb
Defined Under Namespace
Classes: InvalidElement
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(data, id = SecureRandom.uuid) ⇒ Element
constructor
A new instance of Element.
- #to_h ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(data, id = SecureRandom.uuid) ⇒ Element
Returns a new instance of Element.
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/basket/element.rb', line 7 def data @data end |
#id ⇒ Object (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_h ⇒ Object
26 27 28 |
# File 'lib/basket/element.rb', line 26 def to_h {data: data, id: id} end |
#to_json ⇒ Object
30 31 32 |
# File 'lib/basket/element.rb', line 30 def to_json(*) to_h.to_json end |