From b9ad34fb8ada3bf022661e22ece3e11568048d1d Mon Sep 17 00:00:00 2001 From: Maciej Jarmula Date: Fri, 5 Oct 2018 15:58:30 +0100 Subject: [PATCH] Covernt question mark in instance variables --- lib/structural.rb | 1 + lib/structural/model/field.rb | 6 +++-- lib/structural/model/field/ivar_name.rb | 29 +++++++++++++++++++++ lib/structural/version.rb | 2 +- spec/lib/structural/model/field_spec.rb | 34 +++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 lib/structural/model/field/ivar_name.rb diff --git a/lib/structural.rb b/lib/structural.rb index 68127ce..99e065d 100644 --- a/lib/structural.rb +++ b/lib/structural.rb @@ -8,6 +8,7 @@ require 'structural/model/definer' require 'structural/model/descriptor' require 'structural/model/type_casts' +require 'structural/model/field/ivar_name' require 'structural/model/field' require 'structural/model/association' require 'structural/model/has_one' diff --git a/lib/structural/model/field.rb b/lib/structural/model/field.rb index 81dbf3f..c50353d 100644 --- a/lib/structural/model/field.rb +++ b/lib/structural/model/field.rb @@ -1,9 +1,11 @@ module Structural module Model class Field + include IvarName + def initialize(model, name, options) @model = model - @name = name + @name = name.to_s @options = options end @@ -34,7 +36,7 @@ def key end def ivar_name - @ivar_name ||= "@#{@name}" + @ivar_name ||= "@#{safe_name}" end def default_value diff --git a/lib/structural/model/field/ivar_name.rb b/lib/structural/model/field/ivar_name.rb new file mode 100644 index 0000000..77b03f9 --- /dev/null +++ b/lib/structural/model/field/ivar_name.rb @@ -0,0 +1,29 @@ +module Structural + module Model + class Field + module IvarName + def self.included(base) + base.extend(self) + end + + def safe_name + if name_ends_with_question_mark? + name_without_question_mark + else + name + end + end + + private + + def name_ends_with_question_mark? + name.last == '?' + end + + def name_without_question_mark + name.gsub(/\?$/, '_question_mark') + end + end + end + end +end diff --git a/lib/structural/version.rb b/lib/structural/version.rb index b228c23..77e0ebf 100644 --- a/lib/structural/version.rb +++ b/lib/structural/version.rb @@ -1,3 +1,3 @@ module Structural - VERSION = '0.2.0' + VERSION = '0.2.1' end diff --git a/spec/lib/structural/model/field_spec.rb b/spec/lib/structural/model/field_spec.rb index e69de29..f008b8f 100644 --- a/spec/lib/structural/model/field_spec.rb +++ b/spec/lib/structural/model/field_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +module Structural + module Model + describe Field do + subject { described_class.new(model, name, options) } + + let(:model) { 'model' } + let(:name) { 'name' } + let(:options) { {} } + + describe '#ivar_name' do + context 'when the name doesnt contain question mark' do + let(:ivar_name) { "@#{name}" } + + it 'returns name as an instance name' do + expect(subject.ivar_name).to eq(ivar_name) + end + end + end + + describe '#ivar_name' do + context 'when the name contains question mark' do + let(:name) { 'name?' } + let(:ivar_name) { '@name_question_mark' } + + it 'returns name as an instance name' do + expect(subject.ivar_name).to eq(ivar_name) + end + end + end + end + end +end