fluent_accessors 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 527b4fe77875ef004cd143dfe5faa826fbb94366
4
+ data.tar.gz: d7e5b16b0e70f877cf6523b0874181aea18d5737
5
+ SHA512:
6
+ metadata.gz: a59dc662cb657633b9c8a5cfd05d699db7493b1fa23c04db06ab4bf2493249101d3a471ad1b43d98d4dcb792a27f0ee2be264fb13ccaee2513c46bd0b5ba182c
7
+ data.tar.gz: 0b058c12dc332ef237b18e82472934d477503ad437e5f0c3b0a16a18fa989a8546326bb665ad34842bcce71204d97c94263d3800ba8add907ff161eefbff7097
data/.gitignore ADDED
@@ -0,0 +1,57 @@
1
+ .idea
2
+
3
+ *.gem
4
+ *.rbc
5
+ /.config
6
+ /coverage/
7
+ /InstalledFiles
8
+ /pkg/
9
+ /spec/reports/
10
+ /test/tmp/
11
+ /test/version_tmp/
12
+ /tmp/
13
+ .bundle
14
+ .config
15
+ .yardoc
16
+ Gemfile.lock
17
+ InstalledFiles
18
+ _yardoc
19
+ coverage
20
+ doc/
21
+ lib/bundler/man
22
+ pkg
23
+ rdoc
24
+ spec/reports
25
+ test/tmp
26
+ test/version_tmp
27
+ tmp
28
+ *.bundle
29
+ *.so
30
+ *.o
31
+ *.a
32
+ mkmf.log
33
+
34
+
35
+ ## Specific to RubyMotion:
36
+ .dat*
37
+ .repl_history
38
+ build/
39
+
40
+ ## Documentation cache and generated files:
41
+ /.yardoc/
42
+ /_yardoc/
43
+ /doc/
44
+ /rdoc/
45
+
46
+ ## Environment normalisation:
47
+ /.bundle/
48
+ /lib/bundler/man/
49
+
50
+ # for a library or gem, you might want to ignore these files since the code is
51
+ # intended to run in multiple environments; otherwise, check them in:
52
+ # Gemfile.lock
53
+ # .ruby-version
54
+ # .ruby-gemset
55
+
56
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
57
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,20 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ addons:
5
+ code_climate:
6
+ repo_token: 672fdd75b2f1fff020629c07108bf49cffc8fe716c73bc7ca5758dc3eb5124bd
7
+
8
+
9
+ rvm:
10
+ - 2.1.1
11
+ - 2.0.0
12
+
13
+ script: 'bundle exec rake spec'
14
+
15
+ notifications:
16
+ email:
17
+ recipients:
18
+ - eturino@eturino.com
19
+ on_failure: change
20
+ on_success: never
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent_accessors.gemspec
4
+ gemspec
5
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Eduardo Turiño
2
+
3
+ MIT License
4
+
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.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # FluentAccessors
2
+
3
+ Adds class method `fluent_accessor` which will create several methods to enable fluent API access to the instance variables.
4
+
5
+ ## Dependencies
6
+
7
+ it requires Ruby 2, since it uses keyword arguments
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'fluent_accessors'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fluent_accessors
22
+
23
+ ## Usage
24
+
25
+ ### Basic usage
26
+
27
+ ```ruby
28
+ class TestKlass
29
+ extend FluentAccessors
30
+ fluent_accessor :something
31
+ end
32
+
33
+ x = TestKlass.new
34
+
35
+ # normal setter
36
+ x.something = 1
37
+
38
+ # normal getter
39
+ x.something # => 1
40
+
41
+ # fluent setter method
42
+ x.set_something 2 # returns self
43
+ x.something # => 2
44
+
45
+ # using getter with argument => fluent method
46
+ x.something 3 # returns self
47
+ x.something # => 3
48
+ ```
49
+
50
+ ### fluent method
51
+
52
+ the Fluent method (getter with an argument) will:
53
+
54
+ 1. always return `self`
55
+ 2. it will *not* set the value directly:
56
+ a. if the object responds to a `set_myproperty` method, it will call that and assume that it will
57
+ b. if the object does not respond to a `set_myproperty` method, it will call the normal setter `myproperty=`
58
+ c. if the object does not respond to a `set_my_property` nor `myproperty=` methods, it will set the property directly.
59
+
60
+ ### avoiding set_something method
61
+
62
+ if you don't want the `set_something` method, you can specify not to create it.
63
+
64
+ ```ruby
65
+ class TestKlass
66
+ extend FluentAccessors
67
+ fluent_accessor :something, set_method: false
68
+ end
69
+
70
+ x = TestKlass.new
71
+ s.respond_to? :set_method # => false
72
+ ```
73
+
74
+ ### avoiding creating the writer method
75
+
76
+ if you don't want the `something=` method, you can specify not to create it.
77
+
78
+ ```ruby
79
+ class TestKlass
80
+ extend FluentAccessors
81
+ fluent_accessor :something, writer_method: false
82
+ end
83
+
84
+ x = TestKlass.new
85
+ s.respond_to? :something= # => false
86
+ ```
87
+
88
+
89
+ ## Contributing
90
+
91
+ 1. Fork it ( https://github.com/eturino/fluent_accessors/fork )
92
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
93
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
94
+ 4. Push to the branch (`git push origin my-new-feature`)
95
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.rspec_opts = ['--color']
8
+ end
9
+
10
+ task :default => :spec
11
+
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fluent_accessors/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fluent_accessors"
8
+ spec.version = FluentAccessors::VERSION
9
+ spec.authors = ["Eduardo Turiño"]
10
+ spec.email = ["(none)"]
11
+ spec.summary = %q{Gem that provides a macro for creating fluent accessors for instance variables, working similar to attr_accessor}
12
+ # spec.description = %q{TODO: Write a longer description. Optional.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "rspec-nc"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "pry-nav"
28
+ spec.add_development_dependency "pry-rescue"
29
+ spec.add_development_dependency "pry-stack_explorer"
30
+ spec.add_development_dependency "pry-doc"
31
+
32
+ end
@@ -0,0 +1,71 @@
1
+ require "fluent_accessors/version"
2
+
3
+ module FluentAccessors
4
+ def fluent_accessor(*fields, set_method: true, writer_method: true)
5
+ Runner.new(self, fields, set_method, writer_method).call
6
+ end
7
+
8
+ class Runner
9
+ attr_reader :klass, :fields, :setter_enabled, :writer_enabled
10
+
11
+ def initialize(klass, fields, setter_enabled, writer_enabled)
12
+ @klass = klass
13
+ @fields = fields
14
+ @setter_enabled = setter_enabled
15
+ @writer_enabled = writer_enabled
16
+ end
17
+
18
+ def call
19
+ create_writer
20
+ fields.each do |field|
21
+ deal_with_field(field)
22
+ end
23
+ end
24
+
25
+ private
26
+ def create_writer
27
+ if writer_enabled
28
+ klass.send :attr_writer, *fields
29
+ end
30
+ end
31
+
32
+ def deal_with_field(field)
33
+ getter = field
34
+ normal_setter = "#{field}="
35
+ fluent_setter = "set_#{field}"
36
+ instance_var = "@#{field}"
37
+
38
+ create_setter(fluent_setter, normal_setter)
39
+ create_fluent(fluent_setter, getter, instance_var, normal_setter)
40
+ end
41
+
42
+ def create_fluent(fluent_setter, getter, instance_var, normal_setter)
43
+ klass.send :define_method, getter do |*args, &block|
44
+ if args.empty?
45
+ instance_variable_get instance_var
46
+ else
47
+ if respond_to? fluent_setter
48
+ send fluent_setter, *args, &block
49
+ elsif respond_to? normal_setter
50
+ send normal_setter, *args, &block
51
+ else
52
+ instance_variable_set instance_var, args.first
53
+ end
54
+
55
+ self
56
+ end
57
+ end
58
+ end
59
+
60
+ def create_setter(fluent_setter, normal_setter)
61
+ if setter_enabled
62
+ # fluent setter that calls the normal setter and returns self
63
+ klass.send :define_method, fluent_setter do |value, &block|
64
+ send normal_setter, value, &block
65
+ self
66
+ end
67
+ end
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module FluentAccessors
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe FluentAccessors do
4
+
5
+ class FluentAccessorsTestKlass
6
+ extend FluentAccessors
7
+ fluent_accessor :something
8
+ fluent_accessor :other, set_method: false
9
+ fluent_accessor :another, set_method: false
10
+ fluent_accessor :no_writer, writer_method: false, set_method: false
11
+
12
+ def initialize(something, other, another)
13
+ @something = something
14
+ @other = other
15
+ @another = another
16
+ end
17
+
18
+ def set_another(value)
19
+ @set_called = true
20
+
21
+ @another = value
22
+ :return_of_set_another
23
+ end
24
+
25
+ def setter_called?
26
+ !!@set_called
27
+ end
28
+ end
29
+
30
+ describe FluentAccessorsTestKlass do
31
+ subject { described_class.new :hey, :you, :there }
32
+ describe '#something (fluent setter/getter)' do
33
+ describe '#something as getter' do
34
+ it 'return the value' do
35
+ expect(subject.something).to eq :hey
36
+ end
37
+ end
38
+
39
+ describe '#something("val")' do
40
+ it 'set the value and return self' do
41
+ res = subject.something :x
42
+ expect(subject).to be res
43
+ expect(subject.something).to eq :x
44
+ end
45
+ end
46
+
47
+ describe '#set_something("val")' do
48
+ it 'set the value and return self' do
49
+ res = subject.set_something :x
50
+ expect(subject).to be res
51
+ expect(subject.something).to eq :x
52
+ end
53
+ end
54
+ end
55
+
56
+ context 'without fluent setter' do
57
+ describe '#set_other("val")' do
58
+ it 'does not have the method' do
59
+ expect(subject).not_to respond_to :set_other
60
+ end
61
+ end
62
+
63
+ describe '#other("val") (fluent setter)' do
64
+ it 'set the value and return self' do
65
+ res = subject.other :x
66
+ expect(subject).to be res
67
+ expect(subject.other).to eq :x
68
+ end
69
+ end
70
+ end
71
+
72
+ context 'without fluent setter, but with a different set_x method ' do
73
+ describe '#another("val") (fluent setter)' do
74
+ it '(call set_another) and return self, regardless of what set_another returns' do
75
+ res = subject.another :x
76
+ expect(subject).to be res
77
+ expect(subject.another).to eq :x
78
+ expect(subject).to be_setter_called
79
+ end
80
+ end
81
+ end
82
+
83
+ context 'without writer setter' do
84
+ describe '#no_writer("val") (fluent setter)' do
85
+ it 'sets the instance variable the and return self' do
86
+ # check that it works with arrays
87
+ res = subject.no_writer [:x]
88
+ expect(subject).to be res
89
+ expect(subject.no_writer).to eq [:x]
90
+ end
91
+
92
+ it 'does not have the setter' do
93
+ expect(subject).not_to respond_to :no_writer=
94
+ expect(subject).not_to respond_to :set_writer
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ end
@@ -0,0 +1,12 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'codeclimate-test-reporter'
5
+ CodeClimate::TestReporter.start
6
+
7
+ require 'pry'
8
+ require 'fluent_accessors'
9
+
10
+ RSpec.configure do |config|
11
+ # some (optional) config here
12
+ end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent_accessors
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Eduardo Turiño
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
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: rspec-nc
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: pry
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: pry-nav
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: pry-rescue
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: pry-stack_explorer
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: pry-doc
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
+ description:
140
+ email:
141
+ - "(none)"
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".travis.yml"
149
+ - Gemfile
150
+ - LICENSE.txt
151
+ - README.md
152
+ - Rakefile
153
+ - fluent_accessors.gemspec
154
+ - lib/fluent_accessors.rb
155
+ - lib/fluent_accessors/version.rb
156
+ - spec/fluent_accessors_spec.rb
157
+ - spec/spec_helper.rb
158
+ homepage: ''
159
+ licenses:
160
+ - MIT
161
+ metadata: {}
162
+ post_install_message:
163
+ rdoc_options: []
164
+ require_paths:
165
+ - lib
166
+ required_ruby_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ required_rubygems_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ requirements: []
177
+ rubyforge_project:
178
+ rubygems_version: 2.2.2
179
+ signing_key:
180
+ specification_version: 4
181
+ summary: Gem that provides a macro for creating fluent accessors for instance variables,
182
+ working similar to attr_accessor
183
+ test_files:
184
+ - spec/fluent_accessors_spec.rb
185
+ - spec/spec_helper.rb
186
+ has_rdoc: