detagger 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -6,10 +6,10 @@ source "http://rubygems.org"
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
9
- gem "rspec", "~> 2.8.0"
10
- gem "rdoc", "~> 3.12"
11
- gem "bundler", "~> 1.0.0"
12
- gem "jeweler", "~> 1.8.3"
9
+ gem "rspec", ">= 2.8.0"
10
+ gem "rdoc", ">= 3.12"
11
+ gem "bundler", ">= 1.0.0"
12
+ gem "jeweler", ">= 1.8.3"
13
13
  gem 'simplecov', :platforms => :mri_19
14
14
  gem 'rcov', :platforms => :mri_18
15
15
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "detagger"
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["christfo"]
12
- s.date = "2012-03-13"
12
+ s.date = "2012-03-14"
13
13
  s.description = "This is intended to be used as a mixin, usualy with some kind of library that parses\n optional command line arguments. The feature here is that the options may contain tags ('example_tag:') that\n are not resolved until they are used. This means that one option may refer to another. An example might be:\n --stage /tmp/fred --logfile stage:/my.log \n which would later provide a method 'logfile' that would return '/tmp/fred/my.log' "
14
14
  s.email = "chris.fordy@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -40,27 +40,27 @@ Gem::Specification.new do |s|
40
40
 
41
41
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
42
  s.add_runtime_dependency(%q<facets>, [">= 0"])
43
- s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
44
- s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
45
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
46
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
43
+ s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
44
+ s.add_development_dependency(%q<rdoc>, [">= 3.12"])
45
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
46
+ s.add_development_dependency(%q<jeweler>, [">= 1.8.3"])
47
47
  s.add_development_dependency(%q<simplecov>, [">= 0"])
48
48
  s.add_development_dependency(%q<rcov>, [">= 0"])
49
49
  else
50
50
  s.add_dependency(%q<facets>, [">= 0"])
51
- s.add_dependency(%q<rspec>, ["~> 2.8.0"])
52
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
53
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
- s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
51
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
52
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
53
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
54
+ s.add_dependency(%q<jeweler>, [">= 1.8.3"])
55
55
  s.add_dependency(%q<simplecov>, [">= 0"])
56
56
  s.add_dependency(%q<rcov>, [">= 0"])
57
57
  end
58
58
  else
59
59
  s.add_dependency(%q<facets>, [">= 0"])
60
- s.add_dependency(%q<rspec>, ["~> 2.8.0"])
61
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
62
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
63
- s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
60
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
61
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
62
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
63
+ s.add_dependency(%q<jeweler>, [">= 1.8.3"])
64
64
  s.add_dependency(%q<simplecov>, [">= 0"])
65
65
  s.add_dependency(%q<rcov>, [">= 0"])
66
66
  end
@@ -7,51 +7,64 @@ require 'facets/string'
7
7
  module Detagger
8
8
  attr_accessor :tagex
9
9
 
10
- def drill_down_ref(txt,target = self)
10
+ # create a chain of objects that will be used to discover the value behind the tags.
11
+ # This will be ordered, favouring the first item in preference to later ones.
12
+ def set_detag_chain(*args)
13
+ @detag_chain = args
14
+ end
15
+
16
+ def detag_chain
17
+ (@detag_chain || self)
18
+ end
19
+
20
+ def drill_down_ref(txt,targets = detag_chain)
11
21
  return unless txt
12
22
  parm = txt.chomp(":").to_sym
13
- begin
14
- new_txt = target.send(parm)
15
- raise "Self Reference" if new_txt == txt
16
- new_txt
17
- rescue NoMethodError
18
- txt
23
+ [*targets].each do |target|
24
+ begin
25
+ new_txt = target.send(parm)
26
+ raise "Self Reference" if new_txt == txt
27
+ return new_txt
28
+ rescue NoMethodError
29
+ end
19
30
  end
31
+ txt
20
32
  end
21
33
 
22
- def detag( value, target = self, resolved = [] )
34
+ def detag( value, targets = detag_chain, resolved = [] )
23
35
  @tagex ||= /([^\/:]+:)/
36
+ [*targets].each do |target|
37
+ if ( value.respond_to? :call )
38
+ # manipulates self so that it is called with 'this' and not he context of the proc when defined
39
+ value = target.instance_eval &value
40
+ end
24
41
 
25
- if ( value.respond_to? :call )
26
- # manipulates self so that it is called with 'this' and not he context of the proc when defined
27
- value = target.instance_eval &value
28
- end
29
-
30
- if value && value.is_a?(String)
31
- value = value.shatter(@tagex).flatten.map do |mtch|
32
- if mtch =~ @tagex
33
- raise "Circular Reference" if resolved.include? mtch
34
- new_value = drill_down_ref(mtch,target)
35
- mtch == new_value ? new_value : detag(new_value, target, resolved + [mtch] )
42
+ if value && value.is_a?(String)
43
+ value = value.shatter(@tagex).flatten.map do |mtch|
44
+ if mtch =~ @tagex
45
+ raise "Circular Reference" if resolved.include? mtch
46
+ new_value = drill_down_ref(mtch,targets)
47
+ mtch == new_value ? new_value : detag(new_value, [target], resolved + [mtch] )
48
+ else
49
+ mtch
50
+ end
51
+ end
52
+ if (value.uniq == [nil])
53
+ value = nil
36
54
  else
37
- mtch
55
+ value = value.join
38
56
  end
39
57
  end
40
- if (value.uniq == [nil])
41
- value = nil
42
- else
43
- value = value.join
44
- end
45
58
  end
46
59
  value
47
60
  end
48
61
 
49
62
  def method_missing( method, *args, &blk )
50
63
  access, orig_method = *method.to_s.scan(/^(detag|raw)_(.+)$/).flatten
51
- unless orig_method && self.respond_to?(orig_method.to_sym )
64
+ unless orig_method && target = [*detag_chain].find {|t| t.respond_to?( orig_method.to_sym )}
52
65
  super(method,*args,&blk)
53
66
  else
54
- rawval = self.send( orig_method, *args, &blk )
67
+ rawval = target.send( orig_method, *args, &blk )
55
68
  (access == "detag") ? detag( rawval ) : rawval
56
69
  end
57
70
  end
@@ -1,9 +1,10 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require "ostruct"
2
3
 
3
4
  describe Detagger do
4
5
  before( :each ) do
5
6
  @options = Class.new do
6
- def bob; "henry" end
7
+ def bob; "henry:" end
7
8
  def sue; "mary" end
8
9
  def alfy; "sue:" end
9
10
  def jess; "sue:/bob:some text/bob:text" end
@@ -36,7 +37,7 @@ describe Detagger do
36
37
  end
37
38
 
38
39
  it "will give a value for inputs" do
39
- @options.bob.should == "henry"
40
+ @options.bob.should == "henry:"
40
41
  @options.sue.should == "mary"
41
42
  end
42
43
 
@@ -55,7 +56,7 @@ describe Detagger do
55
56
  end
56
57
 
57
58
  it "will support multiple tags in a value" do
58
- @options.detag_jess.should == "mary/henrysome text/henrytext"
59
+ @options.detag_jess.should == "mary/henry:some text/henry:text"
59
60
  end
60
61
 
61
62
  it "will pass through unrecognised tags a plain text" do
@@ -82,6 +83,31 @@ describe Detagger do
82
83
  @options.raw_soph.should == nil
83
84
  @options.detag_soph.should == nil
84
85
  @options.detag_matt.should == nil
86
+ end
87
+
88
+ it "will absorbe nil values inside a string" do
85
89
  @options.detag_mike.should == "marymary"
86
90
  end
91
+
92
+ it "will search for tags down a cahin of objects" do
93
+ second = OpenStruct.new
94
+ second.nick = "Found!"
95
+ @options.set_detag_chain( @options, second )
96
+ @options.detag_nick.should == "Found!"
97
+ end
98
+
99
+ it "will look for tags in strict chain order" do
100
+ second = OpenStruct.new
101
+ second.sue = "not this one"
102
+ @options.set_detag_chain( @options, second )
103
+ @options.detag_sue.should_not == "not this one"
104
+ @options.detag_sue.should == "mary"
105
+ end
106
+
107
+ it "will compose missing tags from both sources" do
108
+ second = OpenStruct.new
109
+ second.henry = "correct"
110
+ @options.set_detag_chain( @options, second )
111
+ @options.detag_bob.should == "correct"
112
+ end
87
113
  end
metadata CHANGED
@@ -1,106 +1,137 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: detagger
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - christfo
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-03-13 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: facets
16
- requirement: &2152557820 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
17
+
18
+ date: 2012-03-14 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
22
21
  type: :runtime
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ version_requirements: *id001
32
+ name: facets
23
33
  prerelease: false
24
- version_requirements: *2152557820
25
- - !ruby/object:Gem::Dependency
26
- name: rspec
27
- requirement: &2152557020 !ruby/object:Gem::Requirement
34
+ - !ruby/object:Gem::Dependency
35
+ type: :development
36
+ requirement: &id002 !ruby/object:Gem::Requirement
28
37
  none: false
29
- requirements:
30
- - - ~>
31
- - !ruby/object:Gem::Version
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: 47
42
+ segments:
43
+ - 2
44
+ - 8
45
+ - 0
32
46
  version: 2.8.0
33
- type: :development
47
+ version_requirements: *id002
48
+ name: rspec
34
49
  prerelease: false
35
- version_requirements: *2152557020
36
- - !ruby/object:Gem::Dependency
37
- name: rdoc
38
- requirement: &2152556340 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ~>
42
- - !ruby/object:Gem::Version
43
- version: '3.12'
50
+ - !ruby/object:Gem::Dependency
44
51
  type: :development
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 31
58
+ segments:
59
+ - 3
60
+ - 12
61
+ version: "3.12"
62
+ version_requirements: *id003
63
+ name: rdoc
45
64
  prerelease: false
46
- version_requirements: *2152556340
47
- - !ruby/object:Gem::Dependency
48
- name: bundler
49
- requirement: &2152555680 !ruby/object:Gem::Requirement
65
+ - !ruby/object:Gem::Dependency
66
+ type: :development
67
+ requirement: &id004 !ruby/object:Gem::Requirement
50
68
  none: false
51
- requirements:
52
- - - ~>
53
- - !ruby/object:Gem::Version
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 23
73
+ segments:
74
+ - 1
75
+ - 0
76
+ - 0
54
77
  version: 1.0.0
55
- type: :development
78
+ version_requirements: *id004
79
+ name: bundler
56
80
  prerelease: false
57
- version_requirements: *2152555680
58
- - !ruby/object:Gem::Dependency
59
- name: jeweler
60
- requirement: &2152554980 !ruby/object:Gem::Requirement
81
+ - !ruby/object:Gem::Dependency
82
+ type: :development
83
+ requirement: &id005 !ruby/object:Gem::Requirement
61
84
  none: false
62
- requirements:
63
- - - ~>
64
- - !ruby/object:Gem::Version
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 49
89
+ segments:
90
+ - 1
91
+ - 8
92
+ - 3
65
93
  version: 1.8.3
66
- type: :development
94
+ version_requirements: *id005
95
+ name: jeweler
67
96
  prerelease: false
68
- version_requirements: *2152554980
69
- - !ruby/object:Gem::Dependency
70
- name: simplecov
71
- requirement: &2152554200 !ruby/object:Gem::Requirement
72
- none: false
73
- requirements:
74
- - - ! '>='
75
- - !ruby/object:Gem::Version
76
- version: '0'
97
+ - !ruby/object:Gem::Dependency
77
98
  type: :development
78
- prerelease: false
79
- version_requirements: *2152554200
80
- - !ruby/object:Gem::Dependency
81
- name: rcov
82
- requirement: &2152553520 !ruby/object:Gem::Requirement
99
+ requirement: &id006 !ruby/object:Gem::Requirement
83
100
  none: false
84
- requirements:
85
- - - ! '>='
86
- - !ruby/object:Gem::Version
87
- version: '0'
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ version_requirements: *id006
109
+ name: simplecov
110
+ prerelease: false
111
+ - !ruby/object:Gem::Dependency
88
112
  type: :development
113
+ requirement: &id007 !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ version_requirements: *id007
123
+ name: rcov
89
124
  prerelease: false
90
- version_requirements: *2152553520
91
- description: ! "This is intended to be used as a mixin, usualy with some kind of library
92
- that parses\n optional command line arguments. The feature here is that the options
93
- may contain tags ('example_tag:') that\n are not resolved until they are used.
94
- This means that one option may refer to another. An example might be:\n --stage
95
- /tmp/fred --logfile stage:/my.log \n which would later provide a method 'logfile'
96
- that would return '/tmp/fred/my.log' "
125
+ description: "This is intended to be used as a mixin, usualy with some kind of library that parses\n optional command line arguments. The feature here is that the options may contain tags ('example_tag:') that\n are not resolved until they are used. This means that one option may refer to another. An example might be:\n --stage /tmp/fred --logfile stage:/my.log \n which would later provide a method 'logfile' that would return '/tmp/fred/my.log' "
97
126
  email: chris.fordy@gmail.com
98
127
  executables: []
128
+
99
129
  extensions: []
100
- extra_rdoc_files:
130
+
131
+ extra_rdoc_files:
101
132
  - LICENSE.txt
102
133
  - README.rdoc
103
- files:
134
+ files:
104
135
  - .document
105
136
  - .rspec
106
137
  - Gemfile
@@ -113,31 +144,37 @@ files:
113
144
  - spec/detagger_spec.rb
114
145
  - spec/spec_helper.rb
115
146
  homepage: http://github.com/christfo/detagger
116
- licenses:
147
+ licenses:
117
148
  - MIT
118
149
  post_install_message:
119
150
  rdoc_options: []
120
- require_paths:
151
+
152
+ require_paths:
121
153
  - lib
122
- required_ruby_version: !ruby/object:Gem::Requirement
154
+ required_ruby_version: !ruby/object:Gem::Requirement
123
155
  none: false
124
- requirements:
125
- - - ! '>='
126
- - !ruby/object:Gem::Version
127
- version: '0'
128
- segments:
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ hash: 3
160
+ segments:
129
161
  - 0
130
- hash: 130063977220471087
131
- required_rubygems_version: !ruby/object:Gem::Requirement
162
+ version: "0"
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
164
  none: false
133
- requirements:
134
- - - ! '>='
135
- - !ruby/object:Gem::Version
136
- version: '0'
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ hash: 3
169
+ segments:
170
+ - 0
171
+ version: "0"
137
172
  requirements: []
173
+
138
174
  rubyforge_project:
139
175
  rubygems_version: 1.8.10
140
176
  signing_key:
141
177
  specification_version: 3
142
178
  summary: a library to 'detag' strings
143
179
  test_files: []
180
+