named_accessors 1.0 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
1
  ## v1.0
2
2
 
3
- * initial release
3
+ * initial release
4
+
5
+ ## v1.1
6
+
7
+ * allow to specify reader/writer name with named_accessor
data/README.md CHANGED
@@ -4,6 +4,12 @@ You ever wanted to create attr_accessor with specified name? Now You can!
4
4
 
5
5
  ## Installation
6
6
 
7
+ ```ruby
8
+ gem 'named_accessors'
9
+ ```
10
+
11
+ or
12
+
7
13
  ```ruby
8
14
  gem 'named_accessors', git: 'git@github.com:zlw/named_accessors.git'
9
15
  ```
@@ -23,6 +29,10 @@ class Post
23
29
  named_accessor :title, as: :fancy_title
24
30
  named_writer :content, as: :post_content
25
31
  named_reader :created_at, as: :when_was_it_created?
32
+
33
+ named_accessor :foo, reader: :foobar, writer: :foobaz
34
+ named_accessor :foo, as: :bar, reader: :foobar
35
+ named_accessor :foo, as: :bar, writer: :foobar
26
36
  end
27
37
 
28
38
  post = Post.new('Lorem ispum')
@@ -1,8 +1,10 @@
1
1
  module NamedAccessors
2
2
  module NamedAccessor
3
3
  def named_accessor(name, options={})
4
- named_writer(name, options)
5
- named_reader(name, options)
4
+ reader, writer = extract_accessor_options(options)
5
+
6
+ named_reader(name, reader)
7
+ named_writer(name, writer)
6
8
  end
7
9
  end
8
10
  end
@@ -16,5 +16,15 @@ module NamedAccessors
16
16
  def instance_variable_name(name)
17
17
  :"@#{name}"
18
18
  end
19
+
20
+ def extract_accessor_options(options)
21
+ [extract_option(options, :reader), extract_option(options, :writer)]
22
+ end
23
+
24
+ def extract_option(options, type)
25
+ option = options.fetch(type, nil)
26
+
27
+ option ? options.merge(as: option) : options
28
+ end
19
29
  end
20
30
  end
@@ -1,3 +1,3 @@
1
1
  module NamedAccessors
2
- VERSION = "1.0"
2
+ VERSION = "1.1"
3
3
  end
@@ -31,4 +31,46 @@ describe Object do
31
31
  klass.send(:instance_variable_name, :foobar).should == :'@foobar'
32
32
  end
33
33
  end
34
+
35
+ context '#extract_option' do
36
+ it 'should return changed `as` option to reader' do
37
+ klass.send(:extract_option, {as: :foo, reader: :foobar}, :reader)[:as].should == :foobar
38
+ end
39
+
40
+ it 'should return not changed `as` options if reader is not given' do
41
+ klass.send(:extract_option, {as: :foo}, :reader)[:as].should == :foo
42
+ end
43
+
44
+ it 'should return changed `as` option to writer' do
45
+ klass.send(:extract_option, {as: :foo, writer: :foobar}, :writer)[:as].should == :foobar
46
+ end
47
+
48
+ it 'should return not changed `as` option if writer is not given' do
49
+ klass.send(:extract_option, {as: :foo}, :writer)[:as].should == :foo
50
+ end
51
+ end
52
+
53
+ context '#extract_accessor_options' do
54
+ it 'should return array of option hashes' do
55
+ klass.send(:extract_accessor_options, {as: :foo}).should == [{as: :foo}, {as: :foo}]
56
+ end
57
+
58
+ it 'should return array of option hashes with changed reader' do
59
+ options = klass.send(:extract_accessor_options, {as: :foo, reader: :foobar})
60
+
61
+ reader, writer = options
62
+
63
+ reader[:as].should == :foobar
64
+ writer[:as].should == :foo
65
+ end
66
+
67
+ it 'should return array of option hashes with changed writer' do
68
+ options = klass.send(:extract_accessor_options, {as: :foo, writer: :foobar})
69
+
70
+ reader, writer = options
71
+
72
+ reader[:as].should == :foo
73
+ writer[:as].should == :foobar
74
+ end
75
+ end
34
76
  end
@@ -1,44 +1,61 @@
1
1
  require 'spec_helper'
2
2
 
3
- class Post
4
- def initialize(title)
5
- @title, @content, @created_at = title, '', 'Today'
6
- end
7
-
8
- named_accessor :title, as: :fancy_title
9
- named_writer :content, as: :post_content
10
- named_reader :created_at, as: :when_was_it_created?
11
- end
12
-
13
3
  describe Object do
14
4
  let(:post) { Post.new('foobar') }
5
+ let(:klass) { Object }
15
6
 
16
- context "#named_reader" do
17
- it "should respond to getter method with specified name" do
18
- post.should respond_to(:when_was_it_created?)
7
+ context '#named_reader' do
8
+ it 'should respond to getter method with specified name' do
9
+ klass.named_reader(:created_at, as: :when_was_it_created?)
10
+ klass.should respond_to :when_was_it_created?
19
11
  end
20
12
 
21
13
  it 'should return value of instance variable' do
22
- post.when_was_it_created?.should == 'Today'
14
+ klass.instance_variable_set(:'@created_at', 'Today')
15
+ klass.when_was_it_created?.should == 'Today'
23
16
  end
24
17
  end
25
18
 
26
- context '#named_writer' do
19
+ context '#named_writed' do
27
20
  it 'should respond to setter method with specified name' do
28
- post.should respond_to(:post_content=)
21
+ klass.named_writer(:content, as: :post_content)
22
+ klass.should respond_to :post_content=
29
23
  end
30
24
 
31
- it "should set value to instance method" do
32
- content = 'barbaz'
33
- post.post_content = content
34
- post.instance_variable_get(:"@content").should == content
25
+ it 'should set value to instance method' do
26
+ klass.post_content = 'Lorem ipsum'
27
+ klass.instance_variable_get(:'@content').should == 'Lorem ipsum'
35
28
  end
36
29
  end
37
30
 
38
31
  context '#named_accessor' do
39
- it "should respond to both getter and setter" do
40
- post.should respond_to(:fancy_title)
41
- post.should respond_to(:fancy_title=)
32
+ it 'should respond to both reader and writer' do
33
+ klass.named_accessor :title, as: :fancy_title
34
+ klass.should respond_to :fancy_title, :fancy_title=
35
+ end
36
+
37
+ it 'should respond to changed writer name' do
38
+ klass.named_accessor :foo, as: :foobar, writer: :bar
39
+ klass.should respond_to :foobar
40
+ klass.should respond_to :bar=
41
+ end
42
+
43
+ it 'should respond to changed reader name' do
44
+ klass.named_accessor :foo, as: :foobaz, reader: :baz
45
+ klass.should respond_to :foobaz=
46
+ klass.should respond_to :baz
47
+ end
48
+
49
+ it 'should respond to changed both reader and writer' do
50
+ klass.named_accessor :foo, as: :dummy, reader: :one, writer: :two
51
+ klass.should respond_to :one
52
+ klass.should respond_to :two=
53
+ end
54
+
55
+ it 'shoud respond to changed both reader and writer when `as` option is not given' do
56
+ klass.named_accessor :foo, reader: :reader, writer: :writer
57
+ klass.should respond_to :reader
58
+ klass.should respond_to :writer=
42
59
  end
43
60
  end
44
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: named_accessors
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: '1.1'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-15 00:00:00.000000000 Z
12
+ date: 2012-02-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70111683845780 !ruby/object:Gem::Requirement
16
+ requirement: &70118597470420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70111683845780
24
+ version_requirements: *70118597470420
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70111683845300 !ruby/object:Gem::Requirement
27
+ requirement: &70118597469940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70111683845300
35
+ version_requirements: *70118597469940
36
36
  description: Create attr_accessor (attr_reader/attr_writer) with specified name
37
37
  email:
38
38
  - zlw.zalewski@gmail.com
@@ -67,12 +67,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
67
  - - ! '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
+ segments:
71
+ - 0
72
+ hash: -3223851256822058182
70
73
  required_rubygems_version: !ruby/object:Gem::Requirement
71
74
  none: false
72
75
  requirements:
73
76
  - - ! '>='
74
77
  - !ruby/object:Gem::Version
75
78
  version: '0'
79
+ segments:
80
+ - 0
81
+ hash: -3223851256822058182
76
82
  requirements: []
77
83
  rubyforge_project: named_accessors
78
84
  rubygems_version: 1.8.15