acts_as_dynamic_method 0.0.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -4,30 +4,31 @@ module ActsAsDynamicMethod
|
|
4
4
|
|
5
5
|
module ClassMethods
|
6
6
|
|
7
|
-
attr_accessor :
|
7
|
+
attr_accessor :json_field
|
8
8
|
|
9
9
|
def acts_as_dynamic_method(options = {})
|
10
|
-
self.
|
11
|
-
|
10
|
+
self.json_field = (options[:json_field] || :params).to_s
|
11
|
+
include ActsAsDynamicMethod::InstanceMethods
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
module InstanceMethods
|
16
16
|
def fields
|
17
|
-
|
18
|
-
|
17
|
+
field_name = self.class.json_field
|
18
|
+
field = self.method(field_name)
|
19
|
+
@fields ||= ((field.call.is_a?(String) ? JSON.parse(field.call) : field.call) || {})
|
19
20
|
end
|
20
21
|
|
21
22
|
def use(field)
|
22
23
|
fields[field]
|
23
24
|
end
|
24
25
|
|
25
|
-
def
|
26
|
+
def set(field, value)
|
26
27
|
fields unless @fields
|
27
28
|
@fields[field] = value
|
28
29
|
end
|
29
30
|
|
30
|
-
def
|
31
|
+
def json_keys
|
31
32
|
fields.keys
|
32
33
|
end
|
33
34
|
|
@@ -37,12 +38,12 @@ module ActsAsDynamicMethod
|
|
37
38
|
|
38
39
|
def method_missing(meth, *args, &block)
|
39
40
|
method_name = meth.to_s
|
40
|
-
if
|
41
|
+
if json_keys.include?(method_name)
|
41
42
|
self.use(method_name)
|
42
|
-
elsif
|
43
|
+
elsif json_keys.include?(method_name.gsub!('=', ''))
|
43
44
|
self.set(method_name, args.first)
|
44
45
|
elsif method_name.match(/\?$/)
|
45
|
-
self.
|
46
|
+
self.json_keys.include?(method_name.gsub('?', ''))
|
46
47
|
else
|
47
48
|
super
|
48
49
|
end
|
@@ -50,11 +51,11 @@ module ActsAsDynamicMethod
|
|
50
51
|
|
51
52
|
end
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
if defined?(ActiveRecord::Base)
|
55
|
+
ActiveRecord::Base.send :extend, ActsAsDynamicMethod::ClassMethods
|
56
|
+
else
|
57
|
+
Object.send :include, ActsAsDynamicMethod::ClassMethods
|
58
|
+
end
|
58
59
|
|
59
60
|
# def self.included(receiver)
|
60
61
|
# receiver.extend ClassMethods
|
@@ -3,10 +3,10 @@ require 'spec_helper'
|
|
3
3
|
describe ActsAsDynamicMethod do
|
4
4
|
|
5
5
|
class TestClass
|
6
|
-
acts_as_dynamic_method
|
6
|
+
acts_as_dynamic_method
|
7
7
|
|
8
8
|
def initialize
|
9
|
-
@params = {
|
9
|
+
@params = {field_name_1: "foo", field_name_2: "bar", field_name_3: 1234, field_name_4: true}.to_json
|
10
10
|
end
|
11
11
|
|
12
12
|
def params
|
@@ -14,14 +14,28 @@ describe ActsAsDynamicMethod do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
class DummyClass
|
18
|
+
acts_as_dynamic_method json_field: :attributes
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
@attributes = {field_name_1: "foo"}.to_json
|
22
|
+
end
|
23
|
+
|
24
|
+
def attributes
|
25
|
+
@attributes
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
17
29
|
let(:base) { TestClass.new }
|
30
|
+
let(:dummy) { DummyClass.new }
|
18
31
|
|
19
32
|
it 'should create method from json key' do
|
20
|
-
base.
|
33
|
+
base.field_name_1.should eq('foo')
|
34
|
+
dummy.field_name_1.should eq('foo')
|
21
35
|
end
|
22
36
|
|
23
37
|
it 'should create method from json key' do
|
24
|
-
base.
|
38
|
+
base.field_name_4.should be_true
|
25
39
|
end
|
26
40
|
|
27
41
|
it "should handle method_mithing" do
|
@@ -30,6 +44,17 @@ describe ActsAsDynamicMethod do
|
|
30
44
|
|
31
45
|
it "should handle to_json method" do
|
32
46
|
base.to_json.should eq(base.params)
|
47
|
+
dummy.to_json.should eq(dummy.attributes)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should be possible to update json values' do
|
51
|
+
base.field_name_1 = 'new_value'
|
52
|
+
base.field_name_1.should == 'new_value'
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should be possible to update json values' do
|
56
|
+
base.field_name_1 = 'new_value'
|
57
|
+
base.to_json.should =~ /new_value/
|
33
58
|
end
|
34
59
|
|
35
60
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_dynamic_method
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|