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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f4637c6e300ad3511f0e8b731d39e0ffc685a75
4
- data.tar.gz: cb3616c6692f5fe7ae94c2b93039f27c74d0944f
3
+ metadata.gz: c729898a7cb71769568a70372a436d6de8d5858b
4
+ data.tar.gz: a73dc6b73df7ec2acef1045694cecbfc3bbb8002
5
5
  SHA512:
6
- metadata.gz: d2131d2bcef25c5c43ccb98a5b5a2cb63ad1b305c34c9e410a90244f8d73ee52e992249eba1182ac740c61c8f2be38042d2379f36f97108d82a3e3ec1d5734a2
7
- data.tar.gz: 6df0e38c7e25c9cf49ed31b0c4151b860b399eb587b786fa0509dc0d23882a09313bc62cbbd84ad03c42ac2a6ac6dac9d3d6e307ca3daa1caf0c4e455bd40951
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 })
@@ -1,3 +1,3 @@
1
1
  module ModelPack
2
- VERSION = "0.9.3"
2
+ VERSION = "0.9.4"
3
3
  end
@@ -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 serialize model with custom serializer" do
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.3
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-10-06 00:00:00.000000000 Z
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler