ropet 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +1 -0
- data/.gitignore +3 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +30 -0
- data/README.md +78 -0
- data/Rakefile +5 -0
- data/lib/ropet.rb +17 -0
- data/lib/ropet/config.rb +46 -0
- data/lib/ropet/parser.rb +89 -0
- data/lib/ropet/version.rb +4 -0
- data/opts.rb +94 -0
- data/ropet.gemspec +22 -0
- data/spec/config_spec.rb +45 -0
- data/spec/parser_spec.rb +139 -0
- data/spec/ropet_helpers_spec.rb +37 -0
- data/spec/spec_helper.rb +7 -0
- metadata +100 -0
data/.autotest
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'autotest/bundler'
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format documentation
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ropet (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
metaclass (0.0.1)
|
11
|
+
mocha (0.10.0)
|
12
|
+
metaclass (~> 0.0.1)
|
13
|
+
rspec (2.7.0)
|
14
|
+
rspec-core (~> 2.7.0)
|
15
|
+
rspec-expectations (~> 2.7.0)
|
16
|
+
rspec-mocks (~> 2.7.0)
|
17
|
+
rspec-core (2.7.1)
|
18
|
+
rspec-expectations (2.7.0)
|
19
|
+
diff-lcs (~> 1.1.2)
|
20
|
+
rspec-mocks (2.7.0)
|
21
|
+
yard (0.7.4)
|
22
|
+
|
23
|
+
PLATFORMS
|
24
|
+
ruby
|
25
|
+
|
26
|
+
DEPENDENCIES
|
27
|
+
mocha (>= 0.10.0)
|
28
|
+
ropet!
|
29
|
+
rspec
|
30
|
+
yard (>= 0.7.4)
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# Ropet
|
2
|
+
|
3
|
+
Ropet is a helper library for easy integration of options in methods.
|
4
|
+
|
5
|
+
## Concept
|
6
|
+
|
7
|
+
An **option** can be a *flag*, for example:
|
8
|
+
|
9
|
+
:debug
|
10
|
+
:no_debug
|
11
|
+
|
12
|
+
or a standard *hash option*:
|
13
|
+
|
14
|
+
:debug
|
15
|
+
{threads: 3}
|
16
|
+
# or, if you prefer:
|
17
|
+
{:toolkit => :gcc}
|
18
|
+
|
19
|
+
## Quick usage
|
20
|
+
|
21
|
+
In a method accepting `*options` as the last argument, you can use:
|
22
|
+
|
23
|
+
# remember to pass `*options` (with an asterisk), because it
|
24
|
+
# passes `options.length` instead of an array in one argument
|
25
|
+
# and #parser doesn't expect one array
|
26
|
+
Ropet.parser *options
|
27
|
+
|
28
|
+
# or manual options
|
29
|
+
parser = Ropet.parser :debug, threads: 2
|
30
|
+
|
31
|
+
parser[:debug] #=> true
|
32
|
+
parser[:threads]#=> 2
|
33
|
+
|
34
|
+
parser.inject *options
|
35
|
+
parser.inject :no_debug, toolkit: :gcc
|
36
|
+
|
37
|
+
|
38
|
+
## Detailed usage
|
39
|
+
|
40
|
+
In addition to standard `*options` in `Ropet#parser`, you can also pass a block with configuration, which executes before injecting/interpreting options.
|
41
|
+
|
42
|
+
{Ropet::Config} provides a small *DSL* for basic configuration, like this:
|
43
|
+
|
44
|
+
parser = Ropet.parser :without_debug do
|
45
|
+
prefix 'without_' # flags starting with 'without_' will be interpreted
|
46
|
+
# as false booleans
|
47
|
+
default :library_path, './lib' # when `library_path` is not set,
|
48
|
+
# it will be taken from defaults list
|
49
|
+
end
|
50
|
+
|
51
|
+
parser[:debug] #=> false
|
52
|
+
parser[:library_path] #=> "./lib"
|
53
|
+
|
54
|
+
parser.inject library_path: "../library"
|
55
|
+
# `#inject` accepts the same kind of
|
56
|
+
# *options that `Ropet#parser`
|
57
|
+
|
58
|
+
parser[:library_path] #=> "../library"
|
59
|
+
|
60
|
+
parser.inject :without_without_templates
|
61
|
+
# for extreme cases, when key names include prefixes
|
62
|
+
# it is advisable to change false_prefix with dsl method
|
63
|
+
# but you probably won't have a key names no_no_*
|
64
|
+
|
65
|
+
parser.has_key? :without_templates #=> true
|
66
|
+
|
67
|
+
parser.settings #=> {:debug=>false, :library_path=>"../library", :without_templates=>false}
|
68
|
+
|
69
|
+
# you can also access defaults whenever you want,
|
70
|
+
# they're not treated in the same way as other options
|
71
|
+
# (which are overwritten in case of conflicts)
|
72
|
+
parser.config.defaults #=> {:library_path=>"./lib"}
|
73
|
+
|
74
|
+
## Copyright
|
75
|
+
|
76
|
+
Copyright © 2012 Jakub Okoński
|
77
|
+
|
78
|
+
<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">Ropet</span> by <span xmlns:cc="http://creativecommons.org/ns#" property="cc:attributionName">Jakub Okoński</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.
|
data/Rakefile
ADDED
data/lib/ropet.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "ropet/version"
|
2
|
+
require 'ropet/parser'
|
3
|
+
|
4
|
+
# Top level module holding all objects
|
5
|
+
module Ropet
|
6
|
+
# Helper for easy creation of parsers
|
7
|
+
# @param options Arguments to parse after initializing
|
8
|
+
# @param block Block to pass for {Ropet::Config.from_dsl}
|
9
|
+
# @return [Parser]
|
10
|
+
def self.parser(*options, &block)
|
11
|
+
if block
|
12
|
+
Parser.new(Config.from_dsl(&block)).tap {|o| o.inject *options }
|
13
|
+
else
|
14
|
+
Parser.new(Config.new).tap {|o| o.inject *options }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/ropet/config.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module Ropet
|
2
|
+
# Customizes parser settings
|
3
|
+
class Config
|
4
|
+
# Keeps all the defaults to use when
|
5
|
+
# there's no value assigned
|
6
|
+
# @return [Hash]
|
7
|
+
attr_reader :defaults
|
8
|
+
|
9
|
+
# Stores prefix to match false flags with
|
10
|
+
# @return [String]
|
11
|
+
attr_reader :false_prefix
|
12
|
+
|
13
|
+
# Sets @defaults and default @false_prefix
|
14
|
+
def initialize
|
15
|
+
@defaults = {}
|
16
|
+
@false_prefix = 'no_'
|
17
|
+
end
|
18
|
+
|
19
|
+
# Creates new instance of Config and executes
|
20
|
+
# dsl methods on it
|
21
|
+
# @param &block Block to execute
|
22
|
+
# @return [Config] Initialized config
|
23
|
+
def self.from_dsl(&block)
|
24
|
+
c = self.new
|
25
|
+
c.instance_exec(&block)
|
26
|
+
return c
|
27
|
+
end
|
28
|
+
|
29
|
+
# Adds a default `value` for `key`
|
30
|
+
# @param [#to_sym] Key for default
|
31
|
+
# @param [Object] Value to assign for default
|
32
|
+
# @return [nil]
|
33
|
+
def default(key, value)
|
34
|
+
@defaults[key.to_sym] = value
|
35
|
+
return nil
|
36
|
+
end
|
37
|
+
|
38
|
+
# Sets the false prefix for a parser
|
39
|
+
# @param [#to_s] Prefix
|
40
|
+
# @return [nil]
|
41
|
+
def prefix(prefix)
|
42
|
+
@false_prefix = prefix.to_s
|
43
|
+
return nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/ropet/parser.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'ropet/config'
|
2
|
+
|
3
|
+
module Ropet
|
4
|
+
# Main class for Ropet, handles all injecting logic,
|
5
|
+
# parsing flags and setting options
|
6
|
+
class Parser
|
7
|
+
# @return [Hash] Assigned options
|
8
|
+
attr_reader :settings
|
9
|
+
|
10
|
+
# @return [Config] Config
|
11
|
+
attr_reader :config
|
12
|
+
|
13
|
+
# Initializes new parser
|
14
|
+
# Sets up @settings
|
15
|
+
# @param [Config, nil] Config to use for the parser
|
16
|
+
def initialize(config = nil)
|
17
|
+
@settings = {}
|
18
|
+
@config = config || Config.new
|
19
|
+
|
20
|
+
return self
|
21
|
+
end
|
22
|
+
|
23
|
+
# Creates new Parser with a block of dsl
|
24
|
+
# methods to pass for Config initialization
|
25
|
+
# @deprecated
|
26
|
+
# @param &block Block to pass
|
27
|
+
# @return [Parser] New parser created from initialized Config
|
28
|
+
def self.let(&block)
|
29
|
+
self.new(Config.from_dsl(&block))
|
30
|
+
end
|
31
|
+
|
32
|
+
# Main method of Ropet.
|
33
|
+
# Parses options and flags and stores them
|
34
|
+
# properly.
|
35
|
+
# @param [<Symbol,String>,Hash] *options Options to parse
|
36
|
+
# @example Injecting flags mixed with hash options
|
37
|
+
# parser.inject :debug, toolchain: 'gcc-4.0'
|
38
|
+
# @return [Parser] Self
|
39
|
+
def inject(*options)
|
40
|
+
options.each do |option|
|
41
|
+
if option.is_a?(Symbol) || option.is_a?(String)
|
42
|
+
flag = option.to_s
|
43
|
+
prefix_range = 0...(config.false_prefix.length)
|
44
|
+
if flag[prefix_range] == config.false_prefix
|
45
|
+
flag.slice! prefix_range # remove prefix from string if it exists there
|
46
|
+
@settings[flag.to_sym] = false
|
47
|
+
else
|
48
|
+
@settings[option] = true
|
49
|
+
end
|
50
|
+
elsif option.is_a? Hash
|
51
|
+
@settings.merge! option
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
return self
|
56
|
+
end
|
57
|
+
|
58
|
+
# Creates new Parser with default Config.
|
59
|
+
# Calls Parser#inject on `*options`.
|
60
|
+
# @param [<Symbol,String>,Hash] *options Options to parse
|
61
|
+
# @deprecated
|
62
|
+
# @return [Parser] New Parser
|
63
|
+
def self.inject(*options)
|
64
|
+
obj = self.new(Config.new)
|
65
|
+
obj.inject options
|
66
|
+
end
|
67
|
+
|
68
|
+
# Checks existence of `key` in defined settings
|
69
|
+
# or defaults.
|
70
|
+
# @param [String,Symbol] key Key to search for
|
71
|
+
# @return [Boolean] Boolean telling if Parser has this key
|
72
|
+
def has_key?(key)
|
73
|
+
key = key.to_sym
|
74
|
+
@settings.has_key?(key) or config.defaults.has_key?(key)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Retrieves value for given `key`.
|
78
|
+
# @param [Symbol,String] index Key to search for
|
79
|
+
# @return [Object] Value stored for given key
|
80
|
+
def [](index)
|
81
|
+
index = index.to_sym
|
82
|
+
unless @settings[index] == nil
|
83
|
+
@settings[index]
|
84
|
+
else
|
85
|
+
config.defaults[index]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/opts.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
class Options
|
4
|
+
attr_reader :settings, :config
|
5
|
+
|
6
|
+
class Config
|
7
|
+
attr_reader :defaults, :false_prefix
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@defaults = {}
|
11
|
+
@false_prefix = 'no'
|
12
|
+
end
|
13
|
+
|
14
|
+
def default(key, value)
|
15
|
+
@defaults[key.to_sym] = value
|
16
|
+
end
|
17
|
+
|
18
|
+
def prefix(prefix)
|
19
|
+
@false_prefix = prefix
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(config)
|
24
|
+
@settings = {}
|
25
|
+
@config = config || Config.new
|
26
|
+
|
27
|
+
return self
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.let(&block)
|
31
|
+
config = Config.new
|
32
|
+
config.instance_exec(&block)
|
33
|
+
self.new(config)
|
34
|
+
end
|
35
|
+
|
36
|
+
def inject(*options)
|
37
|
+
options.each do |option|
|
38
|
+
if option.is_a?(Symbol) || option.is_a?(String)
|
39
|
+
flag = option.to_s
|
40
|
+
prefix_range = 0...(config.false_prefix.length)
|
41
|
+
if flag[prefix_range] == config.false_prefix
|
42
|
+
flag.slice! prefix_range # remove prefix from string if it exists there
|
43
|
+
@settings[flag.to_sym] = false
|
44
|
+
else
|
45
|
+
@settings[option] = true
|
46
|
+
end
|
47
|
+
elsif option.is_a? Hash
|
48
|
+
@settings.merge! option
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
return self
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.inject(*options)
|
56
|
+
obj = self.new(Config.new)
|
57
|
+
obj.inject options
|
58
|
+
end
|
59
|
+
|
60
|
+
def has_key?(key)
|
61
|
+
@settings.has_key?(key) or config.defaults.has_key?(key)
|
62
|
+
end
|
63
|
+
|
64
|
+
def [](index)
|
65
|
+
unless @settings[index] == nil
|
66
|
+
@settings[index]
|
67
|
+
else
|
68
|
+
config.defaults[index]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class Greeter
|
74
|
+
attr_reader :options
|
75
|
+
|
76
|
+
def initialize(*opts)
|
77
|
+
@options = Options.let{prefix 'without_'; default :name, 'Kot'}.inject(opts)
|
78
|
+
|
79
|
+
greet
|
80
|
+
end
|
81
|
+
|
82
|
+
def greet
|
83
|
+
return puts "Hello unknown" unless @options.has_key? :name
|
84
|
+
return puts "Hello noname" if @options[:name] == false
|
85
|
+
return puts "Hello #{@options[:name]}" if @options[:name].is_a? String
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
g = Greeter.new
|
90
|
+
g.options.inject :without_name; g.greet
|
91
|
+
g.options.inject :name => "Kuba"; g.greet
|
92
|
+
|
93
|
+
puts Options.inject(:nodebug).inject(threads: 3).inject(:debug => :gdb).settings ==
|
94
|
+
Options.inject(:nodebug, threads: 3, debug: :gdb).settings
|
data/ropet.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "ropet/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "ropet"
|
7
|
+
s.version = Ropet::VERSION
|
8
|
+
s.authors = ["Jakub Okoński"]
|
9
|
+
s.email = ["jakub@okonski.org"]
|
10
|
+
s.homepage = "http://github.com/farnoy/ropet"
|
11
|
+
s.summary = %q{Parse your method options easily}
|
12
|
+
s.description = %q{Ropet parses options and provides easy API for developer}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_development_dependency "rspec"
|
20
|
+
s.add_development_dependency "mocha", ">= 0.10.0"
|
21
|
+
s.add_development_dependency "yard", ">= 0.7.4"
|
22
|
+
end
|
data/spec/config_spec.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ropet::Config do
|
4
|
+
let :config do
|
5
|
+
Ropet::Config.new
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "constructor" do
|
9
|
+
it "assigns @defaults hash" do
|
10
|
+
config.instance_variable_get(:@defaults).should eq({})
|
11
|
+
end
|
12
|
+
|
13
|
+
it "assigns @false_prefix to default `no`" do
|
14
|
+
config.instance_variable_get(:@false_prefix).should eq('no_')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#default" do
|
19
|
+
it "assings default value for key" do
|
20
|
+
config.default :name, false
|
21
|
+
config.defaults.should include(name: false)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#prefix" do
|
26
|
+
it "assings prefix for false flags" do
|
27
|
+
config.prefix :without_
|
28
|
+
config.false_prefix.should eq('without_')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#from_dsl" do
|
33
|
+
it "provides simple dsl interface" do
|
34
|
+
config = Ropet::Config.from_dsl do
|
35
|
+
prefix 'without_'
|
36
|
+
default :name, false
|
37
|
+
default :age, 20
|
38
|
+
end
|
39
|
+
|
40
|
+
config.false_prefix.should == 'without_'
|
41
|
+
config.defaults.should include(name: false)
|
42
|
+
config.defaults.should include(age: 20)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/parser_spec.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ropet::Parser do
|
4
|
+
let :vanilla_parser do
|
5
|
+
Ropet::Parser.new
|
6
|
+
end
|
7
|
+
|
8
|
+
let :config do
|
9
|
+
mock("Config")
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "constructor" do
|
13
|
+
it "assigns empty hash to @settings" do
|
14
|
+
vanilla_parser.instance_variable_get(:@settings).should eq({})
|
15
|
+
end
|
16
|
+
|
17
|
+
it "assigns @config when passing argument" do
|
18
|
+
config = mock("Anything")
|
19
|
+
parser = Ropet::Parser.new(config)
|
20
|
+
parser.instance_variable_get(:@config).should be(config)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "assings @config to default value without `config` argument" do
|
24
|
+
Ropet::Config.expects(:new).returns(config)
|
25
|
+
vanilla_parser.instance_variable_get(:@config).should be(config)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "let" do
|
30
|
+
let :block do
|
31
|
+
-> {prefix "test"}
|
32
|
+
end
|
33
|
+
|
34
|
+
it "creates Config from given block with #from_dsl" do
|
35
|
+
Ropet::Config.expects(:from_dsl)
|
36
|
+
Ropet::Parser.let(&block)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "creates and returns new Parser" do
|
40
|
+
value = mock
|
41
|
+
Ropet::Config.expects(:from_dsl).returns(config)
|
42
|
+
Ropet::Parser.expects(:new).with(config).returns(value)
|
43
|
+
Ropet::Parser.let(&block).should be(value)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "static #inject" do
|
48
|
+
let :mock_parser do
|
49
|
+
mock_parser = mock
|
50
|
+
mock_parser.stubs :inject
|
51
|
+
mock_parser
|
52
|
+
end
|
53
|
+
|
54
|
+
before do
|
55
|
+
Ropet::Config.expects(:new).returns(config)
|
56
|
+
Ropet::Parser.expects(:new).with(config).returns(mock_parser)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "creates Parser with default Config" do
|
60
|
+
Ropet::Parser.inject(:test)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "triggers instanced #inject" do
|
64
|
+
mock_parser.expects(:inject).with([:test])
|
65
|
+
Ropet::Parser.inject(:test)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "instanced #inject" do
|
70
|
+
it "evaluates all arguments" do
|
71
|
+
first = mock().tap {|m| m.expects(:is_a?).times(3) }
|
72
|
+
second = first.dup
|
73
|
+
vanilla_parser.inject first, second
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "flags parser" do
|
77
|
+
it "recognizes true flag" do
|
78
|
+
vanilla_parser.inject something: true
|
79
|
+
vanilla_parser.settings.should include(:something => true)
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "with a false flag" do
|
83
|
+
it "correctly recognizes default prefix" do
|
84
|
+
vanilla_parser.inject :no_auto
|
85
|
+
vanilla_parser.settings[:auto].should eq(false)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "correctly recognizes custom prefix" do
|
89
|
+
parser = Ropet::Parser.new(Ropet::Config.from_dsl {prefix 'without_'})
|
90
|
+
parser.inject :without_auto
|
91
|
+
parser.settings.should include(:auto => false)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "assigns correct name as a key" do
|
95
|
+
vanilla_parser.inject :no_auto
|
96
|
+
vanilla_parser.settings.keys.should include(:auto)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "hash settings" do
|
102
|
+
it "merges them to existing ones" do
|
103
|
+
vanilla_parser.instance_variable_set(:@settings, {:first => false})
|
104
|
+
vanilla_parser.inject second: true
|
105
|
+
vanilla_parser.settings.should include(:first => false, :second => true)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "merges hash settings overwriting them" do
|
109
|
+
vanilla_parser.inject something: false
|
110
|
+
vanilla_parser.inject something: true
|
111
|
+
vanilla_parser.settings.should include(:something => true)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe "#has_key?" do
|
117
|
+
it "asks @settings for a key" do
|
118
|
+
vanilla_parser.settings.expects(:has_key?).once.with(:auto)
|
119
|
+
vanilla_parser.has_key? 'auto'
|
120
|
+
end
|
121
|
+
|
122
|
+
it "asks @config.defaults for a key" do
|
123
|
+
vanilla_parser.config.defaults.expects(:has_key?).once.with(:auto)
|
124
|
+
vanilla_parser.has_key? 'auto'
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "#[]" do
|
129
|
+
it "asks parsed @settings when available" do
|
130
|
+
vanilla_parser.settings.expects(:[]).with(:auto)
|
131
|
+
vanilla_parser['auto']
|
132
|
+
end
|
133
|
+
|
134
|
+
it "falls back to @config.defaults otherwise" do
|
135
|
+
vanilla_parser.config.defaults.expects(:[]).with(:auto)
|
136
|
+
vanilla_parser['auto']
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Ropet Helpers' do
|
4
|
+
describe "#parser" do
|
5
|
+
let :options do
|
6
|
+
[:debug, threads: 5]
|
7
|
+
end
|
8
|
+
|
9
|
+
it "creates a proper config when providing a block" do
|
10
|
+
config = Ropet::Config.new.tap {|c| c.defaults[:name] = "Jakub"}
|
11
|
+
Ropet::Config.expects(:new).returns(
|
12
|
+
config.tap {|o| o.expects(:instance_exec).once }
|
13
|
+
)
|
14
|
+
Ropet.parser { default :name, "Jakub" }
|
15
|
+
end
|
16
|
+
|
17
|
+
it "creates a proper parser without a block" do
|
18
|
+
double = mock().tap {|o| o.expects(:tap).returns(o) }
|
19
|
+
[:Config, :Parser].each do |sym|
|
20
|
+
Ropet.const_get(sym).expects(:new).returns(double)
|
21
|
+
end
|
22
|
+
Ropet.parser.should eq(double)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "evaluates options with a block" do
|
26
|
+
Ropet::Parser.any_instance.expects(:inject).with(*options)
|
27
|
+
Ropet.parser *options do
|
28
|
+
default :name, "Jakub"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "evaluates options without a block" do
|
33
|
+
Ropet::Parser.any_instance.expects(:inject).with(*options)
|
34
|
+
Ropet.parser *options
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ropet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.rc1
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jakub Okoński
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &27968640 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *27968640
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mocha
|
27
|
+
requirement: &27967900 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.10.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *27967900
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: yard
|
38
|
+
requirement: &27967400 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.7.4
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *27967400
|
47
|
+
description: Ropet parses options and provides easy API for developer
|
48
|
+
email:
|
49
|
+
- jakub@okonski.org
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .autotest
|
55
|
+
- .gitignore
|
56
|
+
- .rspec
|
57
|
+
- Gemfile
|
58
|
+
- Gemfile.lock
|
59
|
+
- README.md
|
60
|
+
- Rakefile
|
61
|
+
- lib/ropet.rb
|
62
|
+
- lib/ropet/config.rb
|
63
|
+
- lib/ropet/parser.rb
|
64
|
+
- lib/ropet/version.rb
|
65
|
+
- opts.rb
|
66
|
+
- ropet.gemspec
|
67
|
+
- spec/config_spec.rb
|
68
|
+
- spec/parser_spec.rb
|
69
|
+
- spec/ropet_helpers_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
homepage: http://github.com/farnoy/ropet
|
72
|
+
licenses: []
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>'
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 1.3.1
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.12
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Parse your method options easily
|
95
|
+
test_files:
|
96
|
+
- spec/config_spec.rb
|
97
|
+
- spec/parser_spec.rb
|
98
|
+
- spec/ropet_helpers_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
has_rdoc:
|