hash_struct 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +12 -0
- data/lib/hash_struct.rb +22 -0
- data/spec/hash_struct_spec.rb +32 -0
- metadata +56 -0
data/README.textile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
h1. About
|
2
|
+
|
3
|
+
HashStruct is similar to Struct from Ruby standard library, the difference is that HashStruct.new creates a class which takes a hash with attributes rather than just positional arguments as Struct.new does.
|
4
|
+
|
5
|
+
The point is that this is behaviour which all the model have, so you can use HashStruct to test thinks which expect something what quacks like a model class.
|
6
|
+
|
7
|
+
h1. Usage
|
8
|
+
|
9
|
+
<pre>
|
10
|
+
User = HashStruct.new(:first_name, :last_name)
|
11
|
+
@user = User.new(first_name: "Jakub", last_name: "Stastny")
|
12
|
+
</pre>
|
data/lib/hash_struct.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class HashStruct
|
4
|
+
# User = HashStruct.new(:first_name, :last_name)
|
5
|
+
# @user = User.new(first_name: "Jakub", last_name: "Stastny")
|
6
|
+
def self.new(*attributes)
|
7
|
+
raise ArgumentError, "you have to specify some attributes" if attributes.empty?
|
8
|
+
Class.new do
|
9
|
+
# setup accessors
|
10
|
+
attributes.each do |attribute|
|
11
|
+
attr_accessor attribute
|
12
|
+
end
|
13
|
+
|
14
|
+
# initialize
|
15
|
+
def initialize(attributes = Hash.new)
|
16
|
+
attributes.each do |attribute, value|
|
17
|
+
self.send("#{attribute}=", value)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "spec"
|
4
|
+
require_relative "../lib/hash_struct"
|
5
|
+
|
6
|
+
describe HashStruct do
|
7
|
+
before(:each) do
|
8
|
+
@struct = HashStruct.new(:first_name, :last_name)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe ".new" do
|
12
|
+
it "should raise ArgumentError if any attributes given" do
|
13
|
+
lambda { HashStruct.new }.should raise_error(ArgumentError)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should create a new class" do
|
17
|
+
@struct.class.should eql(Class)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should description" do
|
21
|
+
instance = @struct.new(first_name: "Jakub", last_name: "Stastny")
|
22
|
+
instance.first_name.should eql("Jakub")
|
23
|
+
instance.last_name.should eql("Stastny")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should create instance method writer and reader method" do
|
27
|
+
instance = @struct.new
|
28
|
+
instance.first_name = "Jakub"
|
29
|
+
instance.first_name.should eql("Jakub")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Jakub \xC5\xA0\xC5\xA5astn\xC3\xBD aka Botanicus"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
date: 2009-11-28 00:00:00 +00:00
|
12
|
+
default_executable:
|
13
|
+
dependencies: []
|
14
|
+
|
15
|
+
description: ""
|
16
|
+
email: knava.bestvinensis@gmail.com
|
17
|
+
executables: []
|
18
|
+
|
19
|
+
extensions: []
|
20
|
+
|
21
|
+
extra_rdoc_files: []
|
22
|
+
|
23
|
+
files:
|
24
|
+
- README.textile
|
25
|
+
- lib/hash_struct.rb
|
26
|
+
- spec/hash_struct_spec.rb
|
27
|
+
has_rdoc: true
|
28
|
+
homepage: http://github.com/botanicus/hash_struct
|
29
|
+
licenses: []
|
30
|
+
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.9.1
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project: hash_struct
|
51
|
+
rubygems_version: 1.3.5
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: HashStruct is similar to Struct from Ruby standard library, the difference is that HashStruct.new creates a class which takes a hash with attributes rather than just positional arguments as Struct.new does.
|
55
|
+
test_files: []
|
56
|
+
|