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 CHANGED
@@ -1,3 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ gem 'activesupport'
data/lib/attribrutal.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "active_support/core_ext/hash"
2
+
1
3
  require "attribrutal/version"
2
4
  require "attribrutal/model.rb"
3
5
  require "attribrutal/coercion.rb"
@@ -1,33 +1,47 @@
1
- module Coercer
1
+ module Attribrutal
2
+ module Type
2
3
 
3
- class Boolean
4
- def self.coerce(arg, default = nil)
5
- if arg == true
6
- true
7
- else
8
- false
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
- class Integer
14
- def self.coerce(arg, default = nil)
15
- if arg.nil?
16
- default
17
- else
18
- Integer(arg)
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
- class String
24
- def self.coerce(arg, default = nil)
25
- if arg.nil?
26
- default
27
- else
28
- String(arg)
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
@@ -7,18 +7,14 @@ module Attribrutal
7
7
  end
8
8
 
9
9
  def initialize( attrs = {} )
10
- all_attributes = attrs.symbolize_keys
11
- attribrutal_attributes = all_attributes.select {|k,v| attribute_keys.include? k }
12
- other_attributes = all_attributes.reject {|k,v| attribute_keys.include? k }
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
- attribute_keys.inject(Hash.new) do |attributes, attribute|
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, @attributes[sym], attrs[:default])
45
+ coercer.send(:coerce, raw_attributes[sym], default_value)
42
46
  else
43
- @attributes[sym] || default
47
+ raw_attributes[sym] ||= default_value
44
48
  end
45
49
  end
46
50
 
47
51
  define_method("#{sym}=".to_sym) do |value|
48
- @attributes[sym] = value
52
+ raw_attributes[sym] = value
49
53
  end
50
54
 
51
55
  if @attributes
@@ -1,3 +1,3 @@
1
1
  module Attribrutal
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -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 == [Coercer::Boolean, Coercer::String, Coercer::Baz]
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
- 10_000.times { Bar.new }
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
- 10_000.times { Bar.new foo: nil, bar: 10, baz: { alpha: "1", beta: "2" } }
50
+ 1000.times { Bar.new(foo: nil, bar: 10, baz: { alpha: "1", beta: "2" }).attributes }
51
51
  end
52
- coercion_time.should < 50
52
+ coercion_time.should < 25
53
53
  end
54
54
 
55
55
  end
@@ -1,6 +1,6 @@
1
1
  class Bar
2
2
  include Attribrutal::Model
3
- attribute :foo, Coercer::Boolean
4
- attribute :bar, Coercer::String, default: "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
@@ -1,5 +1,5 @@
1
1
  class Baz
2
2
  include Attribrutal::Model
3
- attribute :alpha, Coercer::Integer, default: 1
4
- attribute :beta, Coercer::Integer, default: 2
3
+ attribute :alpha, Attribrutal::Type::Integer, default: 1
4
+ attribute :beta, Attribrutal::Type::Integer, default: 2
5
5
  end
@@ -3,7 +3,7 @@ module Coercer
3
3
  class Baz
4
4
  def self.coerce(arg, default = nil)
5
5
  if arg.nil?
6
- default.lambda? ? default.call : default
6
+ default
7
7
  else
8
8
  ::Baz.new(arg)
9
9
  end
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.6
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: 2151528194976546338
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: 2151528194976546338
108
+ hash: 4418677033310836668
109
109
  requirements: []
110
110
  rubyforge_project:
111
111
  rubygems_version: 1.8.23