casting_attributes 0.0.1
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/lib/casting_attributes/type_caster/base.rb +30 -0
- data/lib/casting_attributes/type_caster/boolean.rb +25 -0
- data/lib/casting_attributes/type_caster/fixnum.rb +11 -0
- data/lib/casting_attributes/type_caster/float.rb +11 -0
- data/lib/casting_attributes/type_caster/string.rb +11 -0
- data/lib/casting_attributes/type_caster.rb +10 -0
- data/lib/casting_attributes.rb +50 -0
- metadata +68 -0
@@ -0,0 +1,30 @@
|
|
1
|
+
module CastingAttributes
|
2
|
+
module TypeCaster
|
3
|
+
|
4
|
+
class Base
|
5
|
+
attr_reader :model, :attribute
|
6
|
+
|
7
|
+
def initialize(model, attr)
|
8
|
+
@model = model
|
9
|
+
@attribute = attr
|
10
|
+
end
|
11
|
+
|
12
|
+
def storage_var
|
13
|
+
:"@#{attribute}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def readers
|
17
|
+
[attribute]
|
18
|
+
end
|
19
|
+
|
20
|
+
def writers
|
21
|
+
[:"#{attribute}="]
|
22
|
+
end
|
23
|
+
|
24
|
+
def typecast(value)
|
25
|
+
raise NotImplementedError.new
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module CastingAttributes
|
2
|
+
module TypeCaster
|
3
|
+
|
4
|
+
class Boolean < Base
|
5
|
+
def attribute
|
6
|
+
super.to_s.gsub('?', '').to_sym
|
7
|
+
end
|
8
|
+
|
9
|
+
def readers
|
10
|
+
super << :"#{attribute}?"
|
11
|
+
end
|
12
|
+
|
13
|
+
def typecast(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,10 @@
|
|
1
|
+
module CastingAttributes
|
2
|
+
module TypeCaster
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
require File.expand_path('../type_caster/base', __FILE__)
|
7
|
+
require File.expand_path('../type_caster/boolean', __FILE__)
|
8
|
+
require File.expand_path('../type_caster/fixnum', __FILE__)
|
9
|
+
require File.expand_path('../type_caster/float', __FILE__)
|
10
|
+
require File.expand_path('../type_caster/string', __FILE__)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module CastingAttributes
|
2
|
+
def self.included(base)
|
3
|
+
base.send :extend, ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def casting_attr_reader(attr, type_name, options = {})
|
8
|
+
default_value = options[:default]
|
9
|
+
|
10
|
+
caster = _caster_by_type_name(type_name).new(self, attr)
|
11
|
+
caster.readers.each do |reader|
|
12
|
+
define_method reader do
|
13
|
+
value = instance_variable_get(caster.storage_var)
|
14
|
+
if !value and default_value
|
15
|
+
instance_variable_set(caster.storage_var, default_value)
|
16
|
+
value = default_value
|
17
|
+
end
|
18
|
+
value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
def casting_attr_writer(attr, type_name)
|
25
|
+
caster = _caster_by_type_name(type_name).new(self, attr)
|
26
|
+
caster.writers.each do |writer|
|
27
|
+
define_method writer do |value|
|
28
|
+
instance_variable_set(caster.storage_var, caster.typecast(value))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
def casting_attr_accessor(attr, type_name, options = {})
|
35
|
+
casting_attr_reader(attr, type_name, options)
|
36
|
+
casting_attr_writer(attr, type_name)
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def _caster_by_type_name(type_name)
|
41
|
+
type_name = case type_name
|
42
|
+
when Symbol then type_name.to_s.classify
|
43
|
+
else type_name.to_s
|
44
|
+
end
|
45
|
+
CastingAttributes::TypeCaster.const_get(type_name)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
require File.expand_path('../casting_attributes/type_caster', __FILE__)
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: casting_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: '3.1'
|
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.1'
|
30
|
+
description: Enables using <code>casting_attr_accessor</code> to create typed non-persistent
|
31
|
+
attributes.
|
32
|
+
email: tobias.casper@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- lib/casting_attributes.rb
|
38
|
+
- lib/casting_attributes/type_caster/base.rb
|
39
|
+
- lib/casting_attributes/type_caster/float.rb
|
40
|
+
- lib/casting_attributes/type_caster/boolean.rb
|
41
|
+
- lib/casting_attributes/type_caster/fixnum.rb
|
42
|
+
- lib/casting_attributes/type_caster/string.rb
|
43
|
+
- lib/casting_attributes/type_caster.rb
|
44
|
+
homepage: http://rubygems.org/gems/casting_attributes
|
45
|
+
licenses: []
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.24
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Strictly typed non-persistent model attributes for Rails.
|
68
|
+
test_files: []
|