hash_struct 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -1,13 +1,13 @@
1
1
  h1. About
2
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.
3
+ @HashStruct@ is similar to @Struct@ from Ruby standard library, the difference is that @HashStruct.generate@ creates a class which takes a hash with attributes rather than just positional arguments as @Struct.new@ does.
4
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.
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
6
 
7
7
  h1. Usage
8
8
 
9
9
  <pre>
10
- User = HashStruct.new(:first_name, :last_name)
10
+ User = HashStruct.generate(:first_name, :last_name)
11
11
  @user = User.new(first_name: "Jakub", last_name: "Stastny")
12
12
  </pre>
13
13
 
@@ -19,14 +19,16 @@ module TestMixin
19
19
  end
20
20
 
21
21
  HashStruct.extend(TestMixin)
22
- User = HashStruct.new(:first_name, :last_name)
22
+ User = HashStruct.generate(:first_name, :last_name)
23
23
  User.say_hi
24
24
  # => "Hi!"
25
25
  </pre>
26
26
 
27
27
  <pre>
28
28
  HashStruct.send(:include, TestMixin)
29
- User = HashStruct.new(:first_name, :last_name)
29
+ User = HashStruct.generate(:first_name, :last_name)
30
30
  User.new.say_hi
31
31
  # => "Hi!"
32
32
  </pre>
33
+
34
+ You may want to create your custom class inhterited from @HashStruct@ in case you want to extend it.
data/lib/hash_struct.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  class HashStruct
4
- # User = HashStruct.new(:first_name, :last_name)
4
+ # User = HashStruct.generate(:first_name, :last_name)
5
5
  # @user = User.new(first_name: "Jakub", last_name: "Stastny")
6
- def self.new(*attributes)
6
+ def self.generate(*attributes)
7
7
  raise ArgumentError, "you have to specify some attributes" if attributes.empty?
8
8
 
9
9
  included = self.included_modules
10
10
  extended = (class << self; self; end).included_modules
11
11
 
12
- Class.new do
12
+ Class.new(self) do
13
13
  # setup accessors
14
14
  attributes.each do |attribute|
15
15
  attr_accessor attribute
@@ -21,20 +21,6 @@ class HashStruct
21
21
  self.send("#{attribute}=", value)
22
22
  end
23
23
  end
24
-
25
- # NOTE: second possible implementation (maybe better) is Class.new(self)
26
- # but then we have to used the original new method, so the object will be properly created
27
- # include modules
28
- (included - self.included_modules).each do |mixin|
29
- puts "Including #{mixin}"
30
- self.send(:include, mixin)
31
- end
32
-
33
- # extended modules
34
- (extended - (class << self; self; end).included_modules).each do |mixin|
35
- puts "Extending #{mixin}"
36
- self.extend(mixin)
37
- end
38
24
  end
39
25
  end
40
26
  end
@@ -5,12 +5,12 @@ require_relative "../lib/hash_struct"
5
5
 
6
6
  describe HashStruct do
7
7
  before(:each) do
8
- @struct = HashStruct.new(:first_name, :last_name)
8
+ @struct = HashStruct.generate(:first_name, :last_name)
9
9
  end
10
10
 
11
- describe ".new" do
11
+ describe ".generate" do
12
12
  it "should raise ArgumentError if any attributes given" do
13
- lambda { HashStruct.new }.should raise_error(ArgumentError)
13
+ lambda { HashStruct.generate }.should raise_error(ArgumentError)
14
14
  end
15
15
 
16
16
  it "should create a new class" do
@@ -38,13 +38,13 @@ describe HashStruct do
38
38
 
39
39
  it "should inherit all mixins of HashStruct" do
40
40
  @hs.send(:include, @mixin)
41
- @struct = @hs.new(:first_name, :last_name)
41
+ @struct = @hs.generate(:first_name, :last_name)
42
42
  @struct.instance_methods.should include(:say_hi)
43
43
  end
44
44
 
45
45
  it "should be extended by all mixins of HashStruct" do
46
46
  @hs.extend(@mixin)
47
- @struct = @hs.new(:first_name, :last_name)
47
+ @struct = @hs.generate(:first_name, :last_name)
48
48
  @struct.methods.should include(:say_hi)
49
49
  end
50
50
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jakub \xC5\xA0\xC5\xA5astn\xC3\xBD aka Botanicus"
@@ -51,6 +51,6 @@ rubyforge_project: hash_struct
51
51
  rubygems_version: 1.3.5
52
52
  signing_key:
53
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.
54
+ summary: HashStruct is similar to Struct from Ruby standard library, the difference is that HashStruct.generate creates a class which takes a hash with attributes rather than just positional arguments as Struct.new does.
55
55
  test_files: []
56
56