Skip to content
This repository
Browse code

Add spec for Virtus::ModuleExtensions#attribute

* Change Virtus::Attribute instances to be comparable using equalizer.
  • Loading branch information...
commit 8d959988d461dda1a160fcb6052d340ed3a8068d 1 parent cc1cd68
Dan Kubb authored June 11, 2012
1  lib/virtus/attribute.rb
@@ -7,6 +7,7 @@ class Attribute
7 7
     extend DescendantsTracker
8 8
     extend TypeLookup
9 9
     extend Options
  10
+    include Equalizer.new(inspect) << :name << :options
10 11
 
11 12
     accept_options :primitive, :accessor, :reader,
12 13
       :writer, :coercion_method, :default
8  lib/virtus/module_extensions.rb
@@ -4,8 +4,16 @@ module Virtus
4 4
   #
5 5
   module ModuleExtensions
6 6
 
  7
+    # Define an attribute in the module
  8
+    #
  9
+    # @see Virtus::Extensions#attribute
  10
+    #
  11
+    # @return [self]
  12
+    #
  13
+    # @api private
7 14
     def attribute(*args)
8 15
       attribute_definitions << args
  16
+      self
9 17
     end
10 18
 
11 19
   private
31  spec/unit/virtus/module_extensions/attribute_spec.rb
... ...
@@ -0,0 +1,31 @@
  1
+require 'spec_helper'
  2
+
  3
+describe Virtus::ModuleExtensions, '#attribute' do
  4
+  subject { object.attribute(name, type, options) }
  5
+
  6
+  let(:object)  { Module.new              }
  7
+  let(:name)    { :name                   }
  8
+  let(:type)    { String                  }
  9
+  let(:options) { { :default => default } }
  10
+  let(:default) { 'John Doe'.freeze       }
  11
+
  12
+  before do
  13
+    object.extend(Virtus::ModuleExtensions)
  14
+  end
  15
+
  16
+  it { should be(object) }
  17
+
  18
+  it 'tracks the attribute for extension' do
  19
+    subject
  20
+    instance = Object.new
  21
+    instance.extend(object)
  22
+    instance.attributes[name].should eql(default)
  23
+  end
  24
+
  25
+  it 'tracks the attribute for inclusion' do
  26
+    subject
  27
+    klass = Class.new
  28
+    klass.send(:include, object)
  29
+    klass.attribute_set[name].should eql(Virtus::Attribute::String.new(name, options))
  30
+  end
  31
+end

0 notes on commit 8d95998

Please sign in to comment.
Something went wrong with that request. Please try again.