optionize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [maiha@wota.jp]
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.
data/README ADDED
@@ -0,0 +1,52 @@
1
+ Optionize
2
+ =========
3
+
4
+ argument helper
5
+
6
+
7
+ Usage
8
+ =====
9
+
10
+ Optionize can extract args with various formats like this.
11
+
12
+ user("maiha", 14)
13
+ user(:name => 'maiha', :age => 14)
14
+ user("maiha", :age => 14)
15
+
16
+
17
+ In this case, we define 'user' method as following
18
+
19
+ require 'optionize'
20
+ def user(*args)
21
+ opts = Optionize.new(args, :name, :age)
22
+ opts[:name] # => "maiha"
23
+ opts.name # => "maiha"
24
+ opts[:age] # => 14
25
+
26
+
27
+ Extensions
28
+ ==========
29
+
30
+ Extend Array class to respond to optionize method
31
+
32
+ require 'optionize/array'
33
+ opts = ['maiha'].optionize(:name, :age)
34
+ opts.name # => 'maiha'
35
+ opts.age # => nil
36
+
37
+ Install
38
+ =======
39
+
40
+ gem install optionize
41
+
42
+
43
+ Homepage
44
+ ========
45
+
46
+ git://github.com/maiha/optionize.git
47
+
48
+
49
+ Author
50
+ ======
51
+ Maiha <maiha@wota.jp>
52
+
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ GEM_NAME = "optionize"
5
+ AUTHOR = "maiha"
6
+ EMAIL = "maiha@wota.jp"
7
+ HOMEPAGE = "http://github.com/maiha/optionize"
8
+ SUMMARY = "method argument utils"
9
+ GEM_VERSION = "0.1.0"
10
+
11
+ spec = Gem::Specification.new do |s|
12
+ s.rubyforge_project = 'asakusarb'
13
+ s.executables = []
14
+ s.name = GEM_NAME
15
+ s.version = GEM_VERSION
16
+ s.platform = Gem::Platform::RUBY
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = ["README", "MIT-LICENSE"]
19
+ s.summary = SUMMARY
20
+ s.description = s.summary
21
+ s.author = AUTHOR
22
+ s.email = EMAIL
23
+ s.homepage = HOMEPAGE
24
+ s.require_path = 'lib'
25
+ s.files = %w(MIT-LICENSE README Rakefile) + Dir.glob("{lib,spec,app,public,stubs}/**/*")
26
+ end
27
+
28
+ Rake::GemPackageTask.new(spec) do |pkg|
29
+ pkg.gem_spec = spec
30
+ end
31
+
32
+ desc "Install the gem"
33
+ task :install do
34
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
35
+ end
36
+
37
+ desc "Uninstall the gem"
38
+ task :uninstall do
39
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
40
+ end
41
+
42
+ desc "Create a gemspec file"
43
+ task :gemspec do
44
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
45
+ file.puts spec.to_ruby
46
+ end
47
+ end
48
+
49
+ require 'spec/rake/spectask'
50
+ desc 'Default: run spec examples'
51
+ task :default => 'spec'
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def optionize(*args)
3
+ Optionize.new(self, *args)
4
+ end
5
+ end
data/lib/optionize.rb ADDED
@@ -0,0 +1,46 @@
1
+ class Optionize
2
+ attr_reader :names
3
+
4
+ def initialize(given, *names)
5
+ static = names.last.respond_to?(:merge) ? names.pop : {}
6
+ dynamic = given.last.respond_to?(:merge) ? given.pop : {}
7
+
8
+ @names = names.map{|i| i.to_s}
9
+ @hash = {}
10
+
11
+ # static
12
+ static.each_pair{|key, val| self[key] = val }
13
+
14
+ # dynamic
15
+ dynamic.each_pair{|key, val| self[key] = val }
16
+
17
+ # given
18
+ given.each_with_index do |value, i|
19
+ key = names[i] or
20
+ raise IndexError, "expected %d args but got %s %s" % [names.size, i+1, given.inspect]
21
+ self[key] = value
22
+ end
23
+ end
24
+
25
+ def [](key)
26
+ @hash[key.to_s]
27
+ end
28
+
29
+ def []=(key, val)
30
+ @hash[key.to_s] = val
31
+ end
32
+
33
+ def named_values
34
+ names.map{|name| self[name]}
35
+ end
36
+
37
+ private
38
+ def method_missing(name, *args)
39
+ if name.to_s =~ /(.*)=$/
40
+ self[$1] = args.first
41
+ else
42
+ self[name]
43
+ end
44
+ end
45
+ end
46
+
@@ -0,0 +1,27 @@
1
+ module Spec
2
+ module Example
3
+ module Subject
4
+ module ExampleGroupMethods
5
+ def its(*args, &block)
6
+ describe(its_pretty_title(args)) do
7
+ define_method(:subject) { super().send(*args) }
8
+ it(&block)
9
+ end
10
+ end
11
+
12
+ private
13
+ def its_pretty_title(args)
14
+ return args.first if args.size == 1
15
+ key = args.first.to_s
16
+ vals = args[1..-1]
17
+ if key == '[]'
18
+ vals.inspect
19
+ else
20
+ "%s(%s)" % [key, vals.map{|i|i.to_s}.join(', ')]
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,102 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
2
+
3
+ describe Optionize do
4
+ describe ".new" do
5
+ it "should raise ArgumentError when no args given" do
6
+ lambda {
7
+ Optionize.new
8
+ }.should raise_error(ArgumentError)
9
+ end
10
+
11
+ it "should accept array" do
12
+ lambda {
13
+ Optionize.new([])
14
+ }.should_not raise_error(ArgumentError)
15
+ end
16
+ end
17
+
18
+ describe ".new(['maiha',14], :name, :age)" do
19
+ subject { Optionize.new(['maiha',14], :name, :age) }
20
+
21
+ ######################################################################
22
+ ### Mash access
23
+
24
+ its(:[], "name") { should == 'maiha' }
25
+ its(:[], :name ) { should == 'maiha' }
26
+
27
+ its(:[], "age" ) { should == 14 }
28
+ its(:[], :age ) { should == 14 }
29
+
30
+ ######################################################################
31
+ ### Named access
32
+
33
+ its(:name) { should == 'maiha' }
34
+ its(:age ) { should == 14 }
35
+
36
+ its(:no_such_method) { should == nil }
37
+
38
+ ######################################################################
39
+ ### Given names
40
+
41
+ its (:names) { should == ['name', 'age'] } # should be strings
42
+ end
43
+
44
+
45
+ ######################################################################
46
+ ### Mash for runtime values
47
+
48
+ describe ".new([{:name=>'maiha'}], :name, :age)" do
49
+ subject { Optionize.new([{:name=>'maiha'}], :name, :age) }
50
+ its(:name) { should == 'maiha' }
51
+ end
52
+
53
+ describe ".new([{'name'=>'maiha'}], :name, :age)" do
54
+ subject { Optionize.new([{'name'=>'maiha'}], :name, :age) }
55
+ its(:name) { should == 'maiha' }
56
+ end
57
+
58
+ describe ".new([{:name=>'maiha'}], 'name', 'age')" do
59
+ subject { Optionize.new([{:name=>'maiha'}], 'name', 'age') }
60
+ its(:name) { should == 'maiha' }
61
+ end
62
+
63
+ ######################################################################
64
+ ### Hash and Array for given values
65
+
66
+ describe ".new(['maiha', {:age=>14}], :name, :age)" do
67
+ subject { Optionize.new(['maiha', {:age=>14}], :name, :age) }
68
+ its(:name ) { should == 'maiha' }
69
+ its("name") { should == 'maiha' }
70
+ its(:age ) { should == 14 }
71
+ its("age" ) { should == 14 }
72
+ end
73
+
74
+ ######################################################################
75
+ ### Default values
76
+
77
+ describe ".new([], :name, :age, {:name=>'maiha'})" do
78
+ subject { Optionize.new([], :name, :age, {:name=>'maiha'}) }
79
+ its(:name ) { should == 'maiha' }
80
+ its("name") { should == 'maiha' }
81
+ end
82
+
83
+ ######################################################################
84
+ ### Overwrite default values
85
+
86
+ describe ".new(['nksk'], :name, :age, {:name=>'maiha'})" do
87
+ subject { Optionize.new(['nksk'], :name, :age, {:name=>'maiha'}) }
88
+ its(:name ) { should == 'nksk' }
89
+ its("name") { should == 'nksk' }
90
+ its(:age ) { should == nil }
91
+ its("age" ) { should == nil }
92
+ end
93
+
94
+ describe ".new([:age=>16], :name, :age, {:name=>'maiha'})" do
95
+ subject { Optionize.new([:age=>16], :name, :age, {:name=>'maiha'}) }
96
+ its(:name ) { should == 'maiha' }
97
+ its("name") { should == 'maiha' }
98
+ its(:age ) { should == 16 }
99
+ its("age" ) { should == 16 }
100
+ end
101
+
102
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec'
2
+
3
+ require File.join(File.dirname(__FILE__), '/../lib/optionize')
4
+ require File.join(File.dirname(__FILE__), '/its_helper')
@@ -0,0 +1,29 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
2
+
3
+ describe Array do
4
+ def enable
5
+ code = File.read(File.join(File.dirname(__FILE__), '/../lib/optionize/array.rb')){}
6
+ eval(code)
7
+ end
8
+
9
+ def disable
10
+ Array.send(:remove_method, :optionize) rescue nil
11
+ end
12
+
13
+ it "should not provide #optionize" do
14
+ disable
15
+ [].should_not respond_to(:optionize)
16
+ end
17
+
18
+ it "should provide #optionize when required" do
19
+ enable
20
+ [].should respond_to(:optionize)
21
+ end
22
+
23
+ describe "#optionize" do
24
+ before { enable }
25
+ subject { ['maiha'].optionize(:name) }
26
+ its(:class) { should == Optionize }
27
+ its(:name) { should == 'maiha' }
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: optionize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - maiha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-01 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: method argument utils
17
+ email: maiha@wota.jp
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - MIT-LICENSE
25
+ files:
26
+ - MIT-LICENSE
27
+ - README
28
+ - Rakefile
29
+ - lib/optionize/array.rb
30
+ - lib/optionize.rb
31
+ - spec/optionize_spec.rb
32
+ - spec/its_helper.rb
33
+ - spec/spec_helper.rb
34
+ - spec/syntax_spec.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/maiha/optionize
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project: asakusarb
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: method argument utils
63
+ test_files: []
64
+