atom-tools 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/atom/yaml.rb DELETED
@@ -1,116 +0,0 @@
1
- require "atom/entry"
2
- require "yaml"
3
-
4
- module Atom
5
- class Time
6
- def taguri; nil end
7
- end
8
-
9
- class Element < Hash
10
- def taguri # :nodoc:
11
- end
12
-
13
- def to_yaml_properties # :nodoc:
14
- self.class.elements.find_all do |n,k,r|
15
- v = get(n)
16
- v and not (v.respond_to? :empty? and v.empty?)
17
- end.map { |n,k,r| "@#{n}" }
18
- end
19
-
20
- def to_yaml( opts = {} ) # :nodoc:
21
- YAML::quick_emit( object_id, opts ) do |out|
22
- out.map( taguri, to_yaml_style ) do |map|
23
- self.to_yaml_properties.each do |m|
24
- map.add( m[1..-1], instance_variable_get( m ) )
25
- end
26
- end
27
- end
28
- end
29
- end
30
-
31
- class AttrEl < Atom::Element
32
- def to_yaml( opts = {} )
33
- YAML::quick_emit( object_id, opts ) do |out|
34
- out.map( nil, to_yaml_style ) do |map|
35
- self.class.attrs.each do |n,r|
36
- map.add( n.to_s, self[n.to_s] ) if self[n.to_s]
37
- end
38
- end
39
- end
40
- end
41
- end
42
-
43
- class Text < Atom::Element
44
- def taguri # :nodoc:
45
- end
46
-
47
- def to_yaml( opts = {} ) # :nodoc:
48
- YAML::quick_emit( object_id, opts ) do |out|
49
- out.scalar(taguri, to_s, to_yaml_style)
50
- end
51
- end
52
- def to_yaml_style # :nodoc:
53
- if @content.to_s.match("\n")
54
- :fold
55
- else
56
- :plain
57
- end
58
- end
59
- end
60
-
61
- class Entry
62
- def to_yaml_type # :nodoc:
63
- '!necronomicorp.com,2006/entry'
64
- end
65
-
66
- def to_yaml( opts = {} ) # :nodoc:
67
- YAML::quick_emit( object_id, opts ) do |out|
68
- out.map( taguri, to_yaml_style ) do |map|
69
- self.to_yaml_properties.each do |m|
70
- map.add( m[1..-1], instance_variable_get( m ) )
71
- end
72
- map.add("draft", true) if self.draft
73
- end
74
- end
75
- end
76
-
77
- # parses an Atom::Entry from YAML
78
- def self.from_yaml yaml
79
- hash = if yaml.kind_of?(Hash); yaml else YAML.load(yaml); end
80
-
81
- entry = Atom::Entry.new
82
-
83
- ["id", "published", "updated", "title", "summary", "draft"].each do |name|
84
- entry.send("#{name}=".to_sym, hash[name])
85
- end
86
-
87
- elem_constructs = {"authors" => entry.authors,
88
- "contributors" => entry.contributors,
89
- "links" => entry.links,
90
- "categories" => entry.categories}
91
-
92
- elem_constructs.each do |type,ary|
93
- hash[type] ||= []
94
-
95
- hash[type].each do |yelem|
96
- elem = ary.new
97
-
98
- elem.class.attrs.each do |attrb,req|
99
- elem[attrb.to_s] = yelem[attrb.to_s]
100
- end
101
-
102
- elem.class.elements.each do |name,kind,req|
103
- elem.send("#{name}=".to_sym, yelem[name.to_s])
104
- end
105
- end
106
- end
107
-
108
- entry.tag_with hash["tags"]
109
-
110
- entry.content = hash["content"]
111
- entry.content["type"] = hash["type"] if hash["type"]
112
-
113
- entry
114
- end
115
- end
116
- end