virtual_attributes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ pkg
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,33 @@
1
+ # VirtualAttributes
2
+
3
+ * https://github.com/daemon/virtual_attributes
4
+
5
+ ## DESCRIPTION
6
+
7
+ Usually you can use `attr_accessor` to define virtual attribute.
8
+ You could even use `attr_accessor_with_default` to set default value.
9
+
10
+ But in any case you are unable to typecast values.
11
+
12
+ Let's assume you receive data from form submit.
13
+
14
+ > params[:user].inspect
15
+ => {'enabled' => 'true', 'priority' => '0.5'}
16
+
17
+ Having attributes defined as table columns values would be actual `true` and `0.5`.
18
+
19
+ But with `attr_accessor` fields will still be strings.
20
+
21
+ This is what `virtual_attribute` handles.
22
+
23
+ ## Origins
24
+
25
+ Inspired with virtual_attribute gem by Philip Roberts I tried to use ActiveRecord to typecast.
26
+
27
+ ## Usage
28
+
29
+ class User < ActiveRecord::Base
30
+ virtual_attribute :enabled, :type => :boolean, :default => false
31
+ virtual_attribute :priority, :type => :float, :default => 1.0
32
+ end
33
+
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ begin
2
+ require "bundler"
3
+ Bundler.setup
4
+ rescue LoadError
5
+ $stderr.puts "You need to have Bundler installed to be able build this gem."
6
+ end
7
+
8
+ gemspec = eval(File.read(Dir["*.gemspec"].first))
9
+
10
+
11
+ desc "Validate the gemspec"
12
+ task :gemspec do
13
+ gemspec.validate
14
+ end
15
+
16
+ desc "Build gem locally"
17
+ task :build => :gemspec do
18
+ system "gem build #{gemspec.name}.gemspec"
19
+ FileUtils.mkdir_p "pkg"
20
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", "pkg"
21
+ end
22
+
23
+ desc "Install gem locally"
24
+ task :install => :build do
25
+ system "gem install pkg/#{gemspec.name}-#{gemspec.version}"
26
+ end
27
+
28
+ desc "Clean automatically generated files"
29
+ task :clean do
30
+ FileUtils.rm_rf "pkg"
31
+ end
@@ -0,0 +1,30 @@
1
+ require 'active_record/base'
2
+ require 'active_record/connection_adapters/abstract/schema_definitions'
3
+
4
+ module VirtualAttributes
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def virtual_attribute(name, options)
11
+ name_equals = "#{name.to_s}="
12
+ inst_var = "@#{name.to_s}"
13
+
14
+ define_method(name_equals) do |value|
15
+ attr = ActiveRecord::ConnectionAdapters::Column.new(name, options[:default], options[:type].to_s)
16
+ instance_variable_set inst_var, (attr.type_cast(value) || attr.default)
17
+ end
18
+
19
+ define_method(name) do
20
+ if instance_variable_defined?(inst_var)
21
+ instance_variable_get(inst_var)
22
+ else
23
+ instance_variable_set inst_var, ActiveRecord::ConnectionAdapters::Column.new(name, options[:default], options[:type].to_s).default
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ ActiveRecord::Base.send(:include, VirtualAttributes)
File without changes
@@ -0,0 +1,30 @@
1
+ # coding: UTF-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "virtual_attributes"
5
+ s.version = "0.0.1"
6
+ s.platform = Gem::Platform::RUBY
7
+ s.authors = ["Dmitry Shaposhnik"]
8
+ s.email = ["dmitry@shaposhnik.name"]
9
+ s.homepage = "http://github.com/daemon/virtual_attributes"
10
+ s.summary = "Virtual attributes helper for models in Rails3"
11
+ s.description = ""
12
+
13
+ s.required_rubygems_version = ">= 1.3.6"
14
+
15
+ # If you have runtime dependencies, add them here
16
+ # s.add_runtime_dependency "other", "~> 1.2"
17
+
18
+ # If you have development dependencies, add them here
19
+ # s.add_development_dependency "another", "= 0.9"
20
+
21
+ # The list of files to be contained in the gem
22
+ s.files = `git ls-files`.split("\n")
23
+ # s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
24
+ # s.extensions = `git ls-files ext/extconf.rb`.split("\n")
25
+
26
+ s.require_path = 'lib'
27
+
28
+ # For C extensions
29
+ # s.extensions = "ext/extconf.rb"
30
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: virtual_attributes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Dmitry Shaposhnik
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-04 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: ""
23
+ email:
24
+ - dmitry@shaposhnik.name
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.markdown
35
+ - Rakefile
36
+ - lib/virtual_attributes.rb
37
+ - test/test_helper.rb
38
+ - virtual_attributes.gemspec
39
+ has_rdoc: true
40
+ homepage: http://github.com/daemon/virtual_attributes
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 23
63
+ segments:
64
+ - 1
65
+ - 3
66
+ - 6
67
+ version: 1.3.6
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.5.3
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Virtual attributes helper for models in Rails3
75
+ test_files: []
76
+