strukt 0.0.1 → 0.0.2

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.
Files changed (3) hide show
  1. data/lib/strukt.rb +18 -28
  2. data/spec/strukt_spec.rb +8 -3
  3. metadata +2 -2
data/lib/strukt.rb CHANGED
@@ -1,41 +1,31 @@
1
1
  class Strukt
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
 
4
4
  include Enumerable
5
5
 
6
- class << self
7
- alias_method :subclass_new, :new
8
- end
9
-
10
6
  def self.new(*members)
11
- Class.new(self) do
7
+ Class.new do
12
8
  attr_accessor *members
13
9
 
14
- def self.new(*args, &block)
15
- subclass_new(*args, &block)
10
+ include Enumerable
11
+
12
+ const_set(:MEMBERS, members.dup.freeze)
13
+
14
+ def initialize(params = {})
15
+ params.each { |k, v| send("#{k}=", v) }
16
16
  end
17
17
 
18
- @members = members.dup.freeze
19
- end
20
- end
18
+ def ==(other)
19
+ other.is_a?(self.class) && to_hash == other.to_hash
20
+ end
21
21
 
22
- def self.members
23
- @members.dup
24
- end
25
-
26
- def initialize(params = {})
27
- params.each { |k, v| send("#{k}=", v) }
28
- end
22
+ def to_hash
23
+ {}.tap { |h| self.class::MEMBERS.each { |m| h[m] = send(m) } }
24
+ end
29
25
 
30
- def ==(other)
31
- other.is_a?(self.class) && to_hash == other.to_hash
32
- end
33
-
34
- def to_hash
35
- {}.tap { |h| self.class.members.each { |m| h[m] = send(m) } }
36
- end
37
-
38
- def each(*args, &block)
39
- to_hash.each(*args, &block)
26
+ def each(*args, &block)
27
+ to_hash.each(*args, &block)
28
+ end
29
+ end
40
30
  end
41
31
  end
data/spec/strukt_spec.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  require "bundler/setup"
2
2
 
3
3
  describe Strukt do
4
- Person = Strukt.new(:name, :age)
4
+ class Person < Strukt.new(:name, :age)
5
+ end
5
6
 
6
7
  describe "#initialize" do
7
8
  it "accepts values via hash in the constructor" do
@@ -53,9 +54,9 @@ describe Strukt do
53
54
  end
54
55
  end
55
56
 
56
- describe ".members" do
57
+ describe "::MEMBERS" do
57
58
  it "is the list of member variables" do
58
- expect(Person.members).to eq([:name, :age])
59
+ expect(Person::MEMBERS).to eq([:name, :age])
59
60
  end
60
61
  end
61
62
 
@@ -64,5 +65,9 @@ describe Strukt do
64
65
  person = Person.new(:name => "bob", :age => 50)
65
66
  expect(person.to_hash).to eq({:name => "bob", :age => 50})
66
67
  end
68
+
69
+ it "is frozen" do
70
+ expect { Person::MEMBERS << :height }.to raise_error(/can't modify frozen/)
71
+ end
67
72
  end
68
73
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strukt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-08 00:00:00.000000000 Z
12
+ date: 2014-05-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec