houdinirx 0.0.1 → 0.0.2

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.
data/README CHANGED
@@ -20,10 +20,9 @@ Houdini.define(:word, /\w+/)
20
20
  Call hmatch or hscan on a string and build your expression pieces with the r() method by defining the expression inline or using a pre-defined expression. Then build your match results with the m( ) method.
21
21
 
22
22
  data = t.hmatch do
23
- r("amount", /\d+[,\d]*\.\d+/)
24
- r("phone_number", /\(\d{3}\)\s+\d{3}-\d{4}/)
25
- r("city", :word)
26
- r("state", :word)
23
+ r(/\d+[,\d]*\.\d+/, "amount")
24
+ r(/\(\d{3}\)\s+\d{3}-\d{4}/, "phone_number")
25
+ r(:word, "city", "state")
27
26
  m("\\$(amount) (phone_number) (city), (state)")
28
27
  end
29
28
 
@@ -10,18 +10,32 @@ module Houdini
10
10
  @captures = captures.collect {|c| c.to_sym }
11
11
  end
12
12
 
13
+ def to_hash
14
+ {}.tap do |attributes|
15
+ captures.each { |c| attributes[c] = value_for(c) }
16
+ end
17
+ end
18
+
13
19
  def method_missing(method, *args, &block)
14
- if @captures.include?(method)
15
- index = @captures.index(method)
16
- if @match.class == MatchData
17
- @match.captures[index]
18
- else
19
- @match[index]
20
- end
20
+ value = value_for(method)
21
+
22
+ if value
23
+ return value
21
24
  else
22
25
  raise RuntimeError, "#{method} not captured"
23
26
  end
24
27
  end
25
28
 
29
+ protected
30
+ def value_for(name)
31
+ index = @captures.index(name)
32
+ return nil if index == nil
33
+ if @match.class == MatchData
34
+ @match.captures[index]
35
+ else
36
+ @match[index]
37
+ end
38
+ end
39
+
26
40
  end
27
41
  end
@@ -10,9 +10,9 @@ module Houdini
10
10
 
11
11
  def r(*args)
12
12
  if args.size == 2
13
- @expressions << [args[0], args[1]]
13
+ @expressions << [args[1], args[0]]
14
14
  else
15
- expression = args.pop
15
+ expression = args.shift
16
16
  args.each do |name|
17
17
  @expressions << [name, expression]
18
18
  end
@@ -42,6 +42,7 @@ module Houdini
42
42
  protected
43
43
  def replace_expressions!(regex)
44
44
  @expressions.each do |name, r|
45
+ name = name.to_s
45
46
  if regex =~ /\(#{name}\)/
46
47
  @captures << name
47
48
  end
@@ -1,3 +1,3 @@
1
1
  module Houdini
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: houdinirx
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Eric Campbell
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-08-01 00:00:00 Z
19
+ date: 2011-08-03 00:00:00 Z
20
20
  dependencies: []
21
21
 
22
22
  description: A ruby DSL for building long complex regular expressions used frequently across large data extraction projects