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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f5e2f42ea7f651e47c60029c6d2516a1d0d09eb052b11b81021e8023ec55003
4
- data.tar.gz: bd6f3db39042f1c473140f100a19bc509a5daacddb53fb33a8b4c1e6db38a2cf
3
+ metadata.gz: 494740e7529ee2689a80b6890324e05404a0c1f7aa051a9ab2f399f7e608fd04
4
+ data.tar.gz: 94bc87d2d34a672a3a658dba497210947edcb0b008b2d10d85531e3abde68f4b
5
5
  SHA512:
6
- metadata.gz: f1704750e818cd894d784e461e93cbcadbd2ef943a7b8bda64811b6950a882f9df57da4c030ae4305ad5f6b0f9ded4dd2979088465f047da7f7a96100062b9e5
7
- data.tar.gz: 696b3f7d8734d60012abeaae58770da132c66647523e9d209f25d5f3980eb7f98577378ee568a6748ee62e915948a0615e290a9bfad588e8ece09b8733a4402a
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.3"
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
- def initialize(args = {})
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
 
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: estructural
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fernando Martinez