nakajima-elementor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ module Kernel
2
+ def blank_context(ivars={}, &block)
3
+ klass = Class.new do
4
+ instance_methods.each { |m| undef_method(m) unless m =~ /^(__|instance_|meta)/ }
5
+ end
6
+
7
+ klass.class_eval(&block)
8
+ instance = klass.new
9
+ ivars.each { |key, value| instance.instance_variable_set("@#{key}", value) }
10
+ instance
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Elementor
2
+ class ElementSet < Array
3
+ def with_text(str)
4
+ select { |item| item.text.include?(str) }
5
+ end
6
+
7
+ def inspect
8
+ map(&:text).inspect
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,41 @@
1
+ module Elementor
2
+ class Result
3
+ def initialize(context, &block)
4
+ @context = context
5
+ block.call(naming_context)
6
+ end
7
+
8
+ def naming_context
9
+ @naming_context ||= blank_context(:this => self) do
10
+ def method_missing(sym, *args)
11
+ @this.element_names[sym] = *args
12
+ end
13
+ end
14
+ end
15
+
16
+ def dispatcher
17
+ @dispatcher ||= blank_context(:this => self, :context => @context) do
18
+ def method_missing(sym, *args, &block)
19
+ @this.try(sym, *args, &block) || @context.try(sym, *args, &block) || super
20
+ end
21
+
22
+ def self.method_missing(sym, *args)
23
+ p sym
24
+ end
25
+ end
26
+ end
27
+
28
+ def define_elements!
29
+ element_names.each do |name, selector|
30
+ meta_def(name) do |*mutators|
31
+ set = ElementSet.new(@context.search(selector).to_ary)
32
+ mutators.empty? ? set : mutators.inject(set) { |result, fn| fn[result] }
33
+ end
34
+ end
35
+ end
36
+
37
+ def element_names
38
+ @element_names ||= { }
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,11 @@
1
+ module Spec
2
+ module Matchers
3
+ class Have
4
+ def with_text(*args)
5
+ @args = []
6
+ @args << proc { |set| set.with_text(*args) }
7
+ self
8
+ end
9
+ end
10
+ end
11
+ end
data/lib/elementor.rb ADDED
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH << File.dirname(__FILE__) + '/core_ext'
2
+ $LOAD_PATH << File.dirname(__FILE__) + '/elementor'
3
+
4
+ require 'rubygems'
5
+ require 'nokogiri'
6
+ require 'nakajima'
7
+ require 'kernel'
8
+ require 'result'
9
+ require 'element_set'
10
+
11
+ module Elementor
12
+ def elements(opts={}, &block)
13
+ opts[:from] ||= :body
14
+ doc = Nokogiri(send(opts[:from]))
15
+ namer = Result.new(doc, &block)
16
+ namer.define_elements!
17
+ namer.dispatcher
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nakajima-elementor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pat Nakajima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-19 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nakajima
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: nokogiri
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0"
32
+ version:
33
+ description:
34
+ email: patnakajima@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - lib/elementor
43
+ - lib/elementor.rb
44
+ - lib/elementor/spec.rb
45
+ - lib/elementor/element_set.rb
46
+ - lib/elementor/result.rb
47
+ - lib/core_ext
48
+ - lib/core_ext/kernel.rb
49
+ has_rdoc: false
50
+ homepage: http://github.com/nakajima/elementor
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.2.0
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: Prettier element traversal with Nokogiri
75
+ test_files: []
76
+