act_as_attribute 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/act_as_attribute/exceptions.rb +4 -0
- data/lib/act_as_attribute.rb +59 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3127a642ffd8133126e2832ff3677f01eaf48661
|
4
|
+
data.tar.gz: bdf878db77f47a22223bc997e7e420ee4221e7e3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 342b1800bac559021946fbf99828c6f9270e7dfcf3e2c45e3618243f67f8387a03255cfc488e0208a059290f3ce9eedcf13e609467798c1eb10adf9f6e59f128
|
7
|
+
data.tar.gz: 2557ff11842bf8b27b33857b73169146a1bfcc1caeebde65cf9873d75ced82b7ec77f925e28857b3f8854d84e24b2c0073478fbdffa7ddd1918cc5d2c868b618
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "act_as_attribute/exceptions"
|
2
|
+
module ActAsAttributes
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.send :extend, ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def get_constants
|
11
|
+
self::AVAILABLE_ATTRIBUTES ||= []
|
12
|
+
self::ACCEPTANCE_LEVEL ||= "error"
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_available_as_attribute?(attribute)
|
16
|
+
self.attribute_method?(attribute) ? true : false
|
17
|
+
end
|
18
|
+
|
19
|
+
def deal_with_duplicate_method(arg)
|
20
|
+
if self::ACCEPTANCE_LEVEL == "error"
|
21
|
+
raise Exceptions::AlreadyPresentAsAttribute
|
22
|
+
else
|
23
|
+
warn "Warning: Method '#{arg}' already available as attribute"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def act_as_attribute(model_name, model_attr_as_key= "name", model_attr_as_value="value")
|
28
|
+
|
29
|
+
get_constants
|
30
|
+
|
31
|
+
self::AVAILABLE_ATTRIBUTES.each do |attr|
|
32
|
+
if method_available_as_attribute?(attr)
|
33
|
+
deal_with_duplicate_method(attr)
|
34
|
+
end
|
35
|
+
|
36
|
+
define_method(attr) do
|
37
|
+
available_objects = self.send(model_name)
|
38
|
+
if available_objects.map(&model_attr_as_key.to_sym).include? attr
|
39
|
+
available_objects.send("find_by_#{model_attr_as_key}", attr).send(model_attr_as_value)
|
40
|
+
else
|
41
|
+
"method not defined"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
define_method("#{attr}=") do |value|
|
46
|
+
available_objects = self.send(model_name)
|
47
|
+
if available_objects.map(&model_attr_as_key.to_sym).include? attr
|
48
|
+
new_association= available_objects.send("find_by_#{model_attr_as_key}", attr)
|
49
|
+
new_association.update_attributes(model_attr_as_value.to_sym => value)
|
50
|
+
else
|
51
|
+
available_objects << model_name.to_s.classify.constantize.create(model_attr_as_key.to_sym => attr, model_attr_as_value.to_sym => value)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
ActiveRecord::Base.send :include, ActAsAttributes
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: act_as_attribute
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Annu Yadav
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Gem which will treat its association as its attribute. It will provide
|
14
|
+
getter, setter method for all of its association for which it is defined.
|
15
|
+
email: life.annu.yadav@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/act_as_attribute/exceptions.rb
|
21
|
+
- lib/act_as_attribute.rb
|
22
|
+
homepage: https://github.com/annuyadav/act_as_attribute
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.0.3
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Gem which will treat its association as its attribute.
|
46
|
+
test_files: []
|