entity 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.
- data/README.md +6 -0
- data/init.rb +1 -18
- data/lib/entity.rb +24 -0
- data/spec/entity_spec.rb +3 -3
- metadata +2 -1
data/README.md
CHANGED
data/init.rb
CHANGED
@@ -1,21 +1,4 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
require 'set'
|
3
|
-
|
4
|
-
class Entity < OpenStruct
|
5
|
-
alias attributes marshal_dump
|
6
|
-
@@ensured_attributes = Set.new
|
7
|
-
|
8
|
-
def initialize(attributes = {})
|
9
|
-
super(attributes)
|
10
|
-
@@ensured_attributes.each do |key|
|
11
|
-
unless attributes.has_key?(key)
|
12
|
-
self.public_send("#{key}=", nil)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.ensure_attributes(*attribute_names)
|
18
|
-
@@ensured_attributes.merge(attribute_names)
|
19
|
-
end
|
20
|
-
end
|
3
|
+
require 'entity'
|
21
4
|
|
data/lib/entity.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
class Entity < OpenStruct
|
2
|
+
alias attributes marshal_dump
|
3
|
+
|
4
|
+
def initialize(attributes = {})
|
5
|
+
super(attributes)
|
6
|
+
self.class.ensured_attributes.each do |key|
|
7
|
+
unless attributes.has_key?(key)
|
8
|
+
self.public_send("#{key}=", nil)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def ensure_attributes(*attribute_names)
|
15
|
+
@ensured_attributes ||= Set.new
|
16
|
+
@ensured_attributes.merge(attribute_names)
|
17
|
+
end
|
18
|
+
|
19
|
+
def ensured_attributes
|
20
|
+
@ensured_attributes ||= Set.new
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
data/spec/entity_spec.rb
CHANGED
@@ -6,17 +6,17 @@ end
|
|
6
6
|
|
7
7
|
describe 'Entity' do
|
8
8
|
it 'assigns attributes' do
|
9
|
-
entity =
|
9
|
+
entity = Foo.new(foo: 'foo')
|
10
10
|
entity.foo.should == 'foo'
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'ensures ensured attributes' do
|
14
|
-
entity =
|
14
|
+
entity = Foo.new()
|
15
15
|
entity.attributes.has_key?(:foo).should be_true
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'returns all attributes' do
|
19
|
-
entity =
|
19
|
+
entity = Foo.new(foo: 'moo')
|
20
20
|
entity.attributes.should == { foo: 'moo', bar: nil, buz: nil }
|
21
21
|
end
|
22
22
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: entity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -33,6 +33,7 @@ executables: []
|
|
33
33
|
extensions: []
|
34
34
|
extra_rdoc_files: []
|
35
35
|
files:
|
36
|
+
- lib/entity.rb
|
36
37
|
- spec/entity_spec.rb
|
37
38
|
- spec/spec_helper.rb
|
38
39
|
- Gemfile
|