optruct 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +24 -0
- data/Rakefile +1 -0
- data/lib/optruct.rb +23 -0
- data/optruct.gemspec +19 -0
- data/spec/optruct_spec.rb +65 -0
- metadata +61 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
Optruct
|
2
|
+
===========
|
3
|
+
|
4
|
+
Optruct provides simple Hash-Struct mapping feature.
|
5
|
+
|
6
|
+
It is designed for largely handling given arguments to method as options.
|
7
|
+
|
8
|
+
Use cases
|
9
|
+
----------
|
10
|
+
|
11
|
+
class Stuff
|
12
|
+
include Optruct
|
13
|
+
|
14
|
+
def initialize(a, b, opts={})
|
15
|
+
@option = optruct :ignore_blank, :auto_detect, :force do
|
16
|
+
set :ignore_blank => true, :auto_detect => false, :force => false
|
17
|
+
set opts
|
18
|
+
end
|
19
|
+
if @option.auto_detect
|
20
|
+
# do something
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/optruct.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Optruct
|
2
|
+
|
3
|
+
def self.define(*syms, &block)
|
4
|
+
o = Struct.new(*syms).new
|
5
|
+
o.instance_eval do
|
6
|
+
def set(h={})
|
7
|
+
h.each do |k, v|
|
8
|
+
raise "#{k} is not defined" unless members.include?(k)
|
9
|
+
self[k] = v
|
10
|
+
end
|
11
|
+
self
|
12
|
+
end
|
13
|
+
end
|
14
|
+
o.instance_eval &block if block_given?
|
15
|
+
o
|
16
|
+
end
|
17
|
+
|
18
|
+
def optruct(*syms, &block)
|
19
|
+
Optruct.define(*syms, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
data/optruct.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "optruct"
|
6
|
+
s.version = '0.0.1'
|
7
|
+
s.authors = ["Aiga Matsuo"]
|
8
|
+
s.email = ["mtaiga@gmail.com"]
|
9
|
+
s.homepage = "https://github.com/eiger1976"
|
10
|
+
s.summary = %q{Optruct provides simple Hash-Struct mapping feature.}
|
11
|
+
s.description = %q{Optruct is designed for largely handling given arguments to method as options.}
|
12
|
+
|
13
|
+
s.rubyforge_project = "optruct"
|
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
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.expand_path('../../lib/optruct.rb', __FILE__)
|
3
|
+
|
4
|
+
describe Optruct do
|
5
|
+
|
6
|
+
it "should not be defined blank" do
|
7
|
+
lambda { Optruct.define }.should raise_error
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should accept multiple attribute" do
|
11
|
+
lambda { Optruct.define(:a, :b, :c) }.should_not raise_error
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be accessible to defined attributes" do
|
15
|
+
o = Optruct.define(:name, :age, :sex)
|
16
|
+
o.name.should == nil
|
17
|
+
o.age.should == nil
|
18
|
+
o.sex.should == nil
|
19
|
+
|
20
|
+
o.name = 'John'
|
21
|
+
o.age = 20
|
22
|
+
o.sex = :male
|
23
|
+
o.name.should == 'John'
|
24
|
+
o.age.should == 20
|
25
|
+
o.sex.should == :male
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be accessible to defined attributes with block" do
|
29
|
+
o = Optruct.define(:name, :age, :sex) do
|
30
|
+
set :name => 'Mary', :age => 19, :sex => :female
|
31
|
+
end
|
32
|
+
o.name.should == 'Mary'
|
33
|
+
o.age.should == 19
|
34
|
+
o.sex.should == :female
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should raise error by access to undefined attribute" do
|
38
|
+
o = Optruct.define(:a, :b, :c)
|
39
|
+
lambda { o.d }.should raise_error
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should be able to Module#include" do
|
43
|
+
class Stuff
|
44
|
+
include Optruct
|
45
|
+
attr_reader :option
|
46
|
+
def initialize(a, b, opts={})
|
47
|
+
@option = optruct :op1, :op2, :op3 do
|
48
|
+
set :op1 => 1, :op2 => 2, :op3 => 3 # default values
|
49
|
+
set opts
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
s = Stuff.new(:a, :b)
|
55
|
+
s.option.op1.should == 1
|
56
|
+
s.option.op2.should == 2
|
57
|
+
s.option.op3.should == 3
|
58
|
+
|
59
|
+
s = Stuff.new(:a, :b, :op1 => 0, :op2 => 1, :op3 => 2)
|
60
|
+
s.option.op1.should == 0
|
61
|
+
s.option.op2.should == 1
|
62
|
+
s.option.op3.should == 2
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: optruct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Aiga Matsuo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-22 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Optruct is designed for largely handling given arguments to method as options.
|
17
|
+
email:
|
18
|
+
- mtaiga@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- Gemfile
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- lib/optruct.rb
|
31
|
+
- optruct.gemspec
|
32
|
+
- spec/optruct_spec.rb
|
33
|
+
homepage: https://github.com/eiger1976
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project: optruct
|
56
|
+
rubygems_version: 1.8.5
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Optruct provides simple Hash-Struct mapping feature.
|
60
|
+
test_files: []
|
61
|
+
|