nakajima-elementor 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/core_ext/object.rb +19 -0
- data/lib/core_ext/symbol.rb +7 -0
- data/lib/elementor/result.rb +18 -8
- data/lib/elementor.rb +3 -4
- metadata +3 -10
@@ -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
|
data/lib/elementor/result.rb
CHANGED
@@ -1,10 +1,17 @@
|
|
1
1
|
module Elementor
|
2
2
|
class Result
|
3
|
-
|
3
|
+
attr_reader :context, :opts
|
4
|
+
|
5
|
+
def initialize(context, opts={}, &block)
|
6
|
+
@opts = opts
|
4
7
|
@context = context
|
5
8
|
block.call(naming_context)
|
6
9
|
end
|
7
10
|
|
11
|
+
def parse!(markup)
|
12
|
+
doc(markup)
|
13
|
+
end
|
14
|
+
|
8
15
|
def naming_context
|
9
16
|
@naming_context ||= blank_context(:this => self) do
|
10
17
|
def method_missing(sym, *args)
|
@@ -14,13 +21,9 @@ module Elementor
|
|
14
21
|
end
|
15
22
|
|
16
23
|
def dispatcher
|
17
|
-
@dispatcher ||= blank_context(:this => self, :
|
24
|
+
@dispatcher ||= blank_context(:this => self, :doc => doc) do
|
18
25
|
def method_missing(sym, *args, &block)
|
19
|
-
@this.try(sym, *args, &block) || @
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.method_missing(sym, *args)
|
23
|
-
p sym
|
26
|
+
@this.try(sym, *args, &block) || @doc.try(sym, *args, &block) || super
|
24
27
|
end
|
25
28
|
end
|
26
29
|
end
|
@@ -28,7 +31,7 @@ module Elementor
|
|
28
31
|
def define_elements!
|
29
32
|
element_names.each do |name, selector|
|
30
33
|
meta_def(name) do |*mutators|
|
31
|
-
set = ElementSet.new(
|
34
|
+
set = ElementSet.new(doc.search(selector).to_ary)
|
32
35
|
mutators.empty? ? set : mutators.inject(set) { |result, fn| fn[result] }
|
33
36
|
end
|
34
37
|
end
|
@@ -37,5 +40,12 @@ module Elementor
|
|
37
40
|
def element_names
|
38
41
|
@element_names ||= { }
|
39
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
|
40
50
|
end
|
41
51
|
end
|
data/lib/elementor.rb
CHANGED
@@ -3,16 +3,15 @@ $LOAD_PATH << File.dirname(__FILE__) + '/elementor'
|
|
3
3
|
|
4
4
|
require 'rubygems'
|
5
5
|
require 'nokogiri'
|
6
|
-
require 'nakajima'
|
7
6
|
require 'kernel'
|
7
|
+
require 'object'
|
8
|
+
require 'symbol'
|
8
9
|
require 'result'
|
9
10
|
require 'element_set'
|
10
11
|
|
11
12
|
module Elementor
|
12
13
|
def elements(opts={}, &block)
|
13
|
-
opts
|
14
|
-
doc = Nokogiri(send(opts[:from]))
|
15
|
-
namer = Result.new(doc, &block)
|
14
|
+
namer = Result.new(self, opts, &block)
|
16
15
|
namer.define_elements!
|
17
16
|
namer.dispatcher
|
18
17
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nakajima-elementor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pat Nakajima
|
@@ -12,15 +12,6 @@ cert_chain: []
|
|
12
12
|
date: 2008-11-19 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
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
15
|
- !ruby/object:Gem::Dependency
|
25
16
|
name: nokogiri
|
26
17
|
version_requirement:
|
@@ -45,7 +36,9 @@ files:
|
|
45
36
|
- lib/elementor/element_set.rb
|
46
37
|
- lib/elementor/result.rb
|
47
38
|
- lib/core_ext
|
39
|
+
- lib/core_ext/object.rb
|
48
40
|
- lib/core_ext/kernel.rb
|
41
|
+
- lib/core_ext/symbol.rb
|
49
42
|
has_rdoc: false
|
50
43
|
homepage: http://github.com/nakajima/elementor
|
51
44
|
post_install_message:
|