typed_attributes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ module TypedAttributes
2
+ module Parser
3
+
4
+ class ArrayType < BaseType
5
+ def parse(values)
6
+ values.reject(&:blank?)
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ module TypedAttributes
2
+ module Parser
3
+
4
+ class BaseType
5
+ attr_reader :model, :attribute, :options
6
+
7
+ def initialize(model, attr, options = {})
8
+ @model = model
9
+ @attribute = attr
10
+ @options = options || {}
11
+ end
12
+
13
+ def parse(value)
14
+ raise NotImplementedError.new
15
+ end
16
+
17
+ def storage_var
18
+ :"@#{attribute}"
19
+ end
20
+
21
+ def readers
22
+ [attribute]
23
+ end
24
+
25
+ def writers
26
+ [:"#{attribute}="]
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ module TypedAttributes
2
+ module Parser
3
+
4
+ class BooleanType < BaseType
5
+ def attribute
6
+ super.to_s.gsub('?', '').to_sym
7
+ end
8
+
9
+ def readers
10
+ super << :"#{attribute}?"
11
+ end
12
+
13
+ def parse(value)
14
+ case value
15
+ when TrueClass, FalseClass
16
+ value
17
+ else
18
+ value = value.to_s.strip.downcase
19
+ %w(t true).include?(value) || value.to_i == 1
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ module TypedAttributes
2
+ module Parser
3
+
4
+ class DateTimeType < BaseType
5
+ def parse(value)
6
+ DateTime.new(value)
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ module TypedAttributes
2
+ module Parser
3
+
4
+ class EnumType < SymbolType
5
+ def parse(value)
6
+ value_sym = super(value)
7
+ if options[:from]
8
+ if options[:from].include?(value_sym)
9
+ value_sym
10
+ else
11
+ nil
12
+ end
13
+ else
14
+ value_sym
15
+ end
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ module TypedAttributes
2
+ module Parser
3
+
4
+ class FixnumType < StringType
5
+ def parse(value)
6
+ value.to_i
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ module TypedAttributes
2
+ module Parser
3
+
4
+ class FlagsType < ArrayType
5
+ def parse(values)
6
+ array = super(values)
7
+ array.map!(&:to_sym)
8
+ array.select! { |v| options[:from].include?(v) } if options[:from]
9
+ array
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module TypedAttributes
2
+ module Parser
3
+
4
+ class FloatType < StringType
5
+ def parse(value)
6
+ value.to_f
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module TypedAttributes
2
+ module Parser
3
+
4
+ class IdsType < ArrayType
5
+ def parse(values)
6
+ super(values).map(&:to_i)
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module TypedAttributes
2
+ module Parser
3
+
4
+ class StringType < BaseType
5
+ def parse(value)
6
+ value.to_s
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module TypedAttributes
2
+ module Parser
3
+
4
+ class SymbolType < StringType
5
+ def parse(value)
6
+ value.to_sym
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ module TypedAttributes
2
+ module Parser
3
+ end
4
+ end
5
+
6
+ require File.expand_path('../parser/base_type', __FILE__)
7
+ require File.expand_path('../parser/string_type', __FILE__)
8
+ require File.expand_path('../parser/symbol_type', __FILE__)
9
+ require File.expand_path('../parser/array_type', __FILE__)
10
+ require File.expand_path('../parser/boolean_type', __FILE__)
11
+ require File.expand_path('../parser/date_time_type', __FILE__)
12
+ require File.expand_path('../parser/enum_type', __FILE__)
13
+ require File.expand_path('../parser/fixnum_type', __FILE__)
14
+ require File.expand_path('../parser/flags_type', __FILE__)
15
+ require File.expand_path('../parser/float_type', __FILE__)
16
+ require File.expand_path('../parser/ids_type', __FILE__)
@@ -0,0 +1,45 @@
1
+ module TypedAttributes
2
+ def self.included(base)
3
+ base.send :extend, ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ def field_reader(attr, type_name, options = {})
8
+ caster = type_caster_by_name(type_name).new(self, attr, options)
9
+ caster.readers.each do |reader|
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)
15
+ end
16
+ value
17
+ end
18
+ end
19
+ true
20
+ end
21
+
22
+ def field_writer(attr, type_name, options = {})
23
+ caster = type_caster_by_name(type_name).new(self, attr, options)
24
+ caster.writers.each do |writer|
25
+ define_method writer do |value|
26
+ instance_variable_set(caster.storage_var, caster.parse(value))
27
+ end
28
+ end
29
+ true
30
+ end
31
+
32
+ def field_accessor(attr, type_name, options = {})
33
+ field_reader(attr, type_name, options)
34
+ field_writer(attr, type_name, options)
35
+ true
36
+ end
37
+
38
+ private
39
+ def type_caster_by_name(type_name)
40
+ TypedAttributes::Parser.const_get(type_name.to_s.camelize + 'Type')
41
+ end
42
+ end
43
+ end
44
+
45
+ require File.expand_path('../typed_attributes/parser', __FILE__)
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typed_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tobias Casper
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Casts non-persistent model fields to their respective types, just like
31
+ ActiveRecord does.
32
+ email: tobias.casper@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/typed_attributes.rb
38
+ - lib/typed_attributes/parser.rb
39
+ - lib/typed_attributes/parser/string_type.rb
40
+ - lib/typed_attributes/parser/float_type.rb
41
+ - lib/typed_attributes/parser/date_time_type.rb
42
+ - lib/typed_attributes/parser/array_type.rb
43
+ - lib/typed_attributes/parser/fixnum_type.rb
44
+ - lib/typed_attributes/parser/symbol_type.rb
45
+ - lib/typed_attributes/parser/enum_type.rb
46
+ - lib/typed_attributes/parser/boolean_type.rb
47
+ - lib/typed_attributes/parser/ids_type.rb
48
+ - lib/typed_attributes/parser/flags_type.rb
49
+ - lib/typed_attributes/parser/base_type.rb
50
+ homepage: http://rubygems.org/gems/casting_attributes
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.24
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Strictly typed non-persistent model attributes for Rails.
74
+ test_files: []