socialcastr 0.2.3 → 0.2.4

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.
@@ -94,8 +94,9 @@ module Socialcastr
94
94
  end
95
95
 
96
96
  def find(*arguments)
97
- scope = arguments.slice!(0)
98
- options = arguments.slice!(0) || {}
97
+ args = arguments.dup
98
+ scope = args.slice!(0)
99
+ options = args.slice!(0) || {}
99
100
  case scope
100
101
  when :all then find_every(options)
101
102
  when :first then find_every(options).first
@@ -15,6 +15,12 @@ class String
15
15
  end
16
16
  end
17
17
 
18
+ class Array
19
+ def to_hash
20
+ self.reduce({}) { |h,kv| h[kv[0]] = kv[1]; h }
21
+ end
22
+ end
23
+
18
24
  module Socialcastr
19
25
  module SAX
20
26
 
@@ -31,83 +37,93 @@ module Socialcastr
31
37
  characters(s)
32
38
  end
33
39
 
40
+ def container_type
41
+ @types[-1]
42
+ end
43
+
44
+ def container_value
45
+ @values[-1]
46
+ end
47
+
34
48
  HASH = 104 # 'h'
35
49
  ARRAY = 97 # 'a'
36
50
  INTEGER = 105 # 'i'
37
51
  BOOLEAN = 98 # 'b'
38
52
  STRING = 115 # 's'
39
53
 
40
- def container_type
41
- @types[-1]
54
+ def nil_element!
55
+ @nil = true
42
56
  end
43
57
 
44
- def container_value
45
- @values[-1]
58
+ def nil_element?
59
+ @nil
60
+ end
61
+
62
+ def end_nil_element
63
+ @nil = false
46
64
  end
47
65
 
48
- def parse_attrs_and_get_type(attrs=[])
49
- type = HASH
50
- attrs.each do |attr|
51
- case attr[0]
52
- when "type"
53
- type = attr[1][0]
54
- when "nil"
55
- @nil = true
56
- end
66
+ def parse_attrs_and_get_type(attribute_array=[])
67
+ attributes = attribute_array.to_hash
68
+ return nil_element! if attributes["nil"]
69
+ attributes["type"] ? attributes["type"][0] : HASH
70
+ end
71
+
72
+ def push_element(type)
73
+ @types.push type
74
+ @values.push nil
75
+ end
76
+
77
+ def pop_element
78
+ return [@values.pop, @types.pop]
79
+ end
80
+
81
+ def update_string_element(string)
82
+ @types[-1] = STRING if container_type == HASH
83
+ @values[-1] = container_value ? container_value + string : string
84
+ end
85
+
86
+ def add_to_container(name, element)
87
+ if container_type == ARRAY
88
+ container_value ? @values[-1].push(element) : @values[-1] = [element]
89
+ else # Hash
90
+ container_value ? @values[-1][name]=element : @values[-1] = { name => element }
57
91
  end
58
- return type
59
92
  end
60
93
 
61
94
  def start_element name, attrs = []
62
- if name.contains_dot? # [FIXME] we can't evaluate strings inside elements like <html.title>
63
- @nil = true
64
- return nil
65
- end
95
+ return nil_element! if name.contains_dot? # [FIXME] we can't evaluate strings inside elements like <html.title>
66
96
  type = parse_attrs_and_get_type(attrs)
67
- unless @nil
68
- @types.push type
69
- @values.push nil
70
- end
97
+ return if nil_element?
98
+ push_element(type)
71
99
  end
72
100
 
73
101
  def characters string
74
- return if @nil
75
- return if (string.all_spaces? && (container_type != STRING || container_value.nil?))
76
- @types[-1] = STRING if container_type == HASH
77
- @values[-1] = container_value ? container_value + string : string
102
+ return if nil_element? || (string.all_spaces? && (container_type != STRING || container_value.nil?))
103
+ update_string_element(string)
78
104
  end
79
105
 
80
106
  def end_element name
81
- if @nil
82
- @nil = false
107
+ return end_nil_element if nil_element?
108
+
109
+ (value, type) = pop_element
110
+ case type
111
+ when HASH
112
+ element = Socialcastr.const_get(Socialcastr.to_class_name(name), false).from_hash(value || {})
113
+ when INTEGER
114
+ element = value.to_i
115
+ when BOOLEAN
116
+ element = Boolean.new(value)
117
+ when ARRAY
118
+ element = value || []
83
119
  else
84
- value = @values.pop
85
- type = @types.pop
86
-
87
- # return if value.nil?
88
-
89
- case type
90
- when HASH
91
- element = Socialcastr.const_get(Socialcastr.to_class_name(name)).from_hash(value || {})
92
- when INTEGER
93
- element = value.to_i
94
- when BOOLEAN
95
- element = Boolean.new(value)
96
- when ARRAY
97
- element = value || []
98
- else
99
- element = value
100
- end
101
-
102
- if container_type
103
- if container_type == ARRAY
104
- @values[-1] ? @values[-1].push(element) : @values[-1] = [element]
105
- else # Hash
106
- @values[-1] ? @values[-1][name]=element : @values[-1] = { name => element }
107
- end
108
- else # Root Node
109
- @data = element
110
- end
120
+ element = value
121
+ end
122
+
123
+ if container_type
124
+ add_to_container(name, element)
125
+ else # Root Node
126
+ self.data = element
111
127
  end
112
128
  end
113
129
  end
@@ -1,3 +1,3 @@
1
1
  module Socialcastr
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: socialcastr
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 3
10
- version: 0.2.3
9
+ - 4
10
+ version: 0.2.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Riccardo Cambiassi
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-02 00:00:00 +01:00
19
- default_executable:
18
+ date: 2011-07-18 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: rake
@@ -132,7 +131,6 @@ files:
132
131
  - spec/message_spec.rb
133
132
  - spec/socialcastr_spec.rb
134
133
  - spec/spec_helper.rb
135
- has_rdoc: true
136
134
  homepage: https://github.com/bru/socialcastr
137
135
  licenses: []
138
136
 
@@ -173,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
171
  requirements: []
174
172
 
175
173
  rubyforge_project: socialcastr
176
- rubygems_version: 1.5.3
174
+ rubygems_version: 1.8.5
177
175
  signing_key:
178
176
  specification_version: 3
179
177
  summary: Ruby wrapper for the Socialcast API