elementor 0.0.2

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,19 @@
1
+ class Object
2
+ def try(sym, *args, &block)
3
+ respond_to?(sym) ? send(sym, *args, &block) : nil
4
+ end
5
+
6
+ def metaclass
7
+ class << self; self end
8
+ end
9
+
10
+ def meta_eval(&block)
11
+ metaclass.instance_eval(&block)
12
+ end
13
+
14
+ def meta_def(sym, &block)
15
+ meta_eval do
16
+ define_method(sym, &block)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ class Symbol
2
+ def to_proc
3
+ Proc.new do |target|
4
+ target.send(self)
5
+ end
6
+ end
7
+ end unless :test.respond_to?(:to_proc)
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH << File.dirname(__FILE__) + '/core_ext'
2
+ $LOAD_PATH << File.dirname(__FILE__) + '/elementor'
3
+
4
+ require 'rubygems'
5
+ require 'nokogiri'
6
+ require 'kernel'
7
+ require 'object'
8
+ require 'symbol'
9
+ require 'result'
10
+ require 'element_set'
11
+
12
+ module Elementor
13
+ def elements(opts={}, &block)
14
+ namer = Result.new(self, opts, &block)
15
+ namer.define_elements!
16
+ namer.dispatcher
17
+ end
18
+ 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,51 @@
1
+ module Elementor
2
+ class Result
3
+ attr_reader :context, :opts
4
+
5
+ def initialize(context, opts={}, &block)
6
+ @opts = opts
7
+ @context = context
8
+ block.call(naming_context)
9
+ end
10
+
11
+ def parse!(markup)
12
+ doc(markup)
13
+ end
14
+
15
+ def naming_context
16
+ @naming_context ||= blank_context(:this => self) do
17
+ def method_missing(sym, *args)
18
+ @this.element_names[sym] = *args
19
+ end
20
+ end
21
+ end
22
+
23
+ def dispatcher
24
+ @dispatcher ||= blank_context(:this => self, :doc => doc) do
25
+ def method_missing(sym, *args, &block)
26
+ @this.try(sym, *args, &block) || @doc.try(sym, *args, &block) || super
27
+ end
28
+ end
29
+ end
30
+
31
+ def define_elements!
32
+ element_names.each do |name, selector|
33
+ meta_def(name) do |*mutators|
34
+ set = ElementSet.new(doc.search(selector).to_ary)
35
+ mutators.empty? ? set : mutators.inject(set) { |result, fn| fn[result] }
36
+ end
37
+ end
38
+ end
39
+
40
+ def element_names
41
+ @element_names ||= { }
42
+ end
43
+
44
+ private
45
+
46
+ def doc(markup=nil)
47
+ @doc = nil if markup
48
+ @doc ||= Nokogiri(markup || context.send(opts[:from] || :body)) rescue nil
49
+ end
50
+ end
51
+ 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
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elementor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
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 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nokogiri
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: patnakajima@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - lib/elementor
35
+ - lib/elementor.rb
36
+ - lib/elementor/spec.rb
37
+ - lib/elementor/element_set.rb
38
+ - lib/elementor/result.rb
39
+ - lib/core_ext
40
+ - lib/core_ext/object.rb
41
+ - lib/core_ext/kernel.rb
42
+ - lib/core_ext/symbol.rb
43
+ has_rdoc: false
44
+ homepage: http://github.com/pivotal/elementor
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.0
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: Prettier element traversal with Nokogiri
69
+ test_files: []
70
+