Class: Protopack::Exporter
- Inherits:
-
Object
- Object
- Protopack::Exporter
- Defined in:
- lib/protopack/exporter.rb
Instance Method Summary collapse
- #array_assoc(list) ⇒ Object
- #build_association(obj, table, getter, setter) ⇒ Object
- #build_association_table(obj) ⇒ Object
- #clean_attrs(attrs) ⇒ Object
- #clean_yaml(hsh) ⇒ Object
- #default_export_config ⇒ Object
- #export_config(obj) ⇒ Object
- #maybe_name_methods ⇒ Object
- #name_method(obj) ⇒ Object
- #no_name?(obj_id) ⇒ Boolean
- #remove_blanks(attrs) ⇒ Object
- #styled_literal(str) ⇒ Object
- #to_attributes(obj) ⇒ Object
- #to_package(obj, meta = { }) ⇒ Object
- #to_yaml(obj, meta = {}) ⇒ Object
Instance Method Details
#array_assoc(list) ⇒ Object
5 |
# File 'lib/protopack/exporter.rb', line 5 def array_assoc list ; list.map { |item| to_attributes item } ; end |
#build_association(obj, table, getter, setter) ⇒ Object
27 28 29 30 31 32 |
# 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) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# 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) ⇒ Object
14 |
# 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) ⇒ Object
6 |
# File 'lib/protopack/exporter.rb', line 6 def clean_yaml hsh ; StyledYAML.dump(clean_attrs hsh) ; end |
#default_export_config ⇒ Object
8 |
# File 'lib/protopack/exporter.rb', line 8 def default_export_config ; { fields: [], associations: [] } ; end |
#export_config(obj) ⇒ Object
9 |
# File 'lib/protopack/exporter.rb', line 9 def export_config obj ; default_export_config.merge obj.protopack_export_config ; end |
#maybe_name_methods ⇒ Object
3 |
# File 'lib/protopack/exporter.rb', line 3 def maybe_name_methods ; i{ export_name full_name presentation_name name } ; end |
#name_method(obj) ⇒ Object
4 |
# 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) ⇒ Boolean
7 |
# 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) ⇒ Object
12 |
# 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) ⇒ Object
13 |
# File 'lib/protopack/exporter.rb', line 13 def styled_literal str ; str.is_a?(String) ? StyledYAML.literal(str) : str ; end |
#to_attributes(obj) ⇒ Object
10 |
# 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 = { }) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/protopack/exporter.rb', line 34 def to_package obj, ={ } 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().recursively_stringify_keys! end |
#to_yaml(obj, meta = {}) ⇒ Object
11 |
# File 'lib/protopack/exporter.rb', line 11 def to_yaml obj, ={} ; clean_yaml to_package obj, ; end |