light 1.0.0 → 1.0.1
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 +4 -4
- data/README.md +30 -3
- data/lib/light/model.rb +8 -1
- data/lib/light/version.rb +1 -1
- data/light.gemspec +1 -1
- data/spec/light/model_spec.rb +23 -5
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: acbf70b8bbb6a9b52486af69db0943258636befe
|
4
|
+
data.tar.gz: ed1173653116ef955eb281296a0872621a6b4bed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8570eeec9de0d96122ec59e03cbc28fd38cacba59e9fbc1fa77e3f6965c0ce4c6baefe130dcf1969df02ce162e19447013eb443b9853c989c6bfbb0c51b72c5b
|
7
|
+
data.tar.gz: 6d99baa956e03e77525521da3af626640cbf1761a6650233a9debe02b927b4c61e41a2d164909d758b2c900c182adf3e7807fdaac0765be15a769d0fc0373ab4
|
data/README.md
CHANGED
@@ -1,11 +1,38 @@
|
|
1
1
|
# Light
|
2
2
|
|
3
|
-
When you need all functionalities of ActiveRecord model but for other purposes than db transactions you can use Light. Light has additional `equality_state` and `to_h` method.
|
3
|
+
When you need all functionalities of ActiveRecord model but for other purposes than db transactions you can use Light. Light has additional `equality_state` and `to_h` and `persisted?` method.
|
4
4
|
|
5
5
|
## Examples:
|
6
6
|
|
7
|
-
```
|
8
|
-
|
7
|
+
```ruby
|
8
|
+
require 'light'
|
9
|
+
class Person < Light::Model
|
10
|
+
attributes :id, :name, :email
|
11
|
+
end
|
12
|
+
|
13
|
+
person1 = Person.new(name: 'Pawel', email: 'pawel@o2.pl')
|
14
|
+
person1.to_h # => {"id" => nil, "name"=>"Pawel", "email"=>"pawel@o2.pl"}
|
15
|
+
person1.as_json # => {"id" => nil, "name"=>"Pawel", "email"=>"pawel@o2.pl"}
|
16
|
+
person1.to_json # => "{\"id\":null,\"name\":\"Pawel\",\"email\":\"pawel@o2.pl\"}"
|
17
|
+
|
18
|
+
person2 = Person.new('name' => 'Pawel', email: 'pawel@o2.pl')
|
19
|
+
person3 = Person.new(name: 'Sylwia', email: 'sylwia@o2.pl')
|
20
|
+
|
21
|
+
person1 == person2 # => true
|
22
|
+
person1 == person3 # => false
|
23
|
+
person1.eql?(person2) # => true
|
24
|
+
person1.eql?(person3) # => false
|
25
|
+
person1.persisted? # => false
|
26
|
+
|
27
|
+
class User < Light::Model
|
28
|
+
attributes :id, :email
|
29
|
+
persisted_via_attr :id
|
30
|
+
end
|
31
|
+
|
32
|
+
user = User.new(email: 'somebody@gmail.com')
|
33
|
+
user.persisted? # => false
|
34
|
+
user.id = 1
|
35
|
+
user.persisted? # => true
|
9
36
|
```
|
10
37
|
|
11
38
|
## Installation
|
data/lib/light/model.rb
CHANGED
@@ -16,6 +16,10 @@ module Light
|
|
16
16
|
end
|
17
17
|
@__attributes = (@__attributes || []) + attrs
|
18
18
|
end
|
19
|
+
|
20
|
+
def persisted_via_attr(attr_name)
|
21
|
+
@__persisted_via_attr = attr_name
|
22
|
+
end
|
19
23
|
end
|
20
24
|
|
21
25
|
def equality_state
|
@@ -46,7 +50,10 @@ module Light
|
|
46
50
|
end
|
47
51
|
|
48
52
|
def persisted?
|
49
|
-
|
53
|
+
attr_name = self.class.instance_variable_get(:@__persisted_via_attr)
|
54
|
+
return false if attr_name.nil?
|
55
|
+
value = send(attr_name)
|
56
|
+
!(value.respond_to?(:empty?) ? !!value.empty? : !value)
|
50
57
|
end
|
51
58
|
|
52
59
|
private
|
data/lib/light/version.rb
CHANGED
data/light.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_dependency 'activemodel', '
|
21
|
+
spec.add_dependency 'activemodel', '>= 4.0'
|
22
22
|
spec.add_development_dependency 'bundler', '~> 1.3'
|
23
23
|
spec.add_development_dependency 'rake', '~> 0'
|
24
24
|
spec.add_development_dependency 'rspec', '~> 3.1'
|
data/spec/light/model_spec.rb
CHANGED
@@ -8,7 +8,7 @@ describe Light::Model do
|
|
8
8
|
let(:test_data) { {name: 'Pawel', email: 'pniemczyk@o2.pl', bucket: { test_key: 'stuff'}} }
|
9
9
|
before(:each) { @subject = TestClass.new(test_data) }
|
10
10
|
|
11
|
-
|
11
|
+
context 'initialize' do
|
12
12
|
it 'fillfull instance' do
|
13
13
|
@subject.name.should eq test_data[:name]
|
14
14
|
@subject.email.should eq test_data[:email]
|
@@ -16,7 +16,7 @@ describe Light::Model do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
|
19
|
+
context '#to_h' do
|
20
20
|
it 'default' do
|
21
21
|
@subject.to_h.symbolize_keys.should eq test_data
|
22
22
|
end
|
@@ -27,7 +27,7 @@ describe Light::Model do
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
|
30
|
+
context 'equality' do
|
31
31
|
it 'two instance with same data should be eq' do
|
32
32
|
[
|
33
33
|
TestClass.new(test_data) == TestClass.new(test_data),
|
@@ -36,7 +36,7 @@ describe Light::Model do
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
|
39
|
+
context '#as_json' do
|
40
40
|
it 'default' do
|
41
41
|
@subject.as_json.should eq test_data.stringify_keys
|
42
42
|
end
|
@@ -47,7 +47,7 @@ describe Light::Model do
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
|
50
|
+
context '#to_json' do
|
51
51
|
it 'default' do
|
52
52
|
@subject.to_json.should eq test_data.to_json
|
53
53
|
end
|
@@ -57,4 +57,22 @@ describe Light::Model do
|
|
57
57
|
@subject.to_json(only: :name).should eq hash.to_json
|
58
58
|
end
|
59
59
|
end
|
60
|
+
|
61
|
+
context '#persisted?' do
|
62
|
+
it 'by default returns false' do
|
63
|
+
@subject.persisted?.should eq false
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'when persisted_via_attr is setup' do
|
67
|
+
before { @subject.class.persisted_via_attr :name }
|
68
|
+
it 'returns true when attr value present' do
|
69
|
+
@subject.persisted?.should eq true
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'returns false when attr value is blank' do
|
73
|
+
@subject.name = nil
|
74
|
+
@subject.persisted?.should eq false
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
60
78
|
end
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: light
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pawel Niemczyk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
@@ -120,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
120
|
version: '0'
|
121
121
|
requirements: []
|
122
122
|
rubyforge_project:
|
123
|
-
rubygems_version: 2.6.
|
123
|
+
rubygems_version: 2.6.14
|
124
124
|
signing_key:
|
125
125
|
specification_version: 4
|
126
126
|
summary: Light models
|