From 88f62b4acd57edf2fb89208a30a6ceecfa586cb0 Mon Sep 17 00:00:00 2001 From: did Date: Wed, 16 Sep 2015 10:24:52 +0200 Subject: [PATCH] implement attr_accessor_initialize --- Gemfile.lock | 11 +++++++--- lib/locomotive/common.rb | 1 + lib/locomotive/common/attr_extras_ext.rb | 23 +++++++++++++++++++++ locomotivecms_common.gemspec | 1 + spec/unit/attr_extras_ext_spec.rb | 26 ++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 lib/locomotive/common/attr_extras_ext.rb create mode 100644 spec/unit/attr_extras_ext_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index f665e31..49078ea 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,18 +3,20 @@ PATH specs: locomotivecms_common (0.0.4) activesupport (~> 4.2.1) + attr_extras (~> 4.4.0) colorize stringex (~> 2.5.2) GEM remote: https://rubygems.org/ specs: - activesupport (4.2.1) + activesupport (4.2.4) i18n (~> 0.7) json (~> 1.7, >= 1.7.7) minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) + attr_extras (4.4.0) codeclimate-test-reporter (0.4.7) simplecov (>= 0.7.1, < 1.0.0) colorize (0.7.7) @@ -31,9 +33,9 @@ GEM http-cookie (1.0.2) domain_name (~> 0.5) i18n (0.7.0) - json (1.8.2) + json (1.8.3) mime-types (2.4.3) - minitest (5.6.0) + minitest (5.8.0) multi_json (1.11.0) netrc (0.10.3) rake (10.2.2) @@ -81,3 +83,6 @@ DEPENDENCIES locomotivecms_common! rake (~> 10.1) rspec (~> 3.2.0) + +BUNDLED WITH + 1.10.6 diff --git a/lib/locomotive/common.rb b/lib/locomotive/common.rb index 8c2f6c1..35641ac 100644 --- a/lib/locomotive/common.rb +++ b/lib/locomotive/common.rb @@ -1,5 +1,6 @@ require_relative 'common/version' require_relative 'common/core_ext' +require_relative 'common/attr_extras_ext' require_relative 'common/logger' require_relative 'common/exception' require_relative 'common/notifier' diff --git a/lib/locomotive/common/attr_extras_ext.rb b/lib/locomotive/common/attr_extras_ext.rb new file mode 100644 index 0000000..b024528 --- /dev/null +++ b/lib/locomotive/common/attr_extras_ext.rb @@ -0,0 +1,23 @@ +require 'attr_extras' + +# https://github.com/barsoom/attr_extras/issues/18 +module AttrExtrasExt + + def self.mixin + self::Mixin + end + + module Mixin + + def attr_accessor_initialize(*names, &block) + attr_initialize(*names, &block) + attr_accessor(*AttrExtras::Utils.flat_names(names)) + end + + end + +end + +class Module + include AttrExtrasExt.mixin +end diff --git a/locomotivecms_common.gemspec b/locomotivecms_common.gemspec index f51590a..0876ea9 100644 --- a/locomotivecms_common.gemspec +++ b/locomotivecms_common.gemspec @@ -21,6 +21,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'activesupport', '~> 4.2.1' spec.add_dependency 'stringex', '~> 2.5.2' + spec.add_dependency 'attr_extras', '~> 4.4.0' spec.add_dependency 'colorize' spec.required_ruby_version = '~> 2.0' diff --git a/spec/unit/attr_extras_ext_spec.rb b/spec/unit/attr_extras_ext_spec.rb new file mode 100644 index 0000000..eaea53b --- /dev/null +++ b/spec/unit/attr_extras_ext_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe AttrExtrasExt do + + subject { Person.new('Hank', 'Moody') } + + describe 'reader' do + + it { expect(subject.first_name).to eq 'Hank' } + it { expect(subject.last_name).to eq 'Moody' } + + end + + describe 'writer' do + + before { subject.first_name = 'Karen' } + + it { expect(subject.first_name).to eq 'Karen' } + + end + + + class Person + attr_accessor_initialize :first_name, :last_name + end +end