casting_attr_accessor 0.1.0

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.
@@ -0,0 +1,11 @@
1
+ module CastingAttrAccessor
2
+ module Parser
3
+
4
+ class Array < Base
5
+ def parse(values)
6
+ values.reject(&:blank?)
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ module CastingAttrAccessor
2
+ module Parser
3
+
4
+ class Base
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 default_value
14
+ default = options[:default]
15
+ case default
16
+ when Proc then default.call(model)
17
+ else default
18
+ end
19
+ end
20
+
21
+ def parse(value)
22
+ raise NotImplementedError.new
23
+ end
24
+
25
+ def storage_var
26
+ :"@#{attribute}"
27
+ end
28
+
29
+ def readers
30
+ [attribute]
31
+ end
32
+
33
+ def writers
34
+ [:"#{attribute}="]
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,29 @@
1
+ module CastingAttrAccessor
2
+ module Parser
3
+
4
+ class Boolean < Base
5
+ def attribute
6
+ super.to_s.gsub('?', '').to_sym
7
+ end
8
+
9
+ def default_value
10
+ super || false
11
+ end
12
+
13
+ def readers
14
+ super << :"#{attribute}?"
15
+ end
16
+
17
+ def parse(value)
18
+ case value
19
+ when ::TrueClass, ::FalseClass
20
+ value
21
+ else
22
+ value = value.to_s.strip.downcase
23
+ %w(t true).include?(value) || value.to_i == 1
24
+ end
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ module CastingAttrAccessor
2
+ module Parser
3
+
4
+ class Date < Base
5
+ def parse(value)
6
+ ::Date.parse(value)
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module CastingAttrAccessor
2
+ module Parser
3
+
4
+ class DateTime < Base
5
+ def parse(value)
6
+ ::DateTime.parse(value)
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module CastingAttrAccessor
2
+ module Parser
3
+
4
+ class Enum < Symbol
5
+ def parse(value)
6
+ if options.key?(:from)
7
+ super(value) if options[:from].include?(value_sym)
8
+ else
9
+ raise ArgumentError.new 'Flags attribute has to specify a set of possible values'
10
+ end
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module CastingAttrAccessor
2
+ module Parser
3
+
4
+ class Fixnum < Base
5
+ def parse(value)
6
+ value.to_i
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module CastingAttrAccessor
2
+ module Parser
3
+
4
+ class Flags < Array
5
+ def parse(values)
6
+ raise ArgumentError.new 'Flags attribute has to specify a set of possible values' unless options.key?(:from)
7
+ array = super(values)
8
+ array.map!(&:to_sym)
9
+ array.select! { |v| options[:from].include?(v) }
10
+ array
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module CastingAttrAccessor
2
+ module Parser
3
+
4
+ class Float < String
5
+ def parse(value)
6
+ value.to_f
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module CastingAttrAccessor
2
+ module Parser
3
+
4
+ class Ids < Array
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 CastingAttrAccessor
2
+ module Parser
3
+
4
+ class String < Base
5
+ def parse(value)
6
+ value.to_s
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module CastingAttrAccessor
2
+ module Parser
3
+
4
+ class Symbol < Base
5
+ def parse(value)
6
+ value.to_sym
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module CastingAttrAccessor
2
+ module Parser
3
+ end
4
+ end
5
+
6
+ require File.expand_path('../parser/base', __FILE__)
7
+ require File.expand_path('../parser/string', __FILE__)
8
+ require File.expand_path('../parser/symbol', __FILE__)
9
+ require File.expand_path('../parser/array', __FILE__)
10
+ require File.expand_path('../parser/boolean', __FILE__)
11
+ require File.expand_path('../parser/date', __FILE__)
12
+ require File.expand_path('../parser/date_time', __FILE__)
13
+ require File.expand_path('../parser/enum', __FILE__)
14
+ require File.expand_path('../parser/fixnum', __FILE__)
15
+ require File.expand_path('../parser/flags', __FILE__)
16
+ require File.expand_path('../parser/float', __FILE__)
17
+ require File.expand_path('../parser/ids', __FILE__)
@@ -0,0 +1,74 @@
1
+ module CastingAttrAccessor
2
+ def self.included(base)
3
+ base.send :extend, ClassMethods
4
+ end
5
+
6
+ def self.extended(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def casting_attr_reader(*args)
12
+ parser_for_attributes(*args) do |parser|
13
+ define_readers(parser)
14
+ end
15
+ end
16
+
17
+ def casting_attr_reader(*args)
18
+ parser_for_attributes(*args) do |parser|
19
+ define_writers(parser)
20
+ end
21
+ end
22
+
23
+ def casting_attr_accessor(*args)
24
+ parser_for_attributes(*args) do |parser|
25
+ define_readers(parser)
26
+ define_writers(parser)
27
+ end
28
+ end
29
+
30
+ private
31
+ def parser_for_attributes(*args)
32
+ options = args.extract_options!
33
+ parser_class = get_parser(options.delete(:as) || :string)
34
+ args.each do |attr|
35
+ yield parser_class.new(self, attr, options)
36
+ end
37
+ end
38
+
39
+ def define_readers(parser)
40
+ parser.readers.each do |reader|
41
+ define_method reader do
42
+ unless instance_variable_defined?(parser.storage_var)
43
+ instance_variable_set(parser.storage_var, parser.default_value)
44
+ end
45
+ instance_variable_get(parser.storage_var)
46
+ end
47
+ end
48
+ end
49
+
50
+ def define_writers(parser)
51
+ parser.writers.each do |writer|
52
+ define_method writer do |value|
53
+ instance_variable_set(parser.storage_var, parser.parse(value))
54
+ end
55
+ end
56
+ end
57
+
58
+ def get_parser(name)
59
+ case name
60
+ when Class
61
+ unless type_name < CastingAttrAccessor::Parser::Base
62
+ raise ArgumentError.new 'Type has to inherit from CastingAttrAccessor::Parser::Base'
63
+ end
64
+ name
65
+ when String, Symbol
66
+ CastingAttrAccessor::Parser.const_get(name.to_s.camelize)
67
+ else
68
+ raise ArgumentError.new 'Unknown parser type'
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ require File.expand_path('../casting_attr_accessor/parser', __FILE__)
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: casting_attr_accessor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tobias Casper
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-23 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: '3.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: '3.0'
30
+ description: Casts non-persistent model fields to their specified types, just like
31
+ ActiveRecord does.
32
+ email: tobias.casper@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/casting_attr_accessor.rb
38
+ - lib/casting_attr_accessor/parser/string.rb
39
+ - lib/casting_attr_accessor/parser/ids.rb
40
+ - lib/casting_attr_accessor/parser/array.rb
41
+ - lib/casting_attr_accessor/parser/date.rb
42
+ - lib/casting_attr_accessor/parser/float.rb
43
+ - lib/casting_attr_accessor/parser/fixnum.rb
44
+ - lib/casting_attr_accessor/parser/enum.rb
45
+ - lib/casting_attr_accessor/parser/base.rb
46
+ - lib/casting_attr_accessor/parser/boolean.rb
47
+ - lib/casting_attr_accessor/parser/symbol.rb
48
+ - lib/casting_attr_accessor/parser/flags.rb
49
+ - lib/casting_attr_accessor/parser/date_time.rb
50
+ - lib/casting_attr_accessor/parser.rb
51
+ homepage: https://github.com/tlux/casting_attr_accessor
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.24
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Strongly typed, casting, non-persistent model attributes for ActiveModel.
75
+ test_files: []