model_pack 0.9.3 → 0.9.4
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 +11 -0
- data/lib/model_pack/attribute_methods.rb +1 -0
- data/lib/model_pack/class_methods.rb +1 -1
- data/lib/model_pack/version.rb +1 -1
- data/spec/model_pack_spec.rb +27 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c729898a7cb71769568a70372a436d6de8d5858b
|
4
|
+
data.tar.gz: a73dc6b73df7ec2acef1045694cecbfc3bbb8002
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87a329ad9d6d5c43cc7acce1c8c8e4958052d3a7920f79195f5162b3a8d3f9ca4a222e4c4ca91af2c32949e6ef8d76b32f84d9b701adc77191443a2424e0c419
|
7
|
+
data.tar.gz: 011f7fe6c8a010ee95ec43840744bdf73f24f8902a0b89f788304fe1afe83d941afed1e4fed24848dc89c632f5a393835b5aea3190361de20fb6069c416aeb38
|
data/README.md
CHANGED
@@ -55,6 +55,17 @@ line = Line.new(
|
|
55
55
|
puts line.length
|
56
56
|
```
|
57
57
|
|
58
|
+
### Перекрываем запись аттрибутов
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
class Text
|
62
|
+
attribute :always_string, writer: ->(v) { v.to_s }
|
63
|
+
end
|
64
|
+
|
65
|
+
text = Text.new(always_string: 123)
|
66
|
+
puts text.always_string # "123"
|
67
|
+
```
|
68
|
+
|
58
69
|
### Массив моделей
|
59
70
|
|
60
71
|
```ruby
|
@@ -4,6 +4,7 @@ module ModelPack
|
|
4
4
|
|
5
5
|
class_methods do
|
6
6
|
def register_attribute(name)
|
7
|
+
raise ArgumentError, "class already have method with name `#{name}`" if respond_to?(name)
|
7
8
|
raise ArgumentError, "dublicate attribute with name `#{name}`" if attribute_names.include?(name)
|
8
9
|
attribute_names.push name
|
9
10
|
nil
|
@@ -2,9 +2,9 @@ module ModelPack
|
|
2
2
|
module ClassMethods
|
3
3
|
|
4
4
|
def attribute(name, writer: lambda { |v| v }, default: nil, as: nil, serialize: nil, predicate: nil)
|
5
|
+
register_attribute(name)
|
5
6
|
attribute_reader(name, default: default, as: as, serialize: serialize, predicate: predicate)
|
6
7
|
attribute_writer(name, writer: writer)
|
7
|
-
register_attribute(name)
|
8
8
|
end
|
9
9
|
|
10
10
|
def attribute_writer(name, writer: lambda { |v| v })
|
data/lib/model_pack/version.rb
CHANGED
data/spec/model_pack_spec.rb
CHANGED
@@ -89,7 +89,7 @@ describe ModelPack::ClassMethods do
|
|
89
89
|
expect(json[:points][2]).to include({x: 5, y:5})
|
90
90
|
end
|
91
91
|
|
92
|
-
it "should
|
92
|
+
it "should each attribute use writer" do
|
93
93
|
class SecureData
|
94
94
|
include ModelPack::Document
|
95
95
|
|
@@ -105,6 +105,32 @@ describe ModelPack::ClassMethods do
|
|
105
105
|
expect(unsecure_hash).to include(always_string: "55")
|
106
106
|
end
|
107
107
|
|
108
|
+
it "should serialize with custom serializers" do
|
109
|
+
class SecureDataSerialized
|
110
|
+
include ModelPack::Document
|
111
|
+
|
112
|
+
attribute :hidden_field, serialize: lambda { |v| nil }
|
113
|
+
attribute :const_field, serialize: lambda { |v| :always_this }
|
114
|
+
attribute :always_string, serialize: lambda { |v| v.to_s }
|
115
|
+
end
|
116
|
+
|
117
|
+
secure_data = SecureDataSerialized.new( hidden_field: "secured text", const_field: :some_value, always_string: 55)
|
118
|
+
unsecure_hash = secure_data.serializable_hash
|
119
|
+
expect(unsecure_hash).not_to include(:hidden_field)
|
120
|
+
expect(unsecure_hash).to include(const_field: :always_this)
|
121
|
+
expect(unsecure_hash).to include(always_string: "55")
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should argument error method with name *method* already exists" do
|
125
|
+
expect {
|
126
|
+
class Request
|
127
|
+
include ModelPack::Document
|
128
|
+
|
129
|
+
attribute :method, default: "get"
|
130
|
+
end
|
131
|
+
}.to raise_error
|
132
|
+
end
|
133
|
+
|
108
134
|
it "should serialize and load back model" do
|
109
135
|
polygon = Polygon.new(points: [{x: 3, y: 3}, {x:2, y:1}, {x:4, y:2}])
|
110
136
|
polygon_copy = Polygon.new(polygon.serializable_hash) # or as_json
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_pack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- che
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|