typed_attributes 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/typed_attributes.rb +14 -9
  2. metadata +4 -4
@@ -8,15 +8,12 @@ module TypedAttributes
8
8
  caster = type_caster_by_name(type_name).new(self, attr, options)
9
9
  caster.readers.each do |reader|
10
10
  define_method reader do
11
- value = instance_variable_get(caster.storage_var)
12
- if value.nil?
13
- value = options[:default]
14
- instance_variable_set(caster.storage_var, value)
11
+ unless instance_variable_defined?(caster.storage_var)
12
+ instance_variable_set(caster.storage_var, options[:default])
15
13
  end
16
- value
14
+ instance_variable_get(caster.storage_var)
17
15
  end
18
16
  end
19
- true
20
17
  end
21
18
 
22
19
  def field_writer(attr, type_name, options = {})
@@ -26,18 +23,26 @@ module TypedAttributes
26
23
  instance_variable_set(caster.storage_var, caster.parse(value))
27
24
  end
28
25
  end
29
- true
30
26
  end
31
27
 
32
28
  def field_accessor(attr, type_name, options = {})
33
29
  field_reader(attr, type_name, options)
34
30
  field_writer(attr, type_name, options)
35
- true
36
31
  end
37
32
 
38
33
  private
39
34
  def type_caster_by_name(type_name)
40
- TypedAttributes::Parser.const_get(type_name.to_s.camelize + 'Type')
35
+ case type_name
36
+ when Class
37
+ unless type_name < TypedAttributes::Parser::BaseType
38
+ raise ArgumentError.new 'Supplied type does not inherit from TypedAttributes::Parser::BaseType'
39
+ end
40
+ type_name
41
+ when Symbol, String
42
+ TypedAttributes::Parser.const_get(type_name.to_s.camelize + 'Type')
43
+ else
44
+ raise ArgumentError.new 'Invalid type'
45
+ end
41
46
  end
42
47
  end
43
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: '2.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: '2.0'
30
30
  description: Casts non-persistent model fields to their respective types, just like
31
31
  ActiveRecord does.
32
32
  email: tobias.casper@gmail.com
@@ -47,7 +47,7 @@ files:
47
47
  - lib/typed_attributes/parser/ids_type.rb
48
48
  - lib/typed_attributes/parser/flags_type.rb
49
49
  - lib/typed_attributes/parser/base_type.rb
50
- homepage: http://rubygems.org/gems/casting_attributes
50
+ homepage: http://rubygems.org/gems/typed_attributes
51
51
  licenses: []
52
52
  post_install_message:
53
53
  rdoc_options: []