quarrel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Christian Bradley
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Quarrel
2
+
3
+ Simple named parameters for Ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'quarrel'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install quarrel
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => [:test]
4
+ task :test do
5
+ $LOAD_PATH.unshift "lib", "spec"
6
+ Dir.glob("./spec/**/*_spec.rb") { |f| require f }
7
+ end
@@ -0,0 +1,7 @@
1
+ module Quarrel
2
+ module Duck
3
+ def self.symbolic? subject
4
+ subject.respond_to?(:to_sym) && subject.to_sym == subject
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ module Quarrel
2
+
3
+ class Iterator
4
+ attr_reader :position
5
+
6
+ def initialize subject
7
+ @subject = subject
8
+ rewind
9
+ end
10
+
11
+ def rewind
12
+ @position = -1
13
+ end
14
+
15
+ def next?
16
+ position < @subject.size-1
17
+ end
18
+
19
+ def next
20
+ raise StopIteration, "Iteration reached an end" unless next?
21
+ @position += 1
22
+ @subject.at position
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,78 @@
1
+ require 'singleton'
2
+
3
+ module Quarrel
4
+
5
+ module Parameter
6
+
7
+ class UnsetValue
8
+ include Singleton
9
+ end
10
+
11
+ def self.unset? value
12
+ value == UnsetValue.instance
13
+ end
14
+
15
+ def self.required name
16
+ Required.new name
17
+ end
18
+
19
+ def self.optional name
20
+ Optional.new name
21
+ end
22
+
23
+ def self.defaulted name, default
24
+ Defaulted.new name, default
25
+ end
26
+
27
+ module Base
28
+ attr_reader :name
29
+
30
+ def initialize name
31
+ @name = name
32
+ end
33
+
34
+ def apply data
35
+ data.has_key?(name) && set(data[name]) or unset
36
+ end
37
+
38
+ def set value
39
+ value
40
+ end
41
+
42
+ def unset
43
+ nil
44
+ end
45
+
46
+ end
47
+
48
+ class Required
49
+ include Base
50
+
51
+ def unset
52
+ raise ArgumentError, "No value provided for required parameter :#{name}"
53
+ end
54
+ end
55
+
56
+ class Optional
57
+ include Base
58
+
59
+ def unset
60
+ UnsetValue.instance
61
+ end
62
+ end
63
+
64
+ class Defaulted
65
+ include Base
66
+
67
+ def initialize name, default
68
+ @name, @default = name, default
69
+ end
70
+
71
+ def unset
72
+ @default
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+ end
@@ -0,0 +1,51 @@
1
+ require 'quarrel/duck'
2
+ require 'quarrel/iterator'
3
+
4
+ module Quarrel
5
+
6
+ class Parser
7
+
8
+ def self.parse words, factory
9
+ new(words, factory).parse
10
+ end
11
+
12
+ attr_reader :words, :factory
13
+
14
+ def initialize words, factory
15
+ @words = words
16
+ @factory = factory
17
+ end
18
+
19
+ def parse
20
+ i = Quarrel::Iterator.new @words
21
+ results = []
22
+
23
+ loop do
24
+ word = i.next
25
+
26
+ if Quarrel::Duck.symbolic?(word) && name = word[/^([a-z_]\w*)$/, 1]
27
+ results << factory.required(name.to_sym)
28
+
29
+ elsif Quarrel::Duck.symbolic?(word) && name = word[/^([a-z_]\w*)\?$/, 1]
30
+ results << factory.optional(name.to_sym)
31
+
32
+ elsif Quarrel::Duck.symbolic?(word) && name = word[/^([a-z_]\w*)\=$/, 1]
33
+ raise SyntaxError,
34
+ "Expected default value for :#{name} " +
35
+ "at position #{i.position+1}: #{words.inspect}" unless i.next?
36
+
37
+ results << factory.defaulted(name.to_sym, i.next)
38
+ else
39
+ raise SyntaxError,
40
+ "Expected parameter specification at position #{i.position}, " +
41
+ "got #{word.inspect}: #{words.inspect}"
42
+ end
43
+
44
+ end
45
+
46
+ results
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,3 @@
1
+ module Quarrel
2
+ VERSION = "0.0.1"
3
+ end
data/lib/quarrel.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'quarrel/version'
2
+ require 'quarrel/parser'
3
+ require 'quarrel/parameter'
4
+
5
+ module Quarrel
6
+
7
+ def self.unset? value
8
+ Quarrel::Parameter.unset? value
9
+ end
10
+
11
+ def self.values data, *args
12
+ Quarrel::Controller.new(data, *args).values
13
+ end
14
+
15
+ def self.set_instance_variables target, data, *args
16
+ Quarrel::Controller.new(data, *args).set_instance_variables target
17
+ end
18
+
19
+ class Controller
20
+ attr_reader :data, :args
21
+
22
+ def initialize data, *args
23
+ raise ArgumentError,
24
+ "Must provide one or more specifications for named parameters." if args.empty?
25
+ @data, @args = data, args
26
+ end
27
+
28
+ def params
29
+ Quarrel::Parser.parse(args, Quarrel::Parameter)
30
+ end
31
+
32
+ def values
33
+ params.each_with_object(data).map &:apply
34
+ end
35
+
36
+ def pairs
37
+ params.each_with_object(data).map do |param, data|
38
+ [param.name, param.apply(data)]
39
+ end
40
+ end
41
+
42
+ def set_instance_variables target
43
+ pairs.each do |(name, value)|
44
+ target.instance_variable_set :"@#{name}", value
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+ end
data/quarrel.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'quarrel/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "quarrel"
8
+ spec.version = Quarrel::VERSION
9
+ spec.authors = ["Christian Bradley"]
10
+ spec.email = ["christianbradley@gmail.com"]
11
+ spec.description = %q{Simple named parameters for ruby}
12
+ spec.summary = %q{Simple named parameters for ruby}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "mocha"
24
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+ require 'quarrel/duck'
3
+
4
+ describe "Duck" do
5
+
6
+ it "determines whether or not a value is symbolic" do
7
+ Quarrel::Duck.symbolic?(:foo?).must_equal true
8
+ Quarrel::Duck.symbolic?(":foo?").must_equal false
9
+ end
10
+
11
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require 'quarrel/iterator'
3
+
4
+ describe "Iterator" do
5
+
6
+ let(:array) { %w(The rain in Spain) }
7
+ let(:iterator) { Quarrel::Iterator.new array }
8
+
9
+ subject { iterator }
10
+
11
+ it "retrieves the next value in the array" do
12
+ subject.next.must_equal "The"
13
+ subject.next.must_equal "rain"
14
+ subject.next.must_equal "in"
15
+ subject.next.must_equal "Spain"
16
+ end
17
+
18
+ it "knows whether or not there is a next value" do
19
+ assert subject.next?, "Must have a next value"
20
+ 4.times { subject.next }
21
+ refute subject.next?, "Should not have a next value"
22
+ end
23
+
24
+ it "raises when it reaches the end" do
25
+ 4.times { subject.next }
26
+ e = lambda { subject.next }.must_raise StopIteration
27
+ end
28
+
29
+ it "rewinds" do
30
+ 4.times { subject.next }
31
+ subject.rewind
32
+ subject.next.must_equal "The"
33
+ end
34
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'quarrel/parameter'
3
+
4
+ describe "Required Parameter" do
5
+
6
+ subject { Quarrel::Parameter.required :city }
7
+
8
+ it "sets the value from supplied data" do
9
+ subject.apply(:city => "Austin").must_equal "Austin"
10
+ end
11
+
12
+ it "can be set" do
13
+ subject.set("Austin").must_equal "Austin"
14
+ end
15
+
16
+ it "cannot be unset" do
17
+ e = lambda { subject.unset }.must_raise ArgumentError
18
+ end
19
+
20
+ end
21
+
22
+ describe "Optional Parameter" do
23
+
24
+ subject { Quarrel::Parameter.optional :city }
25
+
26
+ it "can be unset" do
27
+ Quarrel::Parameter.unset?(subject.unset).must_equal true
28
+ end
29
+
30
+ end
31
+
32
+ describe "Defaulted Parameter" do
33
+ subject { Quarrel::Parameter.defaulted :city, "Austin" }
34
+
35
+ it "uses the default value when unset" do
36
+ subject.unset.must_equal "Austin"
37
+ end
38
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'quarrel/parser'
3
+
4
+ describe "Parser" do
5
+ let(:city) { stub :city }
6
+ let(:state) { stub :state }
7
+ let(:zip) { stub :zip }
8
+ let(:country) { stub :country }
9
+ let(:factory) { mock }
10
+ let(:words) { [:city, :state, :zip?, :country=, "US"] }
11
+ let(:parser) { Quarrel::Parser.new words, factory }
12
+
13
+ before do
14
+ factory.stubs(:required)
15
+ factory.stubs(:optional)
16
+ factory.stubs(:defaulted)
17
+ end
18
+
19
+ it "parses words into associated parameter types" do
20
+ factory.expects(:required).with(:city).returns city
21
+ factory.expects(:required).with(:state).returns state
22
+ factory.expects(:optional).with(:zip).returns zip
23
+ factory.expects(:defaulted).with(:country, "US").returns country
24
+ parser.parse.must_equal [city, state, zip, country]
25
+ end
26
+
27
+ describe "no default value provided" do
28
+ let(:words) { [:city, :state, :zip?, :country=] }
29
+
30
+ it "raises a syntax error" do
31
+ e = lambda { parser.parse }.must_raise SyntaxError
32
+ end
33
+ end
34
+
35
+ describe "invalid syntax" do
36
+ let(:words) { [:city, "some value", :state, :zip?] }
37
+ it "raises a syntax error" do
38
+ e = lambda { parser.parse }.must_raise SyntaxError
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'quarrel'
3
+
4
+ describe "Quarrel" do
5
+
6
+ it "returns values from named parameters" do
7
+ city, state, zip, country = Quarrel.values({
8
+ :city => "Austin", :state => "TX", :zip => "78756"
9
+ }, :city, :state, :zip?, :country=, "US")
10
+
11
+ city.must_equal "Austin"
12
+ state.must_equal "TX"
13
+ zip.must_equal "78756"
14
+ country.must_equal "US"
15
+ end
16
+
17
+ it "sets instance variables from named parameters" do
18
+ instance = mock
19
+ instance.expects(:instance_variable_set).with(:@city, "Austin")
20
+ instance.expects(:instance_variable_set).with(:@state, "TX")
21
+ instance.expects(:instance_variable_set).with(:@zip, "78756")
22
+ instance.expects(:instance_variable_set).with(:@country, "US")
23
+
24
+ Quarrel.set_instance_variables(instance, {
25
+ :city => "Austin", :state => "TX", :zip => "78756"
26
+ }, :city, :state, :zip?, :country=, "US")
27
+ end
28
+
29
+ it "checks for unset values" do
30
+ city, state, zip, country = Quarrel.values({
31
+ :city => "Austin", :state => "TX"
32
+ }, :city, :state, :zip?, :country=, "US")
33
+
34
+ Quarrel.unset?(zip).must_equal true
35
+ end
36
+
37
+ it "raises when parameters are unspecified" do
38
+ e = lambda { Quarrel.values :foo => "bar" }.must_raise ArgumentError
39
+ e.message.must_equal "Must provide one or more specifications for named parameters."
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'mocha/setup'
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quarrel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christian Bradley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mocha
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Simple named parameters for ruby
63
+ email:
64
+ - christianbradley@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/quarrel.rb
75
+ - lib/quarrel/duck.rb
76
+ - lib/quarrel/iterator.rb
77
+ - lib/quarrel/parameter.rb
78
+ - lib/quarrel/parser.rb
79
+ - lib/quarrel/version.rb
80
+ - quarrel.gemspec
81
+ - spec/quarrel/duck_spec.rb
82
+ - spec/quarrel/iterator_spec.rb
83
+ - spec/quarrel/parameter_spec.rb
84
+ - spec/quarrel/parser_spec.rb
85
+ - spec/quarrel_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ segments:
101
+ - 0
102
+ hash: 1841157489760787547
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
110
+ - 0
111
+ hash: 1841157489760787547
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.25
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Simple named parameters for ruby
118
+ test_files:
119
+ - spec/quarrel/duck_spec.rb
120
+ - spec/quarrel/iterator_spec.rb
121
+ - spec/quarrel/parameter_spec.rb
122
+ - spec/quarrel/parser_spec.rb
123
+ - spec/quarrel_spec.rb
124
+ - spec/spec_helper.rb