attr_readonly 0.0.1 → 1.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06e0134796e7e42dfa0200161011496bda8bb40a
4
- data.tar.gz: 767f511807d1ff558bbcc3079dd537dde90ddc0c
3
+ metadata.gz: 27965629590e66f907071bae9837d1b90b70e03f
4
+ data.tar.gz: ab2de0b16b3cd2e0e28844408287dbbd9f477b65
5
5
  SHA512:
6
- metadata.gz: 1867b9539b2028da09d187df96fb782e34abae4b851dc40b1a19de4f5e8ba3fb8841639c2cc1b44633137fb3ba16d9d19d9eff768f2f8bc60f90e8fcc4e0f9be
7
- data.tar.gz: 76ec478a356caadd690682bf3ff2d4627a021bb8cc408f2a664ed35f17c2062be4344e79b7ecfb7e2be92281ff117f57f0dcbad384168581ad20c879bbe5784f
6
+ metadata.gz: e6fcee4acdb5afbb97fd7cb55eb645122343bd12e8eb7502ccd4570a50a1e845046d383bdf674ede81b208a5ec98ae888ec61265a146088c5d66fa7109112bb0
7
+ data.tar.gz: fbbe277f83bcbd3bd2251d81bc4b126efc3a9eb6eabffcce8b810358c8413a5e54ed67c4140fa3d77df15b35a83b5ffbf27e74c371962b54340f6254cad559e5
@@ -1,22 +1,23 @@
1
- Copyright (c) 2013 Romain GEORGES
1
+ attr_readonly Copyright (c) 2012-2013 Ultragreen Software, Romain GEORGES
2
+ All rights reserved.
2
3
 
3
- MIT License
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions
6
+ are met:
7
+ 1. Redistributions of source code must retain the above copyright
8
+ notice, this list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright
10
+ notice, this list of conditions and the following disclaimer in the
11
+ documentation and/or other materials provided with the distribution.
4
12
 
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23
+ SUCH DAMAGE.
data/README.md CHANGED
@@ -1,6 +1,22 @@
1
- # AttrReadonly
1
+ # attr_readonly
2
2
 
3
- TODO: Write a gem description
3
+ ## Author
4
+
5
+ Romain GEORGES <romain@ultragreen.net>
6
+ Ultragreen Software
7
+
8
+ ## Copyright
9
+
10
+ Ultragreen Software (c) 2013
11
+ BSD-2 License
12
+
13
+ ## Description
14
+
15
+ Provide a new macro like attr_accessor to make real read_only accessors to frozen dup of the attributs given in parameters
16
+
17
+ ## Version
18
+
19
+ current : 1.0.0
4
20
 
5
21
  ## Installation
6
22
 
@@ -18,7 +34,47 @@ Or install it yourself as:
18
34
 
19
35
  ## Usage
20
36
 
21
- TODO: Write usage instructions here
37
+ This gem add the macro : attr_readonly to class definition
38
+
39
+ You could create reader accessors, in Ruby like :
40
+
41
+ class Test
42
+ attr_reader :foo
43
+ def initialize(foo: '')
44
+ @foo = foo
45
+ end
46
+ end
47
+
48
+ if you try to do a direct affectation you raise an exception NoMethodError :
49
+
50
+ Test::new.foo= 'bar'
51
+ # => undefined method `foo=' for #<Test:0x00000801932598 @foo=""> (NoMethodError)
52
+
53
+ But if you try to use a method on this accessor (for exemple <<), you could modify it !
54
+
55
+ test = Test::new
56
+ p test.foo << 'bar'
57
+ # => "bar"
58
+ p test
59
+ # => <Test:0x000008019323b8 @foo="bar">
60
+
61
+ if you want to secure an API, or prevent inapriopriate usage of a componant you want to diffuse, this a problem.
62
+
63
+ So, use this gem, replace the macro attr_reader by attr_readonly :
64
+
65
+ require 'attr_readonly'
66
+ class Test
67
+ attr_readonly :foo
68
+ def initialize(foo: '')
69
+ @foo = foo
70
+ end
71
+ end
72
+
73
+ Test::new.foo << 'bar'
74
+ # => can't modify frozen String (RuntimeError)
75
+
76
+ No fear, it's just a frozen copy (dup), your real attribut is not frozen
77
+
22
78
 
23
79
  ## Contributing
24
80
 
data/Rakefile CHANGED
@@ -1 +1,57 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rubygems'
3
+ require 'rspec'
4
+ require 'rake'
5
+ require "rake/clean"
6
+ require "rubygems/package_task"
7
+ require "rdoc/task"
8
+ require 'code_statistics'
9
+ require 'rspec/core/rake_task'
10
+ require 'yard'
11
+ require 'yard/rake/yardoc_task.rb'
12
+ require "github/markup"
13
+ require "redcarpet"
14
+ require "rake/tasklib"
15
+ require "roodi"
16
+ require "roodi_task"
17
+
18
+
19
+ RoodiTask.new() do | t |
20
+ t.patterns = %w(lib/**/*.rb)
21
+ t.config = "ultragreen_roodi_coding_convention.yml"
22
+ end
23
+
24
+
25
+ CLEAN.include('*.tmp','*.old')
26
+ CLOBBER.include('*.tmp', 'build/*','#*#')
27
+
28
+
29
+ content = File::readlines(File.join(File.dirname(__FILE__), 'attr_readonly.gemspec')).join
30
+ spec = eval(content)
31
+
32
+ RSpec::Core::RakeTask.new('spec')
33
+
34
+
35
+
36
+ YARD::Rake::YardocTask.new do |t|
37
+ t.files = [ 'lib/**/*.rb', '-','doc/**/*','spec/**/*_spec.rb']
38
+ t.options = %w(--markup-provider=redcarpet --markup=markdown --main=README.md)
39
+ t.options += ['-o', "yardoc"]
40
+ end
41
+ YARD::Config.load_plugin('yard-rspec')
42
+
43
+ namespace :yardoc do
44
+ task :clobber do
45
+ rm_r "yardoc" rescue nil
46
+ rm_r ".yardoc" rescue nil
47
+ end
48
+ end
49
+ task :clobber => "yardoc:clobber"
50
+
51
+
52
+ Gem::PackageTask.new(spec) do |pkg|
53
+ pkg.need_tar = true
54
+ pkg.need_zip = true
55
+ end
56
+
57
+ task :default => [:gem]
@@ -20,4 +20,12 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency('rspec')
24
+ spec.add_development_dependency('yard')
25
+ spec.add_development_dependency('rdoc')
26
+ spec.add_development_dependency('roodi')
27
+ spec.add_development_dependency('code_statistics')
28
+ spec.add_development_dependency('yard-rspec')
29
+ spec.add_development_dependency('github_markup')
30
+ spec.add_development_dependency('redcarpet')
23
31
  end
@@ -1,8 +1,38 @@
1
- require "attr_readonly/version"
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ # Copyright Ultragreen (c) 2012-2�~B013
4
+ #---
5
+ # Author : Romain GEORGES
6
+ # type : gem component library
7
+ # obj : Methodic Module
8
+ #---
9
+
10
+ # require version is securing for debug
11
+ begin
12
+ require "attr_readonly/version"
13
+ rescue LoadError
14
+ puts 'debug'
15
+ end
2
16
 
17
+ # module Methodic
18
+ # @author Romain GEORGES <romain@ultragreen.net>
19
+ # @see http://www.ultragreen.net/projects/attr_reader
20
+ # @version 1.0.0
21
+ # @note this module include a method to be mix in Module (Class)
3
22
  module AttrReadonly
4
23
 
5
-
24
+
25
+ # pretty accessor for specifying mandatories options
26
+ # @param [*Symbol] syms a list of symbols defining accessors to add
27
+ # @return [Array] Array of symbols methods name (accessors) created
28
+ # @example usage
29
+ # require 'attr_readonly'
30
+ # class Test
31
+ # attr_readonly :foo
32
+ # def initialize(foo: '')
33
+ # @foo = :foo
34
+ # end
35
+ # end
6
36
  def attr_readonly *syms
7
37
  syms.each do |method|
8
38
  define_method(method){
@@ -14,8 +44,15 @@ module AttrReadonly
14
44
  end
15
45
 
16
46
 
47
+
48
+ # reimplement Module
49
+ # adding AttrReadonly Mixin for attr_readonly macro
17
50
  class Module
18
51
 
52
+ # include AttrReadonly
19
53
  include AttrReadonly
20
54
 
21
55
  end
56
+
57
+
58
+
@@ -1,3 +1,6 @@
1
+
2
+
1
3
  module AttrReadonly
2
- VERSION = "0.0.1"
4
+ # current version of attr_readonly
5
+ VERSION = "1.0.0"
3
6
  end
@@ -0,0 +1,136 @@
1
+ require './lib/attr_readonly.rb'
2
+
3
+ describe "Globale Conception" do
4
+
5
+
6
+ describe AttrReadonly do
7
+ subject { AttrReadonly }
8
+ specify { subject.should be_an_instance_of Module }
9
+ context "#attr_readonly" do
10
+ it "should be defined a method call :attr_readonly in the module" do
11
+ subject.method_defined?(:attr_readonly).should be_true
12
+ end
13
+ it "should accept a variable number of parameters" do
14
+ subject.method(:attr_readonly).arity.should == -1
15
+ end
16
+ end
17
+ end
18
+
19
+
20
+ describe Module do
21
+ subject { Module }
22
+ specify { subject.should be_an_instance_of Class }
23
+ it "should include module AttrReadonly" do
24
+ subject.include?(AttrReadonly).should be_true
25
+ end
26
+ it "should include method :attr_readonly"do
27
+ subject.method_defined?(:attr_readonly).should be_true
28
+ end
29
+ end
30
+
31
+
32
+ describe "Test Class instance" do
33
+
34
+ before :all do
35
+
36
+ class Test
37
+ attr_readonly :array
38
+ attr_readonly :string
39
+ attr_readonly :hash
40
+ def initialize(array: [], hash: {}, string: "")
41
+ @array = array
42
+ @hash = hash
43
+ @string = string
44
+ end
45
+ end
46
+ end
47
+
48
+
49
+
50
+
51
+
52
+ subject { Test::new }
53
+ specify { subject.should be_an_instance_of Test }
54
+
55
+ describe "instance variables" do
56
+ it "should be exist @hash with an empty hash" do
57
+ subject.instance_variable_get('@hash').should == {}
58
+ end
59
+ it "should be exist @array with an empty array" do
60
+ subject.instance_variable_get('@array').should == []
61
+ end
62
+ it "should be exist @string with an empty string" do
63
+ subject.instance_variable_get('@string').should == ""
64
+ end
65
+ end
66
+ describe "Reader accessors" do
67
+ it "should include method :array" do
68
+ subject.methods.include?(:array).should be_true
69
+ end
70
+ it "should include method :string" do
71
+ subject.methods.include?(:string).should be_true
72
+ end
73
+ it "should include method :hash" do
74
+ subject.methods.include?(:hash).should be_true
75
+ end
76
+ end
77
+ describe "No writer accessors" do
78
+ it "should not include method :array=" do
79
+ subject.methods.include?(:array=).should be_false
80
+ end
81
+ it "should not include method :string=" do
82
+ subject.methods.include?(:string=).should be_false
83
+ end
84
+ it "should not include method :hash=" do
85
+ subject.methods.include?(:hash=).should be_false
86
+ end
87
+
88
+ end
89
+ describe "initials values for read accessors" do
90
+ it "should #hash return an empty hash" do
91
+ subject.hash.should == {}
92
+ end
93
+ it "should #array return an empty array" do
94
+ subject.array.should == []
95
+ end
96
+ it "should #string return an empty string" do
97
+ subject.string.should == ""
98
+ end
99
+ end
100
+
101
+ describe "Attempt to write by attributs object methods" do
102
+ it "should raise an exception if trying to write hash[:test] = 'test'" do
103
+ lambda {subject.hash[:test] = 'test'}.should raise_error RuntimeError
104
+ lambda {subject.hash.store(:key,'test')}.should raise_error RuntimeError
105
+ lambda {subject.hash.merge!({:key => 'test'})}.should raise_error RuntimeError
106
+ end
107
+ it "should raise an exception if trying to write array[1] = 'test'" do
108
+ lambda {subject.array[1] = 'test'}.should raise_error RuntimeError
109
+ lambda {subject.array.push('test')}.should raise_error RuntimeError
110
+ end
111
+ it "should raise an exception if trying to write string << 'test'" do
112
+ lambda {subject.string << 'test' }.should raise_error RuntimeError
113
+ lambda {subject.string.concat(33)}.should raise_error RuntimeError
114
+ end
115
+
116
+ end
117
+
118
+ describe "Getting the instance variable via the accessors could be done, if set by the inside of the Object (simulate via instance_variable_set)" do
119
+ it "should #hash return {:test => 'test'}" do
120
+ subject.instance_variable_set(:@hash,{:test => 'test'})
121
+ subject.hash.should == {:test => 'test'}
122
+ end
123
+ it "should #array return [:test]" do
124
+ subject.instance_variable_set(:@array,[:test])
125
+ subject.array.should == [:test]
126
+ end
127
+ it "should #string return 'test'" do
128
+ subject.instance_variable_set(:@string,'test')
129
+ subject.string.should == "test"
130
+ end
131
+ end
132
+
133
+
134
+ end
135
+
136
+ end
@@ -0,0 +1 @@
1
+ require "spec_helper"
@@ -0,0 +1,16 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ end
12
+
13
+ require 'rubygems'
14
+ require 'rspec'
15
+
16
+
@@ -0,0 +1,25 @@
1
+ AssignmentInConditionalCheck:
2
+ CaseMissingElseCheck:
3
+ ClassLineCountCheck:
4
+ line_count: 300
5
+ ClassNameCheck:
6
+ pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/
7
+ #ClassVariableCheck:
8
+ CyclomaticComplexityBlockCheck:
9
+ complexity: 5
10
+ CyclomaticComplexityMethodCheck:
11
+ complexity: 10
12
+ EmptyRescueBodyCheck:
13
+ ForLoopCheck:
14
+ MethodLineCountCheck:
15
+ line_count: 30
16
+ MethodNameCheck:
17
+ pattern: !ruby/regexp /^[_a-z<>=\[|+-\/\*`]+[_a-z0-9_<>=~@\[\]]*[=!\?]?$/
18
+ # MissingForeignKeyIndexCheck:
19
+ ModuleLineCountCheck:
20
+ line_count: 500
21
+ ModuleNameCheck:
22
+ pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/
23
+ ParameterNumberCheck:
24
+ parameter_count: 5
25
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_readonly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Romain GEORGES
@@ -38,6 +38,118 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rdoc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: roodi
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: code_statistics
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: yard-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: github_markup
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: redcarpet
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
41
153
  description: Provide a new macro like attr_accessor to make real read_only accessors
42
154
  to frozen dup of the attributs given in parameters
43
155
  email:
@@ -54,6 +166,10 @@ files:
54
166
  - attr_readonly.gemspec
55
167
  - lib/attr_readonly.rb
56
168
  - lib/attr_readonly/version.rb
169
+ - spec/attr_readonly_spec.rb
170
+ - spec/init_spec.rb
171
+ - spec/spec_helper.rb
172
+ - ultragreen_roodi_coding_convention.yml
57
173
  homepage: ''
58
174
  licenses:
59
175
  - BSD
@@ -78,4 +194,7 @@ rubygems_version: 2.0.7
78
194
  signing_key:
79
195
  specification_version: 4
80
196
  summary: Provide a new macro like attr_accessor to make real read_only accessors
81
- test_files: []
197
+ test_files:
198
+ - spec/attr_readonly_spec.rb
199
+ - spec/init_spec.rb
200
+ - spec/spec_helper.rb