model_pack 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +77 -4
- data/lib/model_pack/version.rb +1 -1
- data/model_pack.gemspec +3 -0
- data/spec/model_pack_spec.rb +2 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a566b021fe29847c5ec63d781de237c037f693b9
|
4
|
+
data.tar.gz: fd18beaa67efba4dc52f43488c494ecf489f4e2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 512ef1c65902e3e0eeb14f53e4c69152853c9b7c0353a9a58c3a5294edd36fd786e50a8b9b8ad1d657975f5d661356d3954cb124b0cf4d10a70b1586397d52d4
|
7
|
+
data.tar.gz: d9e615aef19f355b001e65a6a5a85527b0330d6805597e5a5ef43bdc5213a22bf762297844737d3fe7b32a1b7a278fe96433c17850a1babc45171f1a3d6b88ff
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Model Pack - model
|
1
|
+
# Model Pack - model interfaces
|
2
2
|
|
3
3
|
Model Pack - набор интерфейсов, который позволяет создавать в объекте специальные аттрибуты.
|
4
4
|
Также добавляет методы работы model attributes. Прозволяет сериализировать иерахнию модельных
|
@@ -20,7 +20,9 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
$ gem install model_pack
|
22
22
|
|
23
|
-
##
|
23
|
+
## Использование
|
24
|
+
|
25
|
+
### Атрибуты и вложеные объекты
|
24
26
|
|
25
27
|
```ruby
|
26
28
|
class Point
|
@@ -33,9 +35,80 @@ end
|
|
33
35
|
class Line
|
34
36
|
include ModelPack::Document
|
35
37
|
|
36
|
-
object :
|
37
|
-
object :
|
38
|
+
object :from, class_name: Point
|
39
|
+
object :to, class_name: Point
|
40
|
+
|
41
|
+
def length
|
42
|
+
Math.sqrt((from.x-to.x)**2 + (from.y-to.y)**2)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
Создаем эксземпляры модели:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
point = Point.new(x: 3, y: 5)
|
51
|
+
line = Line.new(
|
52
|
+
from: { x: 1, y: 1 },
|
53
|
+
to: { x: 3, y: 5}
|
54
|
+
)
|
55
|
+
puts line.length
|
56
|
+
```
|
57
|
+
|
58
|
+
### Массив моделей
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
class Polygon
|
62
|
+
include ModelPack::Document
|
63
|
+
|
64
|
+
array :points, class_name: Point
|
65
|
+
|
66
|
+
def sides
|
67
|
+
return [] if points.size < 2
|
68
|
+
[[points.first]].tap do |sides|
|
69
|
+
(1...(points.size)).to_a.each do |index|
|
70
|
+
sides.last.push points[index]
|
71
|
+
sides.push [points[index]]
|
72
|
+
end
|
73
|
+
sides.last.push(points.first)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def perimeter
|
78
|
+
sides.inject(0) { |sum, side| sum + Math.sqrt((side[0].x-side[1].x)**2 + (side[0].y-side[1].y)**2) }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
polygon = Polygon.new(points: [{x: 0, y: 0}, {x:5, y:0}, {x:5, y:5}])
|
82
|
+
puts polygon.perimeter
|
83
|
+
```
|
84
|
+
|
85
|
+
### Сериализация моделей
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
polygon = Polygon.new(points: [{x: 0, y: 0}, {x:5, y:0}, {x:5, y:5}])
|
89
|
+
json = polygon.as_json() # same serializable_hash
|
90
|
+
```
|
91
|
+
|
92
|
+
### Копирование моделей
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
polygon = Polygon.new(points: [{x: 3, y: 3}, {x:2, y:1}, {x:4, y:2}])
|
96
|
+
polygon_copy = polygon.copy # same polygon_copy = Polygon.new(polygon.serializable_hash)
|
97
|
+
puts polygon_copy
|
98
|
+
```
|
99
|
+
|
100
|
+
### Выборочная сериализация
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
class SecureData
|
104
|
+
include ModelPack::Document
|
105
|
+
attribute :hidden_field, writer: lambda { |v| nil }
|
106
|
+
attribute :const_field, writer: lambda { |v| :always_this }
|
107
|
+
attribute :always_string, writer: lambda { |v| v.to_s }
|
38
108
|
end
|
109
|
+
secure_data = SecureData.new( hidden_field: "secured text", const_field: :some_value, always_string: 55)
|
110
|
+
unsecure_hash = secure_data.serializable_hash
|
111
|
+
puts unsecure_hash
|
39
112
|
```
|
40
113
|
|
41
114
|
## Contributing
|
data/lib/model_pack/version.rb
CHANGED
data/model_pack.gemspec
CHANGED
@@ -13,6 +13,9 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.homepage = ""
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
|
+
spec.required_ruby_version = '>= 2.0.0'
|
17
|
+
spec.required_rubygems_version = '>= 2.3.0'
|
18
|
+
|
16
19
|
spec.files = `git ls-files -z`.split("\x0")
|
17
20
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
21
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
data/spec/model_pack_spec.rb
CHANGED
@@ -35,10 +35,10 @@ describe ModelPack::ClassMethods do
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
line = Line.new(
|
38
|
+
line = Line.new(
|
39
39
|
from: { x: 1, y: 1 },
|
40
40
|
to: { x: 3, y: 5}
|
41
|
-
|
41
|
+
)
|
42
42
|
expect(line.from).to be_a(Point)
|
43
43
|
expect(line.to).to be_a(Point)
|
44
44
|
expect(line.from.attributes).to include({x: 1, y: 1})
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- che
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -88,12 +88,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
88
|
requirements:
|
89
89
|
- - '>='
|
90
90
|
- !ruby/object:Gem::Version
|
91
|
-
version:
|
91
|
+
version: 2.0.0
|
92
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
96
|
+
version: 2.3.0
|
97
97
|
requirements: []
|
98
98
|
rubyforge_project:
|
99
99
|
rubygems_version: 2.4.1
|