lib/conglomerate/serializer.rb in conglomerate-0.3.1 vs lib/conglomerate/serializer.rb in conglomerate-0.4.0

- old
+ new

@@ -39,19 +39,25 @@ else collection end end - def apply_data(collection, data: [], object: nil, default_value: nil) + def apply_data( + collection, data: [], object: nil, default_value: nil, + build_template: false + ) data = data.map do |datum| name = datum[:name] type = datum.fetch(:type, :value) + prompt = datum.fetch(:prompt, nil) value = sanitize_value( object, :name => name, :type => type, :default_value => default_value ) - {"name" => name.to_s, type.to_s => value} + {"name" => name.to_s, type.to_s => value}.tap do |d| + d["prompt"] = prompt if build_template && prompt + end end if data.empty? collection else @@ -102,16 +108,21 @@ end def apply_template(collection) attrs = self.class._attributes .select { |attr| attr[:template] } + attrs += self.class._templates if attrs.empty? collection else collection.merge( - {"template" => apply_data({}, :data => attrs, :default_value => "")} + { + "template" => apply_data( + {}, :data => attrs, :default_value => "", :build_template => true + ) + } ) end end def apply_links(collection, links: self.class._links, object: nil) @@ -199,25 +210,34 @@ self._queries = self._queries << { :rel => rel, :data => data, :block => block } end - def attribute(name, template: false, rel: nil, type: :value, &block) + def attribute( + name, template: false, rel: nil, type: :value, prompt: nil, &block + ) self._attributes = self._attributes << { :name => name, :template => template, :rel => rel, :type => type, - :block => block + :prompt => prompt, :block => block } end def link(rel, &block) self._links = self._links << { :rel => rel, :block => block } end - attr_writer :_href, :_item_href, :_queries, :_attributes, :_links + def template(name, type: :value, prompt: nil) + self._templates = self._templates << { + :name => name, :type => type, :prompt => prompt, :template => true + } + end + attr_writer :_href, :_item_href, :_queries, :_attributes, :_links, + :_templates + def _href @_href || nil end def _item_href @@ -232,9 +252,13 @@ @_attributes || [] end def _links @_links || [] + end + + def _templates + @_templates || [] end end end end