nd_transcriber 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: b38a8fc84b338d68d21a77e46d9701cb45d5fd3d
4
- data.tar.gz: 2b2809662477271a5fc709cc62a2b075a270f427
3
+ metadata.gz: e32b9fb1b31388648d8e4a0fe0dcd8778cc3182e
4
+ data.tar.gz: 36df75be15a632094efe1fd2d4b02cfe11ffe15c
5
5
  SHA512:
6
- metadata.gz: d56af65d922c2e2935793eb95a496b15667f51e468bbfa70c693b4148f2886e48924ac69859256acb93b11769ac31ad0c6dee4190c9b9bab6e1c1d87a5991b5b
7
- data.tar.gz: fb679153664c21ad718cea94af008d71dca4751837745c7885bd0521416f9c225d51a2a61b0cd46b2b6642e483fda14cbb81d97cc8446e2a807d0e01938746cf
6
+ metadata.gz: a00aa9c9594a3ee44e44fd56e18cba47eb5e983fcaa1f2d3a75e1c45496b75f544eb1d11af97e16edf28bc8feb1024ad34f8912845017a18de8e9c93de80e917
7
+ data.tar.gz: 102e8f7119ea4be32d1be08ba234e673b05d32b5952d35e8d183567771cce9da10e681aaeea42dc0349abc6cd46cf1db54e42e82263c5e1d2a66a25ed2a64e11
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,65 @@
1
+ # Transcriber
2
+
3
+ This gem provides an easy way to copy attributes around.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ gem install nd_transcriber
9
+ ```
10
+
11
+ Using bundler:
12
+
13
+ ```ruby
14
+ gem 'nd_transcriber'
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ Basically, you just need to include `NdTranscriber` and define a block wich will
20
+ be called every time an object is copied.
21
+
22
+ ```ruby
23
+ # We are going to copy from Origin objects.
24
+ Origin = Struct.new(:name, :last_name, :gender)
25
+
26
+ # Dest objects will receive the copied values
27
+ Dest = Struct.new(:name, :last_name, :complete_name, :male_female, :transcribed,
28
+ :nickname) do
29
+ include NdTranscriber
30
+
31
+ transcriber do
32
+ copy_fields :last_name, # Copies field last_name as is
33
+ :gender => :male_female, # Copies field gender to field male_female
34
+ :name => [:name, :nickname] # Copies field name to both name and nickname
35
+
36
+ copy :name, to: :complete_name do |name| # Uses the value returned from the block
37
+ # origin is a method that returns the object we are currently copying data from
38
+ "#{name} #{origin.last_name}"
39
+ end
40
+
41
+ # You can also just assign attributes to self.
42
+ self.transcribed = true
43
+ end
44
+ end
45
+
46
+ origin = Origin.new('John', 'Doe', 'M')
47
+
48
+ # The kind of object being copied doesn't matter. It just needs to have the
49
+ # attributes or methods NdTranscriber was told to copy from.
50
+ dest = Dest.transcribe(origin)
51
+
52
+ dest.name # => 'John'
53
+ dest.nickname # => 'John'
54
+ dest.last_name # => 'Doe'
55
+ dest.complete_name # => 'John Doe'
56
+ dest.male_female # => 'M'
57
+ dest.transcribed # => true
58
+ ```
59
+
60
+ ## Why?
61
+
62
+ I need (more than I'd like) to sync data between databases. Initially, I created
63
+ just a class to do so. I just needed to copy attributes from one object to another,
64
+ customizing some values eventually. Now, I'm making it available as a Gem, so
65
+ others can benefit from it and I can share it across projects easily.
@@ -0,0 +1,38 @@
1
+ module NdTranscriber
2
+ class Copier
3
+ attr_accessor :origin, :dest
4
+
5
+ def initialize origin, dest
6
+ self.origin, self.dest = origin, dest
7
+ end
8
+
9
+ def copy_fields *fields
10
+ fields.each do |field|
11
+ if field.is_a?(Hash)
12
+ field.each_pair do |from, to|
13
+ copy from, to: to
14
+ end
15
+ else
16
+ copy field
17
+ end
18
+ end
19
+ dest
20
+ end
21
+
22
+ def copy field, to: field
23
+ value = (field && origin.respond_to?(field) ? origin.send(field) : nil )
24
+ value = yield(value) if block_given?
25
+ if to.is_a?(Array)
26
+ to.each do |f|
27
+ dest.send("#{f}=", value)
28
+ end
29
+ else
30
+ dest.send("#{to}=", value)
31
+ end
32
+ end
33
+
34
+ def method_missing method, *args, &block
35
+ dest.send(method, *args, &block)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'nd_transcriber'
3
+ s.version = '0.0.2'
4
+ s.date = '2014-09-12'
5
+ s.summary = "Copy object's attributes with ease."
6
+ s.description = "This gem provides an easy way to copy attributes around."
7
+ s.authors = ["Diego Aguir Selzlein"]
8
+ s.email = 'diegoselzlein@gmail.com'
9
+
10
+ s.files = `git ls-files`.split("\n")
11
+ s.require_paths = ["lib"]
12
+
13
+ s.homepage = 'https://github.com/nerde/nd_transcriber'
14
+ s.license = 'MIT'
15
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ Origin = Struct.new(:name, :last_name, :gender)
4
+
5
+ Dest = Struct.new(:name, :last_name, :complete_name, :male_female, :transcribed,
6
+ :nickname) do
7
+ include NdTranscriber
8
+
9
+ transcriber do
10
+ copy_fields :last_name,
11
+ :gender => :male_female,
12
+ :name => [:name, :nickname]
13
+ copy :name, to: :complete_name do |name|
14
+ "#{name} #{origin.last_name}"
15
+ end
16
+ self.transcribed = true
17
+ end
18
+ end
19
+
20
+ Dest2 = Struct.new(:id) do
21
+ include NdTranscriber
22
+ transcribes_to do |origin|
23
+ new(5) if origin
24
+ end
25
+ end
26
+
27
+ describe NdTranscriber do
28
+ let(:origin) { Origin.new('John', 'Doe', 'M') }
29
+
30
+ subject { Dest.transcribe(origin) }
31
+
32
+ it "copies attributes" do
33
+ expect(subject.name) .to eq 'John'
34
+ expect(subject.nickname) .to eq 'John'
35
+ expect(subject.last_name) .to eq 'Doe'
36
+ expect(subject.complete_name).to eq 'John Doe'
37
+ expect(subject.male_female) .to eq 'M'
38
+ expect(subject.transcribed) .to eq true
39
+ end
40
+
41
+ it "yields the copied object" do
42
+ expect{ |probe| Dest.transcribe(origin, &probe) }.to yield_with_args(
43
+ kind_of(Dest))
44
+ end
45
+
46
+ context "transcribes_to is provided" do
47
+ subject(:dest2) { Dest2.transcribe(origin) }
48
+
49
+ it "uses transcribes_to to define where to transcribe" do
50
+ expect(dest2.id).to eq(5)
51
+ end
52
+ end
53
+
54
+ describe "copy_all" do
55
+ let(:list) { [Origin.new('John', 'Doe', 'M'),
56
+ Origin.new('Mary', 'Doe', 'F')] }
57
+
58
+ subject(:dests) { Dest.transcribe_all(list) }
59
+
60
+ it 'copies all of the objects' do
61
+ expect(dests.size ).to eq 2
62
+ expect(dests[1].name ).to eq 'Mary'
63
+ expect(dests[1].class).to eq Dest
64
+ end
65
+
66
+ it "yields each copied object" do
67
+ expect{ |probe| Dest.transcribe_all(list, &probe) }.to yield_successive_args(
68
+ kind_of(Dest), kind_of(Dest))
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../../lib/nd_transcriber', __FILE__)
2
+
3
+ RSpec.configure do |config|
4
+ config.order = "random"
5
+ config.before(:each) { GC.disable }
6
+ config.after(:each) { GC.enable }
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nd_transcriber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego Aguir Selzlein
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-19 00:00:00.000000000 Z
11
+ date: 2014-09-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem provides an easy way to copy attributes around.
14
14
  email: diegoselzlein@gmail.com
@@ -16,7 +16,13 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - ".rspec"
20
+ - README.md
19
21
  - lib/nd_transcriber.rb
22
+ - lib/nd_transcriber/copier.rb
23
+ - nd_transcriber.gemspec
24
+ - spec/lib/nd_transcriber_spec.rb
25
+ - spec/spec_helper.rb
20
26
  homepage: https://github.com/nerde/nd_transcriber
21
27
  licenses:
22
28
  - MIT
@@ -42,3 +48,4 @@ signing_key:
42
48
  specification_version: 4
43
49
  summary: Copy object's attributes with ease.
44
50
  test_files: []
51
+ has_rdoc: