Class: Saper::Items::Atom

Inherits:
Saper::Item show all
Defined in:
lib/saper/items/atom.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Saper::Item

[], exists?, inherited, subclasses, try, type, #type

Constructor Details

#initialize(hash) ⇒ Atom

TODO:

Returns a new instance of Atom


16
17
18
# File 'lib/saper/items/atom.rb', line 16

def initialize(hash)
  @atts = to_native(hash)
end

Class Method Details

.new(item) ⇒ Object

TODO:

6
7
8
9
10
11
12
13
# File 'lib/saper/items/atom.rb', line 6

def self.new(item)
  super case item
  when Hash
    item
  else
    raise(Errors::InvalidItem, item)
  end
end

Instance Method Details

#==(other) ⇒ Object

TODO:

21
22
23
# File 'lib/saper/items/atom.rb', line 21

def ==(other)
  to_hash == other
end

#[](name) ⇒ Object

TODO:

31
32
33
# File 'lib/saper/items/atom.rb', line 31

def [](name)
  @atts[name]
end

#serializeObject

TODO:

36
37
38
# File 'lib/saper/items/atom.rb', line 36

def serialize
  Hash[@atts.map { |key, value| [key, value.respond_to?(:serialize) ? value.serialize : value] }]
end

#to_hashObject

TODO:

26
27
28
# File 'lib/saper/items/atom.rb', line 26

def to_hash
  @atts.dup
end

#to_jsonObject

TODO:

58
59
60
# File 'lib/saper/items/atom.rb', line 58

def to_json
  JSON.new(@hash, true)
end

#to_native(object = nil) ⇒ Object

TODO:

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/saper/items/atom.rb', line 41

def to_native(object = nil)
  if object.nil?
    return to_native(to_hash)
  end
  if object.is_a?(Hash)
    return Hash[object.map { |key, value| [key, to_native(value)] }]
  end
  if object.is_a?(Array)
    return object.map { |value| to_native(value) }
  end
  if object.is_a?(Item)
    return object.to_native
  end
  object
end

#to_sObject

TODO:

63
64
65
# File 'lib/saper/items/atom.rb', line 63

def to_s
  to_json
end