lib/conglomerate/serializer.rb in conglomerate-0.8.1 vs lib/conglomerate/serializer.rb in conglomerate-0.9.0

- old
+ new

@@ -20,11 +20,11 @@ private attr_accessor :objects, :context def actions - [:version, :href, :queries, :items, :template, :links] + [:version, :href, :queries, :commands, :items, :template, :links] end def apply_version(collection) collection.merge({"version" => "1.0"}) end @@ -76,10 +76,24 @@ else collection.merge({"queries" => queries}) end end + def apply_commands(collection) + commands = self.class._commands.map do |command| + build_command( + command[:rel], command[:data], command[:prompt], command[:block] + ) + end + + if commands.empty? + collection + else + collection.merge({"commands" => commands}) + end + end + def apply_items(collection) items = objects.map do |object| item = {} if self.class._item_href @@ -164,10 +178,17 @@ query = {"rel" => rel.to_s} query = apply_href(query, :proc => block) apply_data(query, :data => data, :default_value => "") end + def build_command(rel, data, prompt, block) + command = {"rel" => rel.to_s} + command = apply_href(command, :proc => block) + command["prompt"] = prompt if prompt + apply_data(command, :data => data, :default_value => "") + end + def build_item_link(rel, proc: nil, object: nil) link = {"rel" => rel.to_s} apply_href(link, :proc => proc, :object => object) end @@ -227,10 +248,18 @@ self._queries = self._queries << { :rel => rel, :data => data, :block => block } end + def command(rel, data: [], prompt: nil, &block) + data = [*data] + data = data.map { |datum| {:name => datum} } + self._commands = self._commands << { + :rel => rel, :data => data, :prompt => prompt, :block => block + } + end + def attribute( name, template: false, rel: nil, type: :value, prompt: nil, &block ) self._attributes = self._attributes << { :name => name, :template => template, :rel => rel, :type => type, @@ -255,11 +284,11 @@ :name => name, :type => type, :prompt => prompt, :template => true } end attr_writer :_href, :_item_href, :_queries, :_attributes, :_links, - :_item_links, :_templates + :_item_links, :_templates, :_commands def _href @_href || nil end @@ -267,9 +296,13 @@ @_item_href || nil end def _queries @_queries || [] + end + + def _commands + @_commands || [] end def _attributes @_attributes || [] end