estructural 0.0.3 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/estructural.gemspec +1 -1
- data/lib/estructural.rb +4 -3
- data/test/estructural_test.rb +26 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 494740e7529ee2689a80b6890324e05404a0c1f7aa051a9ab2f399f7e608fd04
|
4
|
+
data.tar.gz: 94bc87d2d34a672a3a658dba497210947edcb0b008b2d10d85531e3abde68f4b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb39b45e9fdd41378cdf31e0a7d0a379c489835bf933b9a3ed41003640efbcced4c27d1db77b4a007d2a5fed078453aadd282f5f9cb24ed3d06e9997792100c2
|
7
|
+
data.tar.gz: 454d34feff40dd91fa5073366adf39c925b56fc73a216e82469b3f44de563cb798d0ce2f4cb3cf8d1e9c931145f11798185ce9a241d77e45b21e5acd497c1a60
|
data/estructural.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
s.name = "estructural"
|
4
|
-
s.version = "0.0.
|
4
|
+
s.version = "0.0.5"
|
5
5
|
s.authors = ["Fernando Martinez"]
|
6
6
|
s.email = ["fernando@templ.co"]
|
7
7
|
s.summary = %q(A quick way to define estructured, data-container objects drawing inspiration from `Struct` and `OpenStruct`)
|
data/lib/estructural.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
class Estructural
|
2
2
|
VERSION = '0.0.1'
|
3
3
|
|
4
|
-
def self.new(*attributes, &block)
|
4
|
+
def self.new(*attributes, **defaults, &block)
|
5
5
|
Class.new do
|
6
|
-
attributes.each do |attr|
|
6
|
+
(attributes + defaults.keys).each do |attr|
|
7
7
|
define_method(attr) { instance_variable_get("@#{attr}") }
|
8
8
|
define_method("#{attr}=") {|value| instance_variable_set("@#{attr}", value) }
|
9
9
|
end
|
10
10
|
|
11
|
-
|
11
|
+
define_method(:initialize) do |args = {}|
|
12
|
+
defaults.each { |attribute, value| send("#{attribute}=", value.dup) }
|
12
13
|
args.each { |attribute, value| send "#{attribute}=", value }
|
13
14
|
end
|
14
15
|
|
data/test/estructural_test.rb
CHANGED
@@ -3,7 +3,7 @@ require "minitest/autorun"
|
|
3
3
|
require_relative "../lib/estructural"
|
4
4
|
|
5
5
|
describe Estructural do
|
6
|
-
Subject = Estructural.new(:name, :id) do
|
6
|
+
Subject = Estructural.new(:name, :id, things: []) do
|
7
7
|
puts "SERIALZE DEF"
|
8
8
|
def serialize
|
9
9
|
"id:#{id} name:#{name}"
|
@@ -42,4 +42,29 @@ describe Estructural do
|
|
42
42
|
instance.must_respond_to :serialize
|
43
43
|
instance.serialize.must_equal "id:1 name:a subject has no name"
|
44
44
|
end
|
45
|
+
|
46
|
+
it "assgins default values" do
|
47
|
+
instance = Subject.new(id: 1, name: "1")
|
48
|
+
instance2 = Subject.new(id: 1, name: "1")
|
49
|
+
|
50
|
+
instance.things.must_equal []
|
51
|
+
instance2.things.must_equal []
|
52
|
+
|
53
|
+
instance.things << 1
|
54
|
+
instance.things << 3
|
55
|
+
instance.things << 5
|
56
|
+
|
57
|
+
|
58
|
+
instance2.things << 2
|
59
|
+
instance2.things << 4
|
60
|
+
instance2.things << 6
|
61
|
+
|
62
|
+
instance.things.must_equal [1,3,5]
|
63
|
+
instance2.things.must_equal [2,4,6]
|
64
|
+
end
|
65
|
+
|
66
|
+
it "assgins default values are overriden in initialize" do
|
67
|
+
instance = Subject.new(id: 1, name: "1", things: "a smoke")
|
68
|
+
instance.things.must_equal "a smoke"
|
69
|
+
end
|
45
70
|
end
|