detagger 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
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"
13
+ gem 'simplecov', :platforms => :mri_19
14
+ gem 'rcov', :platforms => :mri_18
15
+ end
16
+
17
+ gem "facets"
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 christfo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = detagger
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to detagger
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 christfo. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "detagger"
18
+ gem.homepage = "http://github.com/christfo/detagger"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{a library to 'detag' strings}
21
+ gem.description = %Q{This is intended to be used as a mixin, usualy with some kind of library that parses
22
+ optional command line arguments. The feature here is that the options may contain tags ('example_tag:') that
23
+ are not resolved until they are used. This means that one option may refer to another. An example might be:
24
+ --stage /tmp/fred --logfile stage:/my.log
25
+ which would later provide a method 'logfile' that would return '/tmp/fred/my.log' }
26
+ gem.email = "chris.fordy@gmail.com"
27
+ gem.authors = ["christfo"]
28
+ # dependencies defined in Gemfile
29
+ end
30
+ Jeweler::RubygemsDotOrgTasks.new
31
+
32
+ require 'rspec/core'
33
+ require 'rspec/core/rake_task'
34
+ RSpec::Core::RakeTask.new(:spec) do |spec|
35
+ spec.pattern = FileList['spec/**/*_spec.rb']
36
+ end
37
+
38
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
39
+ spec.pattern = 'spec/**/*_spec.rb'
40
+ spec.rcov = true
41
+ end
42
+
43
+ task :default => :spec
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "detagger #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "detagger"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["christfo"]
12
+ s.date = "2012-03-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
+ s.email = "chris.fordy@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "detagger.gemspec",
28
+ "lib/detagger.rb",
29
+ "spec/detagger_spec.rb",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = "http://github.com/christfo/detagger"
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = "1.8.10"
36
+ s.summary = "a library to 'detag' strings"
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
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"])
47
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
48
+ s.add_development_dependency(%q<rcov>, [">= 0"])
49
+ else
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"])
55
+ s.add_dependency(%q<simplecov>, [">= 0"])
56
+ s.add_dependency(%q<rcov>, [">= 0"])
57
+ end
58
+ else
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"])
64
+ s.add_dependency(%q<simplecov>, [">= 0"])
65
+ s.add_dependency(%q<rcov>, [">= 0"])
66
+ end
67
+ end
68
+
@@ -0,0 +1,80 @@
1
+ require 'rubygems'
2
+ require 'facets/string'
3
+
4
+ ##
5
+ # Mixin to allow paths and options to be described as 'tags:' that are only pocessed
6
+ # when called for. the tag is satisfied by calls to self
7
+ module Detagger
8
+ attr_accessor :tagex
9
+
10
+ def drill_down_ref(txt,target = self)
11
+ return unless txt
12
+ 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
19
+ end
20
+ end
21
+
22
+ def detag( value, target = self, resolved = [] )
23
+ @tagex ||= /([^\/:]+:)/
24
+
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] )
36
+ else
37
+ mtch
38
+ end
39
+ end
40
+ if (value.uniq == [nil])
41
+ value = nil
42
+ else
43
+ value = value.join
44
+ end
45
+ end
46
+ value
47
+ end
48
+
49
+ def method_missing( method, *args, &blk )
50
+ access, orig_method = *method.to_s.scan(/^(detag|raw)_(.+)$/).flatten
51
+ unless orig_method && self.respond_to?(orig_method.to_sym )
52
+ super(method,*args,&blk)
53
+ else
54
+ rawval = self.send( orig_method, *args, &blk )
55
+ (access == "detag") ? detag( rawval ) : rawval
56
+ end
57
+ end
58
+
59
+ def unless_flag( flag, &blk )
60
+ unless self.send("detag_#{flag}")
61
+ yield blk
62
+ else
63
+ puts "\033[31m!! Skipping step due to #{flag.to_s.quote} being true...\033[0m"
64
+ nil
65
+ end
66
+ end
67
+
68
+ def if_flag( flag, &blk )
69
+ if self.send( "detag_#{flag}" )
70
+ yield blk
71
+ else
72
+ puts "\033[31m!! Skipping step due to #{flag.to_s.quote} being false...\033[0m"
73
+ nil
74
+ end
75
+ end
76
+ end
77
+
78
+
79
+
80
+
@@ -0,0 +1,87 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Detagger do
4
+ before( :each ) do
5
+ @options = Class.new do
6
+ def bob; "henry" end
7
+ def sue; "mary" end
8
+ def alfy; "sue:" end
9
+ def jess; "sue:/bob:some text/bob:text" end
10
+ def david; "persue:" end
11
+ def kate; "kate:" end
12
+ def rachel; "phebs:" end
13
+ def phebs; "rachel:" end
14
+ def greg; "re/rachel:/henry:/fdgfd/ " end
15
+ def jenny; ":alfy:sue:sue:david:" end
16
+
17
+ def fred_; "fred:" end
18
+ def al; "al" end
19
+ def alfred; "BAD" end
20
+ def bert; "al:fred:/al:fred_:" end
21
+
22
+ def jim; "" end
23
+ def soph; nil end
24
+ def matt; "soph:" end
25
+ def mike; "sue:soph:sue:jim:" end
26
+
27
+ include Detagger
28
+ end.new
29
+ # @options.extend Detagger
30
+ end
31
+
32
+
33
+ it "has a detag mand drill_down_ref method" do
34
+ @options. should respond_to :detag
35
+ @options. should respond_to :drill_down_ref
36
+ end
37
+
38
+ it "will give a value for inputs" do
39
+ @options.bob.should == "henry"
40
+ @options.sue.should == "mary"
41
+ end
42
+
43
+ it "will raise when a method is missing" do
44
+ lambda { @options.broken }.should raise_error
45
+ end
46
+
47
+ it "will late bind 'tag' references" do
48
+ @options.detag_alfy.should_not == "sue:"
49
+ @options.detag_alfy.should == @options.sue
50
+ end
51
+
52
+ it "will provide readers for raw values of late bind 'tag' references" do
53
+ @options.raw_alfy.should == "sue:"
54
+ @options.raw_alfy.should_not == @options.sue
55
+ end
56
+
57
+ it "will support multiple tags in a value" do
58
+ @options.detag_jess.should == "mary/henrysome text/henrytext"
59
+ end
60
+
61
+ it "will pass through unrecognised tags a plain text" do
62
+ @options.detag_david.should == "persue:"
63
+ end
64
+
65
+ it "will raise when circular references are found" do
66
+ lambda { @options.detag_kate }.should raise_error "Self Reference"
67
+ lambda { @options.detag_phebs }.should raise_error "Circular Reference"
68
+ lambda { @options.detag_greg }.should raise_error "Circular Reference"
69
+ end
70
+
71
+ it "will not be confused by multiple same tags" do
72
+ @options.detag_jenny.should == ":marymarymarypersue:"
73
+ end
74
+
75
+ it "will not compose new tags from resolved tags" do
76
+ @options.detag_bert.should_not == "BAD/BAD"
77
+ @options.detag_bert.should == "alfred:/alfred:"
78
+ end
79
+
80
+ it "will gracefully handle flag values" do
81
+ @options.raw_jim.should == ""
82
+ @options.raw_soph.should == nil
83
+ @options.detag_soph.should == nil
84
+ @options.detag_matt.should == nil
85
+ @options.detag_mike.should == "marymary"
86
+ end
87
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'detagger'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: detagger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - christfo
9
+ autorequire:
10
+ bindir: bin
11
+ 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'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2152557820
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &2152557020 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.8.0
33
+ type: :development
34
+ 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'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2152556340
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &2152555680 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2152555680
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &2152554980 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.8.3
66
+ type: :development
67
+ 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'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *2152554200
80
+ - !ruby/object:Gem::Dependency
81
+ name: rcov
82
+ requirement: &2152553520 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ 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' "
97
+ email: chris.fordy@gmail.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files:
101
+ - LICENSE.txt
102
+ - README.rdoc
103
+ files:
104
+ - .document
105
+ - .rspec
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ - Rakefile
110
+ - VERSION
111
+ - detagger.gemspec
112
+ - lib/detagger.rb
113
+ - spec/detagger_spec.rb
114
+ - spec/spec_helper.rb
115
+ homepage: http://github.com/christfo/detagger
116
+ licenses:
117
+ - MIT
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ segments:
129
+ - 0
130
+ hash: 130063977220471087
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 1.8.10
140
+ signing_key:
141
+ specification_version: 3
142
+ summary: a library to 'detag' strings
143
+ test_files: []