virtus-delegate 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5054cb57a16fdbb2b7841c3e8875b8fde05137e7
4
+ data.tar.gz: 4c0cf11cbd5d577d52e6c972b9f42bbd24b19a10
5
+ SHA512:
6
+ metadata.gz: c9ef106780e4a7972a19424f3f45135803e8bc9c38523030dccb638634a18a3859db4adabb1e3b2e3c735f1fa816ea3ce6d09a49f42d01e3614649397a57429b
7
+ data.tar.gz: 4216a6d82f0637dc1130b7ec3fd190e4b9ab8feb9c46b96aa6f0fdd1b59f7ecee09124870f52b43772d109ba77b6b9ea7885eb7a23e732cc181455ff144de13f
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,27 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ axiom-types (0.1.1)
5
+ descendants_tracker (~> 0.0.4)
6
+ ice_nine (~> 0.11.0)
7
+ thread_safe (~> 0.3, >= 0.3.1)
8
+ coercible (1.0.0)
9
+ descendants_tracker (~> 0.0.1)
10
+ descendants_tracker (0.0.4)
11
+ thread_safe (~> 0.3, >= 0.3.1)
12
+ equalizer (0.0.9)
13
+ ice_nine (0.11.0)
14
+ rake (0.9.6)
15
+ thread_safe (0.3.3)
16
+ virtus (1.0.2)
17
+ axiom-types (~> 0.1)
18
+ coercible (~> 1.0)
19
+ descendants_tracker (~> 0.0.3)
20
+ equalizer (~> 0.0.9)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ rake
27
+ virtus
@@ -0,0 +1,7 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << "lib"
5
+ t.test_files = FileList['test/*_test.rb']
6
+ t.verbose = true
7
+ end
@@ -0,0 +1,29 @@
1
+ module Virtus
2
+ def self.delegate(klass)
3
+ Virtus::Delegate.for(klass)
4
+ end
5
+
6
+ module Delegate
7
+ extend self
8
+
9
+ def for(klass)
10
+ Module.new do
11
+ attr_reader :_target, :_old_targets
12
+
13
+ def initialize(target)
14
+ @_target = target
15
+ @_old_targets = []
16
+ end
17
+
18
+ klass.attribute_set.each do |attribute|
19
+ define_method(attribute.name) { _target.send(attribute.name) }
20
+ define_method(:"#{attribute.name}=") do |value|
21
+ @_old_targets << _target
22
+ @_target = _target.with(attribute.name => value)
23
+ value
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'test_helper'
2
+ require_relative 'mock_class'
3
+ require 'virtus/delegate'
4
+
5
+ describe Virtus::Delegate do
6
+ let(:object) { MockClass.new(id: 123, name: "Hello World", nickname: 'it works') }
7
+ subject { DelegatedClass.new(object) }
8
+
9
+ it "gets the readers correctly" do
10
+ assert_equal 123, subject.id
11
+ assert_equal "Hello World", subject.name
12
+ end
13
+
14
+ it "report the delegate class as the class" do
15
+ assert_equal DelegatedClass, subject.class
16
+ end
17
+
18
+ it "only delegates to the methods on the attribute set" do
19
+ assert_raises NoMethodError do
20
+ subject.other_method
21
+ end
22
+ end
23
+
24
+ it "allows you to use super correctly" do
25
+ assert_equal 'it works, omg', subject.nickname
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ require 'virtus'
2
+ require 'virtus/delegate'
3
+
4
+ class MockClass
5
+ include Virtus.value_object
6
+
7
+ values do
8
+ attribute :id, Integer
9
+ attribute :name, String
10
+ attribute :nickname, String
11
+ end
12
+
13
+ def other_method
14
+ true
15
+ end
16
+ end
17
+
18
+ class DelegatedClass
19
+ include Virtus.delegate(MockClass)
20
+
21
+ def nickname
22
+ super + ', omg'
23
+ end
24
+
25
+ def nickname=(val)
26
+ super(val.delete(' ').downcase)
27
+ end
28
+ end
@@ -0,0 +1,52 @@
1
+ require_relative 'test_helper'
2
+ require_relative 'mock_class'
3
+ require 'virtus/delegate'
4
+
5
+ describe Virtus::Delegate do
6
+ let(:object) { MockClass.new(id: 123, name: "Hello World", nickname: 'it works') }
7
+ subject { DelegatedClass.new(object) }
8
+
9
+ it "gets the writers correctly" do
10
+ subject.id = 456
11
+ subject.name = "Bye World"
12
+
13
+ assert_equal 456, subject.id
14
+ assert_equal "Bye World", subject.name
15
+ end
16
+
17
+ it "builds a new object" do
18
+ old_target = subject._target
19
+ subject.id = 456
20
+
21
+ assert_equal 123, old_target.id
22
+ assert_equal 456, subject.id
23
+ end
24
+
25
+ it "ACTUALLY creates a new object" do
26
+ old_target = subject._target
27
+ subject.id = 456
28
+
29
+ refute_equal old_target.object_id, subject._target.id
30
+ end
31
+
32
+ it "keeps older targets in history" do
33
+ old_target = subject._target
34
+ subject.id = 456
35
+
36
+ assert_equal [old_target], subject._old_targets
37
+ end
38
+
39
+ it "keeps more than one target in history" do
40
+ older_target = subject._target
41
+ subject.id = 1
42
+ old_target = subject._target
43
+ subject.id = 2
44
+
45
+ assert_equal [older_target, old_target], subject._old_targets
46
+ end
47
+
48
+ it "allows you to use super correctly" do
49
+ subject.nickname = 'Ninja Master'
50
+ assert_equal 'ninjamaster, omg', subject.nickname
51
+ end
52
+ end
@@ -0,0 +1,4 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'virtus'
4
+ require 'minitest/autorun'
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |gem|
3
+ gem.version = '0.0.1'
4
+
5
+ gem.authors = ["Cainã Costa"]
6
+ gem.email = ["cainan.costa@gmail.com"]
7
+ gem.description = %q{Adds a layer of delegation to your Virtus objects.}
8
+ gem.summary = %q{Adds a layer of delegation to your Virtus objects.}
9
+ gem.homepage = "https://github.com/cfcosta/virtus-delegate"
10
+
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- test/*`.split("\n")
13
+ gem.name = "virtus-delegate"
14
+ gem.require_paths = ["lib"]
15
+
16
+ gem.add_runtime_dependency 'virtus', '~>1.0'
17
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: virtus-delegate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cainã Costa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: virtus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: Adds a layer of delegation to your Virtus objects.
28
+ email:
29
+ - cainan.costa@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - Gemfile.lock
36
+ - Rakefile
37
+ - lib/virtus/delegate.rb
38
+ - test/getter_test.rb
39
+ - test/mock_class.rb
40
+ - test/setter_test.rb
41
+ - test/test_helper.rb
42
+ - virtus-delegate.gemspec
43
+ homepage: https://github.com/cfcosta/virtus-delegate
44
+ licenses: []
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.0.14
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Adds a layer of delegation to your Virtus objects.
66
+ test_files:
67
+ - test/getter_test.rb
68
+ - test/mock_class.rb
69
+ - test/setter_test.rb
70
+ - test/test_helper.rb