Class: Uberous::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/uberous/uber.rb

Constant Summary collapse

ATTR_NAMES =
[:id, :name, :rel, :url, :action, :transclude, :model, :sending, :accepting, :value].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Data

Returns a new instance of Data.



36
37
38
39
# File 'lib/uberous/uber.rb', line 36

def initialize(attrs = {})
  @attrs = ATTR_NAMES.each_with_object({}) {|name, h| h[name] = attrs[name] if attrs.has_key?(name) } # slice
  @data_collection = Array(attrs[:data_collection])
end

Instance Attribute Details

#data_collectionObject (readonly)

Returns the value of attribute data_collection.



35
36
37
# File 'lib/uberous/uber.rb', line 35

def data_collection
  @data_collection
end

Instance Method Details

#add_data(data) ⇒ Object



50
51
52
# File 'lib/uberous/uber.rb', line 50

def add_data(data)
  @data_collection << data
end


54
55
56
57
# File 'lib/uberous/uber.rb', line 54

def add_link(rel, url, options = {})
  link = Data.new(options.merge(rel: rel, url: url))
  add_data(link)
end

#to_hashObject



59
60
61
62
63
64
# File 'lib/uberous/uber.rb', line 59

def to_hash
  hash = @attrs.dup
  data = @data_collection.map(&:to_hash)
  hash[:data] = data unless data.empty?
  hash
end