acts_as_dynamic_method 0.0.1 → 0.0.2

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.
@@ -16,4 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rspec"
19
21
  end
@@ -1,5 +1,59 @@
1
1
  require "acts_as_dynamic_method/version"
2
2
 
3
3
  module ActsAsDynamicMethod
4
- # Your code goes here...
5
- end
4
+
5
+ module ClassMethods
6
+
7
+ attr_accessor :dynamic_method_text_field
8
+
9
+ def acts_as_dynamic_method(options = {})
10
+ self.dynamic_method_text_field = (options[:dynamic_method_text_field] || :params).to_s
11
+ include ActsAsDynamicMethod::InstanceMethods
12
+ end
13
+ end
14
+
15
+ module InstanceMethods
16
+ def fields
17
+ field = self.class.dynamic_method_text_field
18
+ @fields ||= ((field.is_a?(String) ? JSON.parse(self.send(field)) : self.send(field)) || {})
19
+ end
20
+
21
+ def use(field)
22
+ fields[field]
23
+ end
24
+
25
+ def []=(field, value)
26
+ fields unless @fields
27
+ @fields[field] = value
28
+ end
29
+
30
+ def keys
31
+ fields.keys
32
+ end
33
+
34
+ def to_json
35
+ fields.to_json
36
+ end
37
+
38
+ def method_missing(meth, *args, &block)
39
+ method_name = meth.to_s
40
+ if keys.include?(method_name)
41
+ self.use(method_name)
42
+ elsif keys.include?(method_name.gsub!('=', ''))
43
+ self.set(method_name, args.first)
44
+ elsif method_name.match(/\?$/)
45
+ self.keys.include?(method_name.gsub('?', ''))
46
+ else
47
+ super
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ if defined?(ActiveRecord::Base)
54
+ ActiveRecord::Base.send :include, ActsAsDynamicMethod::ClassMethods
55
+ else
56
+ Object.send :include, ActsAsDynamicMethod::ClassMethods
57
+ end
58
+
59
+ end
@@ -1,3 +1,3 @@
1
1
  module ActsAsDynamicMethod
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsDynamicMethod do
4
+
5
+ class TestClass
6
+ acts_as_dynamic_method dynamic_method_text_field: :params
7
+
8
+ def initialize
9
+ @params = {location_of_role: "location_of_role_value", job_status: "job_status_value", degree_1: "degree_1_value", collaboration: 'true'}.to_json
10
+ end
11
+
12
+ def params
13
+ @params
14
+ end
15
+ end
16
+
17
+ let(:base) { TestClass.new }
18
+
19
+ it 'should create method from json key' do
20
+ base.location_of_role.should eq('location_of_role_value')
21
+ end
22
+
23
+ it 'should create method from json key' do
24
+ base.collaboration.should be_true
25
+ end
26
+
27
+ it "should handle method_mithing" do
28
+ expect { base.aaa }.to raise_error
29
+ end
30
+
31
+ it "should handle to_json method" do
32
+ base.to_json.should eq(base.params)
33
+ end
34
+
35
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+ require 'bundler/setup'
4
+
5
+ require 'acts_as_dynamic_method' # and any other gems you need
6
+
7
+ RSpec.configure do |config|
8
+ # some (optional) config here
9
+ 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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,23 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2013-07-16 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description: create methods dynamicly from json string
15
31
  email:
16
32
  - oleksiy.zaytsew@gmail.com
@@ -26,6 +42,8 @@ files:
26
42
  - acts_as_dynamic_method.gemspec
27
43
  - lib/acts_as_dynamic_method.rb
28
44
  - lib/acts_as_dynamic_method/version.rb
45
+ - spec/acts_as_dynmic_method_spec.rb
46
+ - spec/spec_helper.rb
29
47
  homepage: ''
30
48
  licenses: []
31
49
  post_install_message:
@@ -50,4 +68,6 @@ rubygems_version: 1.8.24
50
68
  signing_key:
51
69
  specification_version: 3
52
70
  summary: create methods dynamicly from json string
53
- test_files: []
71
+ test_files:
72
+ - spec/acts_as_dynmic_method_spec.rb
73
+ - spec/spec_helper.rb