class Protopack::Exporter

Public Instance Methods

array_assoc(list ;) click to toggle source
# File lib/protopack/exporter.rb, line 5
def array_assoc list      ; list.map { |item| to_attributes item }                                     ; end
build_association(obj, table, getter, setter) click to toggle source
# File lib/protopack/exporter.rb, line 27
def build_association obj, table, getter, setter
  assoc = obj.send getter
  return if assoc.blank? || assoc.empty?
  attrs = (assoc.respond_to?(:each)) ? array_assoc(assoc) : to_attributes(assoc)
  table[setter] = attrs
end
build_association_table(obj) click to toggle source
# File lib/protopack/exporter.rb, line 16
def build_association_table obj
  export_config(obj)[:associations].inject({ }) { |table, name|
    if name.is_a? Hash
      build_association obj, table, name[:get], name[:set]
    else
      build_association obj, table, name, "#{name}_attributes"
    end
    table
  }
end
clean_attrs(attrs ;) click to toggle source
# File lib/protopack/exporter.rb, line 14
def clean_attrs     attrs ; remove_blanks(attrs).recursively_replace_values { |k,v| styled_literal v } ; end
clean_yaml(hsh ;) click to toggle source
# File lib/protopack/exporter.rb, line 6
def clean_yaml hsh        ; StyledYAML.dump(clean_attrs hsh)                                           ; end
default_export_config() click to toggle source
# File lib/protopack/exporter.rb, line 8
def default_export_config ; { fields: [], associations: [] }                                           ; end
export_config(obj ;) click to toggle source
# File lib/protopack/exporter.rb, line 9
def export_config     obj ; default_export_config.merge obj.protopack_export_config                    ; end
maybe_name_methods() click to toggle source
# File lib/protopack/exporter.rb, line 3
def maybe_name_methods    ; %i{ export_name full_name presentation_name name }                         ; end
name_method(obj ;) click to toggle source
# File lib/protopack/exporter.rb, line 4
def name_method obj       ; maybe_name_methods.detect { |m| obj.respond_to?(m) && obj.send(m) }        ; end
no_name?(obj_id ;) click to toggle source
# File lib/protopack/exporter.rb, line 7
def no_name? obj_id       ; raise "no name for object: #{obj_id.inspect}" if obj_id.blank?             ; end
remove_blanks(attrs ;) click to toggle source
# File lib/protopack/exporter.rb, line 12
def remove_blanks   attrs ; attrs.recursively_remove_by_value! { |v| (v != false) && v.blank? }        ; end
styled_literal(str ;) click to toggle source
# File lib/protopack/exporter.rb, line 13
def styled_literal    str ; str.is_a?(String) ? StyledYAML.literal(str) : str                          ; end
to_attributes(obj ;) click to toggle source
# File lib/protopack/exporter.rb, line 10
def to_attributes     obj ; obj.slice(*export_config(obj)[:fields]).merge build_association_table obj  ; end
to_package(obj, meta={ }) click to toggle source
# File lib/protopack/exporter.rb, line 34
def to_package obj, meta={ }
  obj_name        = obj.send(name_method obj)
  obj_id          = obj_name.to_s.gsub(/_/, '-')

  no_name? obj_id

  hsh = {
    id:          obj_id,
    description: obj_name,
    type:        obj.class.name,
    attributes:  to_attributes(obj),
  }.deep_merge(meta).recursively_stringify_keys!
end
to_yaml(obj, meta={}) click to toggle source
# File lib/protopack/exporter.rb, line 11
def to_yaml  obj, meta={} ; clean_yaml to_package obj, meta                                            ; end