Class: Uberous::Data
- Inherits:
-
Object
- Object
- Uberous::Data
- 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
-
#data_collection ⇒ Object
readonly
Returns the value of attribute data_collection.
Instance Method Summary collapse
- #add_data(data) ⇒ Object
- #add_link(rel, url, options = {}) ⇒ Object
-
#initialize(attrs = {}) ⇒ Data
constructor
A new instance of Data.
- #to_hash ⇒ Object
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_collection ⇒ Object (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 |
#add_link(rel, url, options = {}) ⇒ Object
54 55 56 57 |
# File 'lib/uberous/uber.rb', line 54 def add_link(rel, url, = {}) link = Data.new(.merge(rel: rel, url: url)) add_data(link) end |
#to_hash ⇒ Object
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 |