lib/conglomerate/serializer.rb in conglomerate-0.14.0 vs lib/conglomerate/serializer.rb in conglomerate-0.15.0
- old
+ new
@@ -2,13 +2,13 @@
module Serializer
def self.included(descendant)
descendant.extend(ClassMethods)
end
- def initialize(objects, context: nil)
+ def initialize(objects, options = {})
self.objects = [*objects].compact
- self.context = context
+ self.context = options.fetch(:context, nil)
end
def serialize
{
"collection" => actions.inject({}) do |collection, action|
@@ -27,11 +27,14 @@
def apply_version(collection)
collection.merge({"version" => "1.0"})
end
- def apply_href(collection, proc: self.class._href, object: nil)
+ def apply_href(collection, options = {})
+ proc = options.fetch(:proc, self.class._href)
+ object = options.fetch(:object, nil)
+
if proc
if object
collection.merge({"href" => context.instance_exec(object, &proc)})
else
collection.merge({"href" => context.instance_eval(&proc)})
@@ -39,14 +42,16 @@
else
collection
end
end
- def apply_data(
- collection, data: [], object: nil, default_value: nil,
- build_template: false
- )
+ def apply_data(collection, options = {})
+ data = options.fetch(:data, [])
+ object = options.fetch(:object, nil)
+ default_value = options.fetch(:default_value, nil)
+ build_template = options.fetch(:build_template, false)
+
data = data.map do |datum|
name = datum[:name]
type = datum.fetch(:type, :value)
prompt = datum.fetch(:prompt, nil)
value = sanitize_value(
@@ -139,11 +144,14 @@
}
)
end
end
- def apply_links(collection, links: self.class._links, object: nil)
+ def apply_links(collection, options = {})
+ links = options.fetch(:links, self.class._links)
+ object = options.fetch(:object, nil)
+
if object && !links.empty?
links = links.map do |link|
if !link.has_key?(:name) || present?(object.send(link[:name]))
build_item_link(
link[:rel], :proc => link[:block], :object => object
@@ -185,16 +193,23 @@
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)
+ def build_item_link(rel, options = {})
+ proc = options.fetch(:proc, nil)
+ object = options.fetch(:object, nil)
+
link = {"rel" => rel.to_s}
apply_href(link, :proc => proc, :object => object)
end
- def sanitize_value(object, name:, type: :value, default_value: nil)
+ def sanitize_value(object, options = {})
+ name = options.fetch(:name)
+ type = options.fetch(:type, :value)
+ default_value = options.fetch(:default_value, nil)
+
if object.nil? || object.send(name).nil?
if type == :array
[]
elsif type == :object
{}
@@ -238,29 +253,37 @@
def item_href(&block)
self._item_href = block
end
- def query(rel, data: [], &block)
+ def query(rel, options = {}, &block)
+ data = options.fetch(:data, [])
+
data = [*data]
data = data.map { |datum| {:name => datum} }
self._queries = self._queries << {
:rel => rel, :data => data, :block => block
}
end
- def command(rel, data: [], prompt: nil, &block)
+ def command(rel, options = {}, &block)
+ data = options.fetch(:data, [])
+ prompt = options.fetch(:prompt, nil)
+
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
- )
+ def attribute(name, options = {}, &block)
+ template = options.fetch(:template, false)
+ rel = options.fetch(:rel, nil)
+ type = options.fetch(:type, :value)
+ prompt = options.fetch(:prompt, nil)
+
self._attributes = self._attributes << {
:name => name, :template => template, :rel => rel, :type => type,
:prompt => prompt, :block => block
}
end
@@ -275,10 +298,13 @@
self._item_links = self._item_links << {
:rel => rel, :block => block
}
end
- def template(name, type: :value, prompt: nil)
+ def template(name, options = {})
+ type = options.fetch(:type, :value)
+ prompt = options.fetch(:prompt, nil)
+
self._templates = self._templates << {
:name => name, :type => type, :prompt => prompt, :template => true
}
end