lib/conglomerate/serializer.rb in conglomerate-0.2.1 vs lib/conglomerate/serializer.rb in conglomerate-0.3.0

- old
+ new

@@ -40,12 +40,15 @@ collection end end def apply_data(collection, data: [], object: nil) - data = data.map do |name| - {"name" => name.to_s, "value" => object.nil? ? "" : object.send(name)} + data = data.map do |datum| + name = datum[:name] + value = object.nil? ? "" : object.send(name) + + {"name" => name.to_s, datum.fetch(:type, "value").to_s => value} end if data.empty? collection else @@ -73,12 +76,12 @@ item = apply_href( item, :proc => self.class._item_href, :object => object ) end - names = self.class._attributes.map { |attr| attr[:name] } - item = apply_data(item, :data => names, :object => object) + attributes = self.class._attributes + item = apply_data(item, :data => attributes, :object => object) links = self.class._attributes .select { |attr| attr[:block] } if links.empty? @@ -96,11 +99,10 @@ end def apply_template(collection) attrs = self.class._attributes .select { |attr| attr[:template] } - .map { |attr| attr[:name] } if attrs.empty? collection else collection.merge({"template" => apply_data({}, :data => attrs)}) @@ -171,17 +173,20 @@ def item_href(&block) self._item_href = block end def query(rel, data: [], &block) + data = [*data] + data = data.map { |datum| {:name => datum} } self._queries = self._queries << { - :rel => rel, :data => [*data], :block => block + :rel => rel, :data => data, :block => block } end - def attribute(name, template: false, rel: nil, &block) + def attribute(name, template: false, rel: nil, type: :value, &block) self._attributes = self._attributes << { - :name => name, :template => template, :rel => rel, :block => block + :name => name, :template => template, :rel => rel, :type => type, + :block => block } end def link(rel, &block) self._links = self._links << {