doc_wrapper 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,49 @@
1
1
  module DocWrapper
2
2
  module ClassMethods
3
3
 
4
+ # Create a typed property definition for a document wrapper.
5
+ # The property_name must be a symbol.
6
+ def property (property_name, type, selector, options = {}, &block)
7
+ raise "Unhandled property type: #{type.to_s}" if ![:string, :date, :time, :boolean, :raw].include?(type)
8
+ add_property_definition(property_name, build_property_definition(property_name, type, selector, initialize_options(options), block))
9
+ end
10
+
11
+ def multi_property (property_name, selectors, options = {}, &block)
12
+ raise "Multi properties require a block" if block.nil?
13
+ add_property_definition(property_name, MultiPropertyDefinition.new(property_name, selectors, initialize_options(options), block))
14
+ end
15
+
16
+ def has_many (property_name, selector, klass, options = {})
17
+ options = initialize_options(options)
18
+ define_method property_name do
19
+ get_has_many( property_name, selector, klass, options)
20
+ end
21
+ end
22
+
23
+ def has_one (property_name, selector, klass, options = {})
24
+ options = initialize_options(options)
25
+ define_method(property_name) do
26
+ get_has_one(property_name, selector, klass, options)
27
+ end
28
+ end
29
+
30
+ def namespaces (namespaces)
31
+ @namespaces
32
+ # define_method(:namespaces) do
33
+ # namespaces
34
+ # end
35
+ end
36
+
37
+ ##################
38
+ # Helper Methods #
39
+ ##################
40
+
41
+ def add_property_definition (property_name, wrapper)
42
+ add_property_name(property_name)
43
+ add_property_wrapper(property_name, wrapper)
44
+ add_property_accessor(property_name)
45
+ end
46
+
4
47
  # Add a property name to the singleton property_names attribute.
5
48
  def add_property_name (property_name)
6
49
  # Add the property name to the property_names collection.
@@ -25,21 +68,10 @@ module DocWrapper
25
68
  # Make sure the options have a :document key with a value of 1.
26
69
  # This forces all lookups to be for the 0th document in documents if
27
70
  # the user did not specify an offset into the array.
28
- { :document => 1 }.merge(options)
29
- end
30
-
31
- def add_property_definition (property_name, wrapper)
32
- add_property_name(property_name)
33
- add_property_wrapper(property_name, wrapper)
34
- add_property_accessor(property_name)
71
+ options = { :document => 1 }.merge(options)
72
+ options.merge(@namespaces) if @namespaces
35
73
  end
36
74
 
37
- # Create a typed property definition for a document wrapper.
38
- # The property_name must be a symbol.
39
- def property (property_name, type, selector, options = {}, &block)
40
- raise "Unhandled property type: #{type.to_s}" if ![:string, :date, :time, :boolean, :raw].include?(type)
41
- add_property_definition(property_name, build_property_definition(property_name, type, selector, initialize_options(options), block))
42
- end
43
75
 
44
76
  def build_property_definition (property_name, type, selector, options, block)
45
77
  DocWrapper.const_get("#{camelize(type.to_s)}PropertyDefinition").new(property_name, type, selector, initialize_options(options), block)
@@ -49,23 +81,5 @@ module DocWrapper
49
81
  string.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
50
82
  end
51
83
 
52
- def multi_property (property_name, selectors, options = {}, &block)
53
- raise "Multi properties require a block" if block.nil?
54
- add_property_definition(property_name, MultiPropertyDefinition.new(property_name, selectors, initialize_options(options), block))
55
- end
56
-
57
- def has_many (property_name, selector, klass, options = {})
58
- options = initialize_options(options)
59
- define_method property_name do
60
- get_has_many( property_name, selector, klass, options)
61
- end
62
- end
63
-
64
- def has_one (property_name, selector, klass, options = {})
65
- options = initialize_options(options)
66
- define_method(property_name) do
67
- get_has_one(property_name, selector, klass, options)
68
- end
69
- end
70
84
  end
71
85
  end
@@ -11,10 +11,14 @@ module DocWrapper
11
11
  end
12
12
 
13
13
  def property (documents)
14
- begin
15
- transform(documents[@options[:document] - 1].search(@selector))
16
- rescue Nokogiri::CSS::SyntaxError
17
- transform(documents[@options[:document] - 1].xpath(@selector))
14
+ if options[:namespaces]
15
+ transform(documents[@options[:document] - 1].xpath(@selector, options[:namespaces] ))
16
+ else
17
+ begin
18
+ transform(documents[@options[:document] - 1].search(@selector))
19
+ rescue Nokogiri::CSS::SyntaxError
20
+ transform(documents[@options[:document] - 1].xpath(@selector))
21
+ end
18
22
  end
19
23
  end
20
24
 
@@ -1,3 +1,3 @@
1
1
  module DocWrapper
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.1"
3
3
  end
metadata CHANGED
@@ -1,13 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: doc_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 59
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 9
9
- - 0
10
- version: 0.9.0
4
+ version: 0.9.1
11
5
  platform: ruby
12
6
  authors:
13
7
  - Mark Menard
@@ -15,85 +9,59 @@ autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
11
 
18
- date: 2011-03-06 00:00:00 -05:00
12
+ date: 2011-03-14 00:00:00 -04:00
19
13
  default_executable:
20
14
  dependencies:
21
15
  - !ruby/object:Gem::Dependency
22
16
  name: activesupport
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
26
20
  requirements:
27
21
  - - ">="
28
22
  - !ruby/object:Gem::Version
29
- hash: 7
30
- segments:
31
- - 3
32
- - 0
33
- - 0
34
23
  version: 3.0.0
35
- type: :runtime
36
- version_requirements: *id001
24
+ version:
37
25
  - !ruby/object:Gem::Dependency
38
26
  name: bundler
39
- prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
41
- none: false
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
42
30
  requirements:
43
31
  - - ">="
44
32
  - !ruby/object:Gem::Version
45
- hash: 23
46
- segments:
47
- - 1
48
- - 0
49
- - 0
50
33
  version: 1.0.0
51
- type: :development
52
- version_requirements: *id002
34
+ version:
53
35
  - !ruby/object:Gem::Dependency
54
36
  name: nokogiri
55
- prerelease: false
56
- requirement: &id003 !ruby/object:Gem::Requirement
57
- none: false
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
58
40
  requirements:
59
41
  - - ">="
60
42
  - !ruby/object:Gem::Version
61
- hash: 3
62
- segments:
63
- - 0
64
43
  version: "0"
65
- type: :development
66
- version_requirements: *id003
44
+ version:
67
45
  - !ruby/object:Gem::Dependency
68
46
  name: rspec
69
- prerelease: false
70
- requirement: &id004 !ruby/object:Gem::Requirement
71
- none: false
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
72
50
  requirements:
73
51
  - - ">="
74
52
  - !ruby/object:Gem::Version
75
- hash: 15
76
- segments:
77
- - 2
78
- - 0
79
- - 0
80
53
  version: 2.0.0
81
- type: :development
82
- version_requirements: *id004
54
+ version:
83
55
  - !ruby/object:Gem::Dependency
84
56
  name: ZenTest
85
- prerelease: false
86
- requirement: &id005 !ruby/object:Gem::Requirement
87
- none: false
57
+ type: :development
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
88
60
  requirements:
89
61
  - - ">="
90
62
  - !ruby/object:Gem::Version
91
- hash: 3
92
- segments:
93
- - 0
94
63
  version: "0"
95
- type: :development
96
- version_requirements: *id005
64
+ version:
97
65
  description: Using the DocWrapper DSL you can easily define classes that wrap HTML DOM Documents allowing extraction of properties using either XPath or CSS selectors.
98
66
  email:
99
67
  - mark@enablelabs.com
@@ -137,29 +105,21 @@ rdoc_options: []
137
105
  require_paths:
138
106
  - lib
139
107
  required_ruby_version: !ruby/object:Gem::Requirement
140
- none: false
141
108
  requirements:
142
109
  - - ">="
143
110
  - !ruby/object:Gem::Version
144
- hash: 3
145
- segments:
146
- - 0
147
111
  version: "0"
112
+ version:
148
113
  required_rubygems_version: !ruby/object:Gem::Requirement
149
- none: false
150
114
  requirements:
151
115
  - - ">="
152
116
  - !ruby/object:Gem::Version
153
- hash: 23
154
- segments:
155
- - 1
156
- - 3
157
- - 6
158
117
  version: 1.3.6
118
+ version:
159
119
  requirements: []
160
120
 
161
121
  rubyforge_project: doc_wrapper
162
- rubygems_version: 1.3.7
122
+ rubygems_version: 1.3.5
163
123
  signing_key:
164
124
  specification_version: 3
165
125
  summary: Declarative DSL for defining classes to wrap HTML DOM Documents