has_dynamic_attribute 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/has_dynamic_attribute.gemspec +59 -0
- data/lib/has_dynamic_attribute.rb +5 -0
- data/lib/hda/ar_accessors.rb +89 -0
- data/lib/hda/av_form_helpers.rb +23 -0
- data/test/helper.rb +10 -0
- data/test/test_has_dynamic_attribute.rb +7 -0
- metadata +102 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 tim.teng
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= has_dynamic_attribute
|
2
|
+
|
3
|
+
Guide will come soon...
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 tim.teng. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "has_dynamic_attribute"
|
8
|
+
gem.summary = %Q{build dynamic attributes at instance level}
|
9
|
+
gem.description = %Q{build dynamic attributes at instance level, saved in database with json format}
|
10
|
+
gem.email = "tim.rubist@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/tteng/has_dynamic_attribute"
|
12
|
+
gem.authors = ["tim.teng"]
|
13
|
+
gem.add_development_dependency "json", ">= 1.4.6"
|
14
|
+
gem.add_development_dependency "json_pure", ">= 1.2.2"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/test_*.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
task :test => :check_dependencies
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "has_dynamic_attribute #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{has_dynamic_attribute}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["tim.teng"]
|
12
|
+
s.date = %q{2010-08-25}
|
13
|
+
s.description = %q{build dynamic attributes at instance level, saved in database with json format}
|
14
|
+
s.email = %q{tim.rubist@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"has_dynamic_attribute.gemspec",
|
27
|
+
"lib/has_dynamic_attribute.rb",
|
28
|
+
"lib/hda/ar_accessors.rb",
|
29
|
+
"lib/hda/av_form_helpers.rb",
|
30
|
+
"test/helper.rb",
|
31
|
+
"test/test_has_dynamic_attribute.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/tteng/has_dynamic_attribute}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.6}
|
37
|
+
s.summary = %q{build dynamic attributes at instance level}
|
38
|
+
s.test_files = [
|
39
|
+
"test/test_has_dynamic_attribute.rb",
|
40
|
+
"test/helper.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_development_dependency(%q<json>, [">= 1.4.6"])
|
49
|
+
s.add_development_dependency(%q<json_pure>, [">= 1.2.2"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<json>, [">= 1.4.6"])
|
52
|
+
s.add_dependency(%q<json_pure>, [">= 1.2.2"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<json>, [">= 1.4.6"])
|
56
|
+
s.add_dependency(%q<json_pure>, [">= 1.2.2"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,89 @@
|
|
1
|
+
#reader,writer for dynatic attributes
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
module HdaArAccessor
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def has_dynamic_attribute attr
|
13
|
+
instance_eval <<-EOF
|
14
|
+
alias origin_find find
|
15
|
+
def find *args
|
16
|
+
obj = origin_find(args.shift)
|
17
|
+
unless obj.blank?
|
18
|
+
case obj
|
19
|
+
when Array
|
20
|
+
obj.map{|o| o.map_json_text_to_struct!}
|
21
|
+
else
|
22
|
+
obj.map_json_text_to_struct!
|
23
|
+
end
|
24
|
+
end
|
25
|
+
obj
|
26
|
+
end
|
27
|
+
EOF
|
28
|
+
include HdaArAccessor::InstanceMethods
|
29
|
+
write_inheritable_attribute :dynamic_attr, attr
|
30
|
+
class_inheritable_reader :dynamic_attr
|
31
|
+
after_validation :map_struct_to_json_text!
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
module InstanceMethods
|
37
|
+
|
38
|
+
attr_accessor :dy_attrs
|
39
|
+
|
40
|
+
def method_missing mth, *args
|
41
|
+
if mth.to_s =~ /^da_(.*)=$/
|
42
|
+
return @dy_attrs.send("#{$1}=", args.shift)
|
43
|
+
elsif mth.to_s =~ /^da_(.*)$/
|
44
|
+
return @dy_attrs.send($1)
|
45
|
+
end
|
46
|
+
super
|
47
|
+
end
|
48
|
+
|
49
|
+
def initialize args={}
|
50
|
+
super args.merge(:dy_attrs => OpenStruct.new)
|
51
|
+
end
|
52
|
+
|
53
|
+
def map_json_text_to_struct!
|
54
|
+
self.dy_attrs = OpenStruct.new(JSON.parse(self.send(dynamic_attr) || "{}"))
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
def dy_attributes= attrs
|
59
|
+
if attrs.blank?
|
60
|
+
self.dy_attrs = OpenStruct.new({})
|
61
|
+
else
|
62
|
+
deleted_attrs = dy_attrs_hash.keys - attrs.map{|hsh| hsh[:da_label]}
|
63
|
+
deleted_attrs.each do |attr|
|
64
|
+
self.send("da_#{attr}=",nil)
|
65
|
+
end
|
66
|
+
attrs.each do |hash|
|
67
|
+
dy_attr, value = hash[:da_label], hash[:da_value]
|
68
|
+
next if (dy_attr.blank? || value.blank?)
|
69
|
+
self.send("da_#{dy_attr}=", value)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def dy_attrs_hash
|
75
|
+
@dy_attrs.as_json.values.shift rescue {}
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def map_struct_to_json_text!
|
81
|
+
value = dy_attrs_hash
|
82
|
+
value.delete_if{|k,v|v.blank?}
|
83
|
+
self.send "#{dynamic_attr}=", value.to_json
|
84
|
+
self
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ActionView
|
2
|
+
|
3
|
+
module Helpers
|
4
|
+
|
5
|
+
module FormHelper
|
6
|
+
|
7
|
+
def add_dy_attrs_link link_name, obj, options={}
|
8
|
+
default_options = { :container => 'dy_attrs', :partial => 'dy_attrs'}
|
9
|
+
options = default_options.merge(options)
|
10
|
+
link_to_function link_name do |page|
|
11
|
+
page.insert_html :bottom, options[:container], :partial => options[:partial], :locals => {:obj => obj}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def dynamic_attrs_for obj, &block
|
16
|
+
fields_for("dy_attributes[]", obj, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_dynamic_attribute
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- tim.teng
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-25 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: json
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 4
|
30
|
+
- 6
|
31
|
+
version: 1.4.6
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: json_pure
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 1
|
43
|
+
- 2
|
44
|
+
- 2
|
45
|
+
version: 1.2.2
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
description: build dynamic attributes at instance level, saved in database with json format
|
49
|
+
email: tim.rubist@gmail.com
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- LICENSE
|
56
|
+
- README.rdoc
|
57
|
+
files:
|
58
|
+
- .document
|
59
|
+
- .gitignore
|
60
|
+
- LICENSE
|
61
|
+
- README.rdoc
|
62
|
+
- Rakefile
|
63
|
+
- VERSION
|
64
|
+
- has_dynamic_attribute.gemspec
|
65
|
+
- lib/has_dynamic_attribute.rb
|
66
|
+
- lib/hda/ar_accessors.rb
|
67
|
+
- lib/hda/av_form_helpers.rb
|
68
|
+
- test/helper.rb
|
69
|
+
- test/test_has_dynamic_attribute.rb
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://github.com/tteng/has_dynamic_attribute
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options:
|
76
|
+
- --charset=UTF-8
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 1.3.6
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: build dynamic attributes at instance level
|
100
|
+
test_files:
|
101
|
+
- test/test_has_dynamic_attribute.rb
|
102
|
+
- test/helper.rb
|