magic_options 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,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in magic_options.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ magic_options
2
+ -------------
3
+
4
+ MagicOptions is a ruby module that provides an initialize method that takes an
5
+ optional hash of options. Each key is taken as the name of an instance
6
+ variable, to which the associated value is assigned.
7
+
8
+ Example
9
+ -------
10
+
11
+ ```ruby
12
+ require 'rubygems'
13
+ require 'magic_options'
14
+
15
+ Cow.new :color => "brown", :gender => "female"
16
+ => #<Cow:0x7f77e409a6d8 @gender="female", @color="brown">
17
+ Cow.new :color => "brown", :gender => "female", :wheels => 4
18
+ => ArgumentError: Unknown option wheels for new Cow
19
+
20
+ class Gullible
21
+
22
+ include MagicOptions
23
+
24
+ end
25
+
26
+ Gullible.new :accepts => "anything"
27
+ => #<Gullible:0x7f77e407fdb0 @accepts="anything">
28
+
29
+ class Professor
30
+
31
+ include MagicOptions
32
+
33
+ magic_options :iq, :hair_style
34
+
35
+ end
36
+
37
+ Professor.new :hair_style => :einsteinian
38
+ => #<Professor:0x7f77e406d980 @hair_style=:einsteinian>
39
+ Professor.new :does_not_take => :anything
40
+ => ArgumentError: Unknown option does_not_take for new Professor
41
+ ```
42
+
43
+ Obtaining
44
+ ---------
45
+
46
+ <https://github.com/sheldonh/magic_options>
47
+
48
+ Credits
49
+ -------
50
+
51
+ Pair programmed with [@rorymckinley][c1]
52
+
53
+ [c1]: http://twitter.com/#!/rorymckinley
54
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,3 @@
1
+ module MagicOptions
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,38 @@
1
+ require "magic_options/version"
2
+
3
+ module MagicOptions
4
+
5
+ def initialize(options = {})
6
+ options.each do |option, value|
7
+ magic_options_validate(option)
8
+ instance_variable_set "@#{option}".to_sym, value
9
+ end
10
+ end
11
+
12
+ def self.included(base)
13
+ base.extend(ClassMethods)
14
+ end
15
+
16
+ module ClassMethods
17
+
18
+ attr_accessor :magic_options_allowed
19
+
20
+ def magic_options(*allowed)
21
+ self.magic_options_allowed = allowed
22
+ end
23
+
24
+ end
25
+
26
+ private
27
+
28
+ def magic_options_validate(option)
29
+ return unless allowed = self.class.magic_options_allowed
30
+ if allowed[0] == :accessors
31
+ return if respond_to?(option)
32
+ else
33
+ return if allowed.include?(option)
34
+ end
35
+ raise ArgumentError, "Unknown option #{option} in new #{self.class}"
36
+ end
37
+
38
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "magic_options/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "magic_options"
7
+ s.version = MagicOptions::VERSION
8
+ s.authors = ["Sheldon Hearn", "Rory McKinley"]
9
+ s.email = ["sheldonh@starjuice.net", "rorymckinley@gmail.com"]
10
+ s.homepage = "http://github.com/sheldonh/magic_options"
11
+ s.summary = "Ruby module to provide initialize with magic options"
12
+
13
+ s.rubyforge_project = "magic_options"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency('rspec')
21
+ end
@@ -0,0 +1,87 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe "MagicOptions" do
4
+
5
+ require 'magic_options'
6
+ class Klazz
7
+ include MagicOptions
8
+ attr_accessor :accessible
9
+ end
10
+
11
+ context "without specifying magic_options" do
12
+
13
+ it "sets instance variables for all options" do
14
+ object = Klazz.new(:color => "green")
15
+ object.instance_variables.should include("@color")
16
+ object.instance_variable_get(:@color).should == "green"
17
+ end
18
+
19
+ end
20
+
21
+ context "with magic_options :accessors" do
22
+
23
+ before(:each) do
24
+ Klazz.send :magic_options, :accessors
25
+ end
26
+
27
+ it "allows options named after accessors" do
28
+ lambda {
29
+ object = Klazz.new(:accessible => "is an accessor")
30
+ }.should_not raise_error
31
+ end
32
+
33
+ it "disallows any other option" do
34
+ lambda {
35
+ object = Klazz.new(:color => "is not an accessor")
36
+ }.should raise_error(ArgumentError)
37
+ end
38
+
39
+ end
40
+
41
+ context "with magic_options :color, :shape" do
42
+
43
+ before(:each) do
44
+ Klazz.send :magic_options, :color, :shape
45
+ end
46
+
47
+ it "allows an option called :color" do
48
+ lambda {
49
+ object = Klazz.new(:color => "green")
50
+ }.should_not raise_error
51
+ end
52
+
53
+ it "allows options an option called :shape" do
54
+ lambda {
55
+ object = Klazz.new(:shape => "round")
56
+ }.should_not raise_error
57
+ end
58
+
59
+ it "disallows any other option" do
60
+ lambda {
61
+ object = Klazz.new(:size => "is not a magic option")
62
+ }.should raise_error
63
+ end
64
+
65
+ end
66
+
67
+ context "with a disallowed option" do
68
+
69
+ before(:each) do
70
+ Klazz.send :magic_options, :allowed
71
+ end
72
+
73
+ it "raises an ArgumentError" do
74
+ lambda {
75
+ object = Klazz.new(:size => "is not a magic option")
76
+ }.should raise_error(ArgumentError)
77
+ end
78
+
79
+ it "says which class disallowed which option" do
80
+ lambda {
81
+ object = Klazz.new(:size => "is not a magic option")
82
+ }.should raise_error("Unknown option size in new Klazz")
83
+ end
84
+
85
+ end
86
+
87
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec'
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magic_options
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Sheldon Hearn
14
+ - Rory McKinley
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-09-23 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description:
36
+ email:
37
+ - sheldonh@starjuice.net
38
+ - rorymckinley@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - README.md
49
+ - Rakefile
50
+ - lib/magic_options.rb
51
+ - lib/magic_options/version.rb
52
+ - magic_options.gemspec
53
+ - spec/magic_options/magic_options_spec.rb
54
+ - spec/spec_helper.rb
55
+ homepage: http://github.com/sheldonh/magic_options
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project: magic_options
84
+ rubygems_version: 1.8.5
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Ruby module to provide initialize with magic options
88
+ test_files: []
89
+