attribrutal 0.0.6 → 0.0.10
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.
- data/Gemfile +2 -0
- data/lib/attribrutal.rb +2 -0
- data/lib/attribrutal/coercion.rb +36 -22
- data/lib/attribrutal/model.rb +15 -11
- data/lib/attribrutal/version.rb +1 -1
- data/spec/attribrutal_spec.rb +4 -4
- data/spec/support/models/bar.rb +2 -2
- data/spec/support/models/baz.rb +2 -2
- data/spec/support/models/coercer.rb +1 -1
- metadata +3 -3
    
        data/Gemfile
    CHANGED
    
    
    
        data/lib/attribrutal.rb
    CHANGED
    
    
    
        data/lib/attribrutal/coercion.rb
    CHANGED
    
    | @@ -1,33 +1,47 @@ | |
| 1 | 
            -
            module  | 
| 1 | 
            +
            module Attribrutal
         | 
| 2 | 
            +
              module Type
         | 
| 2 3 |  | 
| 3 | 
            -
             | 
| 4 | 
            -
             | 
| 5 | 
            -
             | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 4 | 
            +
                class Boolean
         | 
| 5 | 
            +
                  def self.coerce(arg, default = nil)
         | 
| 6 | 
            +
                    if arg == true
         | 
| 7 | 
            +
                      true
         | 
| 8 | 
            +
                    else
         | 
| 9 | 
            +
                      false
         | 
| 10 | 
            +
                    end
         | 
| 9 11 | 
             
                  end
         | 
| 10 12 | 
             
                end
         | 
| 11 | 
            -
              end
         | 
| 12 13 |  | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 14 | 
            +
                class Integer
         | 
| 15 | 
            +
                  def self.coerce(arg, default = nil)
         | 
| 16 | 
            +
                    if arg.nil?
         | 
| 17 | 
            +
                      default
         | 
| 18 | 
            +
                    else
         | 
| 19 | 
            +
                      Integer(arg)
         | 
| 20 | 
            +
                    end
         | 
| 19 21 | 
             
                  end
         | 
| 20 22 | 
             
                end
         | 
| 21 | 
            -
              end
         | 
| 22 23 |  | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 24 | 
            +
                class String
         | 
| 25 | 
            +
                  def self.coerce(arg, default = nil)
         | 
| 26 | 
            +
                    if arg.nil?
         | 
| 27 | 
            +
                      default
         | 
| 28 | 
            +
                    else
         | 
| 29 | 
            +
                      String(arg)
         | 
| 30 | 
            +
                    end
         | 
| 29 31 | 
             
                  end
         | 
| 30 32 | 
             
                end
         | 
| 31 | 
            -
              end
         | 
| 32 33 |  | 
| 34 | 
            +
                class Array
         | 
| 35 | 
            +
                  def self.coerce(arg, default = nil)
         | 
| 36 | 
            +
                    return default unless arg
         | 
| 37 | 
            +
                    arg.collect {|member| @subtype.coerce(member) }
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                  def self.[] (subtype)
         | 
| 41 | 
            +
                    @subtype = subtype
         | 
| 42 | 
            +
                    self
         | 
| 43 | 
            +
                  end
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              end
         | 
| 33 47 | 
             
            end
         | 
    
        data/lib/attribrutal/model.rb
    CHANGED
    
    | @@ -7,18 +7,14 @@ module Attribrutal | |
| 7 7 | 
             
                end
         | 
| 8 8 |  | 
| 9 9 | 
             
                def initialize( attrs = {} )
         | 
| 10 | 
            -
                  all_attributes | 
| 11 | 
            -
                   | 
| 12 | 
            -
                  other_attributes | 
| 13 | 
            -
                  @attributes = attribrutal_attributes
         | 
| 10 | 
            +
                  all_attributes         = attrs.symbolize_keys
         | 
| 11 | 
            +
                  @raw_attributes        = all_attributes.select {|k,v| attribute_keys.include? k }
         | 
| 12 | 
            +
                  other_attributes       = all_attributes.reject {|k,v| attribute_keys.include? k }
         | 
| 14 13 | 
             
                  other_attributes.map {|key, val| self.send("#{key}=", val) if respond_to?("#{key}=") }
         | 
| 15 14 | 
             
                end
         | 
| 16 15 |  | 
| 17 16 | 
             
                def raw_attributes
         | 
| 18 | 
            -
                   | 
| 19 | 
            -
                    attributes[attribute] = @attributes[attribute]
         | 
| 20 | 
            -
                    attributes
         | 
| 21 | 
            -
                  end
         | 
| 17 | 
            +
                  @raw_attributes
         | 
| 22 18 | 
             
                end
         | 
| 23 19 |  | 
| 24 20 | 
             
                def attributes
         | 
| @@ -37,15 +33,23 @@ module Attribrutal | |
| 37 33 | 
             
                  def attribute (sym, coercer=nil, attrs = {})
         | 
| 38 34 |  | 
| 39 35 | 
             
                    define_method(sym) do
         | 
| 36 | 
            +
                      default_value = case attrs[:default].class.name
         | 
| 37 | 
            +
                                      when "NilClass", "TrueClass", "FalseClass", "Numeric", "Fixnum", "Symbol"
         | 
| 38 | 
            +
                                        attrs[:default]
         | 
| 39 | 
            +
                                      when "Proc"
         | 
| 40 | 
            +
                                        attrs[:default].call
         | 
| 41 | 
            +
                                      else
         | 
| 42 | 
            +
                                        attrs[:default].clone
         | 
| 43 | 
            +
                                      end
         | 
| 40 44 | 
             
                      if coercer && coercer.respond_to?(:coerce)
         | 
| 41 | 
            -
                        coercer.send(:coerce,  | 
| 45 | 
            +
                        coercer.send(:coerce, raw_attributes[sym], default_value)
         | 
| 42 46 | 
             
                      else
         | 
| 43 | 
            -
                         | 
| 47 | 
            +
                        raw_attributes[sym] ||= default_value
         | 
| 44 48 | 
             
                      end
         | 
| 45 49 | 
             
                    end
         | 
| 46 50 |  | 
| 47 51 | 
             
                    define_method("#{sym}=".to_sym) do |value|
         | 
| 48 | 
            -
                       | 
| 52 | 
            +
                      raw_attributes[sym] = value
         | 
| 49 53 | 
             
                    end
         | 
| 50 54 |  | 
| 51 55 | 
             
                    if @attributes
         | 
    
        data/lib/attribrutal/version.rb
    CHANGED
    
    
    
        data/spec/attribrutal_spec.rb
    CHANGED
    
    | @@ -10,7 +10,7 @@ describe "Attribrutal#attributes" do | |
| 10 10 | 
             
              end
         | 
| 11 11 |  | 
| 12 12 | 
             
              it "records defined attribute types" do
         | 
| 13 | 
            -
                Bar.attributes.values.should == [ | 
| 13 | 
            +
                Bar.attributes.values.should == [Attribrutal::Type::Boolean, Attribrutal::Type::String, Coercer::Baz]
         | 
| 14 14 | 
             
              end
         | 
| 15 15 |  | 
| 16 16 | 
             
              it "supports mass assignment" do
         | 
| @@ -42,14 +42,14 @@ describe "Attribrutal#attributes" do | |
| 42 42 |  | 
| 43 43 | 
             
              it "is fast enough" do
         | 
| 44 44 | 
             
                initialization_time = 1000 * Benchmark.realtime do
         | 
| 45 | 
            -
                   | 
| 45 | 
            +
                  1000.times { Bar.new }
         | 
| 46 46 | 
             
                end
         | 
| 47 47 | 
             
                initialization_time.should < 5
         | 
| 48 48 |  | 
| 49 49 | 
             
                coercion_time = 1000 * Benchmark.realtime do
         | 
| 50 | 
            -
                   | 
| 50 | 
            +
                  1000.times { Bar.new(foo: nil, bar: 10, baz: { alpha: "1", beta: "2" }).attributes }
         | 
| 51 51 | 
             
                end
         | 
| 52 | 
            -
                coercion_time.should <  | 
| 52 | 
            +
                coercion_time.should < 25
         | 
| 53 53 | 
             
              end
         | 
| 54 54 |  | 
| 55 55 | 
             
            end
         | 
    
        data/spec/support/models/bar.rb
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            class Bar
         | 
| 2 2 | 
             
              include Attribrutal::Model
         | 
| 3 | 
            -
              attribute :foo,  | 
| 4 | 
            -
              attribute :bar,  | 
| 3 | 
            +
              attribute :foo, Attribrutal::Type::Boolean
         | 
| 4 | 
            +
              attribute :bar, Attribrutal::Type::String, default: "bar"
         | 
| 5 5 | 
             
              attribute :baz, Coercer::Baz, default: lambda { ::Baz.new alpha: 50, beta: 100 }
         | 
| 6 6 | 
             
            end
         | 
    
        data/spec/support/models/baz.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: attribrutal
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.10
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -96,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement | |
| 96 96 | 
             
                  version: '0'
         | 
| 97 97 | 
             
                  segments:
         | 
| 98 98 | 
             
                  - 0
         | 
| 99 | 
            -
                  hash:  | 
| 99 | 
            +
                  hash: 4418677033310836668
         | 
| 100 100 | 
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 101 101 | 
             
              none: false
         | 
| 102 102 | 
             
              requirements:
         | 
| @@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 105 105 | 
             
                  version: '0'
         | 
| 106 106 | 
             
                  segments:
         | 
| 107 107 | 
             
                  - 0
         | 
| 108 | 
            -
                  hash:  | 
| 108 | 
            +
                  hash: 4418677033310836668
         | 
| 109 109 | 
             
            requirements: []
         | 
| 110 110 | 
             
            rubyforge_project: 
         | 
| 111 111 | 
             
            rubygems_version: 1.8.23
         |