named_accessors 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rspec
6
+ /.idea
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v1.0
2
+
3
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in named_accessors.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ ## Description
2
+
3
+ You ever wanted to create attr_accessor with specified name? Now You can!
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem 'named_accessors', git: 'git@github.com:zlw/named_accessors.git'
9
+ ```
10
+
11
+ Gem was tested under 1.9.3
12
+
13
+ ## Usage
14
+
15
+ Just use named_accessor (or named_reader/named_writer) class method in Your class
16
+
17
+ ```ruby
18
+ class Post
19
+ def initialize(title)
20
+ @title, @content, @created_at = title, 'Lorem ipsum', Time.now
21
+ end
22
+
23
+ named_accessor :title, as: :fancy_title
24
+ named_writer :content, as: :post_content
25
+ named_reader :created_at, as: :when_was_it_created?
26
+ end
27
+
28
+ post = Post.new('Lorem ispum')
29
+ post.fancy_title #=> Lorem ipsum
30
+ post.fancy_title = 'Bored with lorem ipsum'
31
+
32
+ post.post_content = 'Maybe some true content here'
33
+
34
+ post.when_was_it_created? #=> 2012-02-15 15:56:08 +0100
35
+ ```
36
+
37
+ ## License
38
+
39
+ Copyright (C) 2012 Krzysztof Zalewski
40
+
41
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
42
+
43
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
44
+
45
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,8 @@
1
+ module NamedAccessors
2
+ module NamedAccessor
3
+ def named_accessor(name, options={})
4
+ named_writer(name, options)
5
+ named_reader(name, options)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ module NamedAccessors
2
+ module NamedReader
3
+ def named_reader(name, options={})
4
+ variable = instance_variable_name(name)
5
+
6
+ define_method getter_name(options) do
7
+ instance_variable_get variable
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module NamedAccessors
2
+ module NamedWriter
3
+ def named_writer(name, options={})
4
+ variable = instance_variable_name(name)
5
+
6
+ define_method setter_name(options) do |val|
7
+ instance_variable_set variable, val
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ module NamedAccessors
2
+ module Utilities
3
+ private
4
+ def getter_name(options)
5
+ extract_as_option(options)
6
+ end
7
+
8
+ def setter_name(options)
9
+ :"#{extract_as_option(options)}="
10
+ end
11
+
12
+ def extract_as_option(options)
13
+ options.fetch(:as) rescue raise "You must specify `as` option"
14
+ end
15
+
16
+ def instance_variable_name(name)
17
+ :"@#{name}"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module NamedAccessors
2
+ VERSION = "1.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ Dir[File.dirname(__FILE__) + '/named_accessors/*.rb'].each {|file| require file }
2
+
3
+ module NamedAccessors
4
+ include Utilities
5
+ include NamedReader
6
+ include NamedWriter
7
+ include NamedAccessor
8
+ end
9
+
10
+ class Object
11
+ extend NamedAccessors
12
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "named_accessors/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "named_accessors"
7
+ s.version = NamedAccessors::VERSION
8
+ s.authors = ["Krzysztof Zalewski"]
9
+ s.email = ["zlw.zalewski@gmail.com"]
10
+ s.homepage = "https://github.com/zlw/named_accessors"
11
+ s.summary = %q{Create attr_accessor (attr_reader/attr_writer) with specified name}
12
+ s.description = %q{Create attr_accessor (attr_reader/attr_writer) with specified name}
13
+
14
+ s.rubyforge_project = "named_accessors"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rake"
22
+ s.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Object do
4
+ let(:klass) { Object }
5
+
6
+ context '#extract_as_option' do
7
+ it 'should return as option' do
8
+ klass.send(:extract_as_option, {as: :foobar}).should == :foobar
9
+ end
10
+
11
+ it 'should raise exception when `as` option is not given' do
12
+ not_specified_as_option = lambda { klass.send(:extract_as_option, {}) }
13
+ not_specified_as_option.should raise_exception "You must specify `as` option"
14
+ end
15
+ end
16
+
17
+ context '#getter_name' do
18
+ it 'should return getter name' do
19
+ klass.send(:getter_name, {as: :dummy_getter}).should == :dummy_getter
20
+ end
21
+ end
22
+
23
+ context '#setter_name' do
24
+ it 'should return setter name' do
25
+ klass.send(:setter_name, {as: :dummy_setter}).should == :dummy_setter=
26
+ end
27
+ end
28
+
29
+ context '#instance_variable_name' do
30
+ it 'should return proper instance variable name' do
31
+ klass.send(:instance_variable_name, :foobar).should == :'@foobar'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
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
+ describe Object do
14
+ let(:post) { Post.new('foobar') }
15
+
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?)
19
+ end
20
+
21
+ it 'should return value of instance variable' do
22
+ post.when_was_it_created?.should == 'Today'
23
+ end
24
+ end
25
+
26
+ context '#named_writer' do
27
+ it 'should respond to setter method with specified name' do
28
+ post.should respond_to(:post_content=)
29
+ end
30
+
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
35
+ end
36
+ end
37
+
38
+ 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=)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1 @@
1
+ require 'named_accessors'
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: named_accessors
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Krzysztof Zalewski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70111683845780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70111683845780
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70111683845300 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70111683845300
36
+ description: Create attr_accessor (attr_reader/attr_writer) with specified name
37
+ email:
38
+ - zlw.zalewski@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - CHANGELOG.md
45
+ - Gemfile
46
+ - README.md
47
+ - Rakefile
48
+ - lib/named_accessors.rb
49
+ - lib/named_accessors/named_accessor.rb
50
+ - lib/named_accessors/named_reader.rb
51
+ - lib/named_accessors/named_writer.rb
52
+ - lib/named_accessors/utilities.rb
53
+ - lib/named_accessors/version.rb
54
+ - named_accessors.gemspec
55
+ - spec/named_accessors/named_accessor_module_spec.rb
56
+ - spec/named_accessors/named_accessors_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: https://github.com/zlw/named_accessors
59
+ licenses: []
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project: named_accessors
78
+ rubygems_version: 1.8.15
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Create attr_accessor (attr_reader/attr_writer) with specified name
82
+ test_files:
83
+ - spec/named_accessors/named_accessor_module_spec.rb
84
+ - spec/named_accessors/named_accessors_spec.rb
85
+ - spec/spec_helper.rb