o_patch 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 +5 -13
- data/.gitignore +1 -0
- data/.travis.yml +3 -0
- data/Gemfile.lock +1 -1
- data/README.md +88 -3
- data/lib/o_patch/patcher.rb +2 -2
- data/o_patch.gemspec +1 -1
- metadata +17 -16
checksums.yaml
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
|
|
5
|
-
data.tar.gz: !binary |-
|
|
6
|
-
MTBmOTIzNmQ0NjhlMzcyY2I4ZGI2NWM1MjE4YThkYTM4ZTc1NTk2MA==
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 2acac7437a2fda4c9d014ce2aea3f2c6ee7fd927
|
|
4
|
+
data.tar.gz: 33f410f04aae3cc1ad121c814309d13b380fde43
|
|
7
5
|
SHA512:
|
|
8
|
-
metadata.gz:
|
|
9
|
-
|
|
10
|
-
NTRhMDY0NjliYTI1MzgxZDk4NjNhMzQ1MzI3NzFiYjRiMzU5ZDk4MTc5MDU0
|
|
11
|
-
ODFkMjI5MmVjMWMxZGZlODMxODVjOWU0ZTE4OWQyN2VjNDE0NmE=
|
|
12
|
-
data.tar.gz: !binary |-
|
|
13
|
-
NGRmYTFiZDEyYWVkN2E0ZDlkYTEwMTdkZmNlYTkxYWM2ZDZkZWExZTVmODVk
|
|
14
|
-
YjllZTI3YzY5NTYxMzU5Nzk4ZTBjNzQ3MmNiMjcyYzAxZDkyZTBlY2JhMWQ5
|
|
15
|
-
NTIxYmU2NjBkNzQ1MWMzZjQ5ZmZlNWY4ZTFkMDg1NjYxMzI2MTk=
|
|
6
|
+
metadata.gz: 89e83a940dcad0c3b670ba7b7d80033ab3a0495d8d871c282d6669bf46c4719db26031961e6d8548265ad2c557085998d1014147981040253ba776268fba38d2
|
|
7
|
+
data.tar.gz: 1856749f67be4da20d40aa9e3f014964d46c20faed70273e053bf59fd36dfda7c2eee54f4f52a2a4a8b6073471e383183109727d5157fb038e06facca597c030
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -1,4 +1,89 @@
|
|
|
1
|
-
opatch
|
|
2
|
-
|
|
1
|
+
# OPatch [](https://travis-ci.org/AlbertGazizov/opatch) [](https://codeclimate.com/github/AlbertGazizov/opatch)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
OPatch is ruby objects patcher in declarative way. By providing simple DSL it allows you write complex patch logic with ease
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
Let's say you have the folowing plain ruby classes:
|
|
9
|
+
```ruby
|
|
10
|
+
class Person
|
|
11
|
+
attr_accessor :name, :address
|
|
12
|
+
|
|
13
|
+
def initialize(name:, address: nil)
|
|
14
|
+
@name = name
|
|
15
|
+
@address = address
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class Address
|
|
20
|
+
attr_accessor :city, :country
|
|
21
|
+
|
|
22
|
+
def initialize(city:, country:)
|
|
23
|
+
@city = city
|
|
24
|
+
@country = country
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
```
|
|
28
|
+
And you have the following instance of the Person class:
|
|
29
|
+
```ruby
|
|
30
|
+
person = Person.new(name: "John Smith", address: Address.new(city: "Kazan", country: "Russia"))
|
|
31
|
+
```
|
|
32
|
+
Now you want to update person's name and address fields, let's use OPatch for that.
|
|
33
|
+
You need to call OPatch with attributes you want to update:
|
|
34
|
+
```ruby
|
|
35
|
+
new_attributes = { name: "Jim White", address: { country: "USA", city: "New York" } }
|
|
36
|
+
OPatch.patch(person, new_attributes) do
|
|
37
|
+
field :name
|
|
38
|
+
object :address do
|
|
39
|
+
field :country
|
|
40
|
+
field :city
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
```
|
|
44
|
+
That's all! Now you have the person updated:
|
|
45
|
+
```ruby
|
|
46
|
+
=> #<Person:0x007f8bc42487c0 @name="Jim White", @address=#<Address:0x007f8bc4248838 @city="New York", @country="USA">>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Creating nested objects
|
|
50
|
+
Opatch allows you to build nested objects if you specify build block.
|
|
51
|
+
Lets see how it works if we want to build address when it wasn't created initialilly:
|
|
52
|
+
```ruby
|
|
53
|
+
person = Person.new(name: "John Smith")
|
|
54
|
+
=> #<Person:0x007f8bc40239e0 @name="John Smith", @address=nil>
|
|
55
|
+
```
|
|
56
|
+
```ruby
|
|
57
|
+
new_attributes = { address: { country: "USA", city: "New York" } }
|
|
58
|
+
OPatch.patch(person, new_attributes) do
|
|
59
|
+
field :name
|
|
60
|
+
object :address, build: proc { |person, attributes| person.address = Address.new(attributes) } do
|
|
61
|
+
field :country
|
|
62
|
+
field :city
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
```
|
|
66
|
+
```ruby
|
|
67
|
+
=> #<Person:0x007f8bc4130590 @name="John Smith", @address=#<Address:0x007f8bc4049a28 @city="New York", @country="USA">>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
### Deleting nested objects
|
|
72
|
+
If you provide nil for the nested object the object will be removed:
|
|
73
|
+
```ruby
|
|
74
|
+
person = Person.new(name: "John Smith", address: Address.new(city: "Kazan", country: "Russia"))
|
|
75
|
+
|
|
76
|
+
OPatch.patch(person, name: 'Vasya', address: nil) do
|
|
77
|
+
field :name
|
|
78
|
+
object :address do
|
|
79
|
+
field :country
|
|
80
|
+
field :city
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
=> #<Person:0x007f8bc400a850 @name="Vasya", @address=nil>
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Author
|
|
88
|
+
Albert Gazizov, [@deeper4k](https://twitter.com/deeper4k)
|
|
3
89
|
|
|
4
|
-
Declarative ruby objects patcher
|
data/lib/o_patch/patcher.rb
CHANGED
|
@@ -56,12 +56,12 @@ class OPatch::Patcher
|
|
|
56
56
|
attributes[collection_name].each do |child_attributes|
|
|
57
57
|
key_value = attributes_key_value(child_attributes, key)
|
|
58
58
|
if key_value
|
|
59
|
-
child_object = collection_hash[key_value].first
|
|
59
|
+
child_object = (collection_hash[key_value] || []).first
|
|
60
60
|
raise "#{collection_name} don't have an object with key: #{key_value}" unless child_object
|
|
61
61
|
if child_attributes[:_destroy]
|
|
62
62
|
collection.delete(child_object)
|
|
63
63
|
else
|
|
64
|
-
self.class.patch(child_object, child_attributes, &block)
|
|
64
|
+
self.class.patch(child_object, child_attributes, &block) if block_given?
|
|
65
65
|
end
|
|
66
66
|
else
|
|
67
67
|
build_block.call(entity, child_attributes)
|
data/o_patch.gemspec
CHANGED
|
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
|
3
3
|
|
|
4
4
|
Gem::Specification.new do |spec|
|
|
5
5
|
spec.name = "o_patch"
|
|
6
|
-
spec.version = "0.0.
|
|
6
|
+
spec.version = "0.0.2"
|
|
7
7
|
spec.authors = ["Albert Gazizov"]
|
|
8
8
|
spec.description = %q{Declarative ruby objects patcher}
|
|
9
9
|
spec.summary = %q{Declarative ruby objects patcher}
|
metadata
CHANGED
|
@@ -1,50 +1,51 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: o_patch
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Albert Gazizov
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2015-02-19 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
|
|
14
|
+
name: bundler
|
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
|
16
16
|
requirements:
|
|
17
|
-
- - ~>
|
|
17
|
+
- - "~>"
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
19
|
version: '1.3'
|
|
20
|
-
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
21
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
23
|
requirements:
|
|
23
|
-
- - ~>
|
|
24
|
+
- - "~>"
|
|
24
25
|
- !ruby/object:Gem::Version
|
|
25
26
|
version: '1.3'
|
|
26
|
-
prerelease: false
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
|
-
|
|
28
|
+
name: rake
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
|
-
- -
|
|
31
|
+
- - ">="
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
33
|
version: '0'
|
|
34
|
-
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
35
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
37
|
requirements:
|
|
37
|
-
- -
|
|
38
|
+
- - ">="
|
|
38
39
|
- !ruby/object:Gem::Version
|
|
39
40
|
version: '0'
|
|
40
|
-
prerelease: false
|
|
41
41
|
description: Declarative ruby objects patcher
|
|
42
42
|
email:
|
|
43
43
|
executables: []
|
|
44
44
|
extensions: []
|
|
45
45
|
extra_rdoc_files: []
|
|
46
46
|
files:
|
|
47
|
-
- .gitignore
|
|
47
|
+
- ".gitignore"
|
|
48
|
+
- ".travis.yml"
|
|
48
49
|
- Gemfile
|
|
49
50
|
- Gemfile.lock
|
|
50
51
|
- LICENSE
|
|
@@ -64,17 +65,17 @@ require_paths:
|
|
|
64
65
|
- lib
|
|
65
66
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
67
|
requirements:
|
|
67
|
-
- -
|
|
68
|
+
- - ">="
|
|
68
69
|
- !ruby/object:Gem::Version
|
|
69
70
|
version: '0'
|
|
70
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
72
|
requirements:
|
|
72
|
-
- -
|
|
73
|
+
- - ">="
|
|
73
74
|
- !ruby/object:Gem::Version
|
|
74
75
|
version: '0'
|
|
75
76
|
requirements: []
|
|
76
77
|
rubyforge_project:
|
|
77
|
-
rubygems_version: 2.
|
|
78
|
+
rubygems_version: 2.2.2
|
|
78
79
|
signing_key:
|
|
79
80
|
specification_version: 4
|
|
80
81
|
summary: Declarative ruby objects patcher
|