entity_rb 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 49b12775d7fcb0ae8b817244f35ab6933ad7de58
4
- data.tar.gz: fa53e6f2c2aa14712ae617aa820a6957e6e3e5df
3
+ metadata.gz: 01cab009a4a6409a362314526a1148049288eb9d
4
+ data.tar.gz: b663c91b67df1893266ebf5aeea81b8bc476af6d
5
5
  SHA512:
6
- metadata.gz: 7feb84d0e330717f23c5a983df7b8591e1e9c1d7319d710fb6a3b72294055652759d091a8d686a4c46edf7fe94c97cb949fd079b8c7b0dfba98ae4d3b528e435
7
- data.tar.gz: ed29751458490a55d778e8066eace696a9019703fd3f5503325adde92d9ddc9d2e0980c21c1a3c17ce31057f6d4d4a701b40ab9c724064b0b01f4ff5bc1cfdcf
6
+ metadata.gz: 41a7904c86d36c7263fea5931792a1d770ba81e7539530f1127736c2d37388d80c7fe6dca69d5577c63f4a5956b4e40a0677984b678be3e15ec629192729fb0c
7
+ data.tar.gz: c3bfe5a86f736d8cb5a39ea6655dcb4331387747975aa959ff6ec806b766f1f417ce5c5bac1bae346f3163310cab1703c6d02d1aad34a4b0480b6882e6b704b6
data/README.md CHANGED
@@ -7,7 +7,7 @@ TODO: Write a gem description
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'entity'
10
+ gem 'entity_rb'
11
11
  ```
12
12
 
13
13
  And then execute:
@@ -16,7 +16,7 @@ And then execute:
16
16
 
17
17
  Or install it yourself as:
18
18
 
19
- $ gem install entity
19
+ $ gem install entity_rb
20
20
 
21
21
  ## Usage
22
22
 
@@ -24,7 +24,7 @@ TODO: Write usage instructions here
24
24
 
25
25
  ## Contributing
26
26
 
27
- 1. Fork it ( https://github.com/[my-github-username]/entity/fork )
27
+ 1. Fork it ( https://github.com/[my-github-username]/entity_rb/fork )
28
28
  2. Create your feature branch (`git checkout -b my-new-feature`)
29
29
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
30
  4. Push to the branch (`git push origin my-new-feature`)
@@ -1,17 +1,35 @@
1
1
  module Entity
2
2
  class Base
3
- @@fields = []
4
-
5
3
  def initialize(attributes={})
6
4
  attributes.each do |key, value|
7
- send("#{key}=", value) if @@fields.include? key.to_sym
5
+ send("#{key}=", value) if fields.include? key.to_sym
6
+ end
7
+ end
8
+
9
+ def attributes
10
+ hash = {}
11
+
12
+ fields.each do |key|
13
+ hash[key.to_sym] = self.send(key)
8
14
  end
15
+
16
+ hash
17
+ end
18
+
19
+ def self.fields
20
+ @fields
21
+ end
22
+
23
+ def fields
24
+ self.class.fields
9
25
  end
10
26
 
11
27
  protected
12
28
  def self.field(key)
13
29
  raise ArgumentError.new('Key must be a String, Symbol or Array') unless [String, Symbol, Array].include? key.class
14
30
 
31
+ @fields ||= []
32
+
15
33
  if key.is_a? Array
16
34
  key.each do |k|
17
35
  begin
@@ -20,7 +38,7 @@ module Entity
20
38
  end
21
39
  end
22
40
  else
23
- @@fields.push key.to_sym
41
+ fields.push key.to_sym unless fields.include?(key.to_sym)
24
42
 
25
43
  attr_accessor key.to_sym
26
44
  end
@@ -1,3 +1,3 @@
1
1
  module Entity
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,4 +1,4 @@
1
- require './lib/entity/base'
1
+ require './lib/entity_rb/base'
2
2
 
3
3
  describe Entity::Base do
4
4
  describe '.field' do
@@ -93,4 +93,116 @@ describe Entity::Base do
93
93
  end
94
94
  end
95
95
  end
96
+
97
+ describe '.new' do
98
+ before do
99
+ class Test < Entity::Base
100
+ field :field1
101
+ field :field2
102
+ end
103
+ end
104
+
105
+ context 'when all fields exists' do
106
+ subject { Test.new(field1: 'field1', field2: 'field2') }
107
+
108
+ it 'sets field1 value' do
109
+ expect(subject.field1).to eq 'field1'
110
+ end
111
+
112
+ it 'sets field2 value' do
113
+ expect(subject.field2).to eq 'field2'
114
+ end
115
+ end
116
+
117
+ context 'when some fields does not exist' do
118
+ subject { Test.new(field1: 'field1', field2: 'field2', field3: 'field3', field4: 'field4') }
119
+
120
+ it 'sets field1 value' do
121
+ expect(subject.field1).to eq 'field1'
122
+ end
123
+
124
+ it 'sets field2 value' do
125
+ expect(subject.field2).to eq 'field2'
126
+ end
127
+
128
+ it 'does not sets field3 value' do
129
+ expect{subject.field3}.to raise_error(NoMethodError)
130
+ end
131
+
132
+ it 'does not sets field4 value' do
133
+ expect{subject.field4}.to raise_error(NoMethodError)
134
+ end
135
+ end
136
+
137
+ context 'when some existing fields are not initialized' do
138
+ subject { Test.new(field1: 'field1') }
139
+
140
+ it 'sets field1 value' do
141
+ expect(subject.field1).to eq 'field1'
142
+ end
143
+
144
+ it 'sets field2 value' do
145
+ expect(subject.field2).to be_nil
146
+ end
147
+ end
148
+ end
149
+
150
+ describe '#attributes' do
151
+ let(:hash) { { field1: 'field1', field2: 'field2', field3: 'field3', field4: 'field4' } }
152
+
153
+ before do
154
+ class TestClass < Entity::Base
155
+ field :field1
156
+ field :field2
157
+ field :field3
158
+ field :field4
159
+ end
160
+ end
161
+
162
+ subject { TestClass.new(hash) }
163
+
164
+ it "returns a hash with all fields and it's values" do
165
+ expect(subject.attributes).to eq hash
166
+ end
167
+ end
168
+
169
+ describe '.fields' do
170
+ before do
171
+ class TestClass < Entity::Base
172
+ field :field1
173
+ field :field2
174
+ field :field3
175
+ field :field4
176
+ end
177
+ end
178
+
179
+ subject { TestClass.fields }
180
+
181
+ it "returns a hash with all fields and it's values" do
182
+ expect(subject).to eq [:field1, :field2, :field3, :field4]
183
+ end
184
+ end
185
+
186
+ describe '#fields' do
187
+ before do
188
+ class TestClass < Entity::Base
189
+ field :field1
190
+ field :field1
191
+ field :field2
192
+ field :field2
193
+ field :field3
194
+ field :field3
195
+ field :field4
196
+ field :field4
197
+ end
198
+ end
199
+
200
+ subject { TestClass.new.fields }
201
+
202
+ it "returns a hash with all fields and it's values" do
203
+ expect(TestClass).to receive(:fields)
204
+
205
+ subject
206
+ end
207
+ end
96
208
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: entity_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caio Torres e Paulo Patto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-09 00:00:00.000000000 Z
11
+ date: 2015-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.2.2
95
+ rubygems_version: 2.4.6
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: An Entity implementation to store all business logic from the (so called)