meta_methods 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/.idea/meta_methods.iml +96 -21
- data/.idea/workspace.xml +348 -164
- data/.ruby-version +1 -1
- data/CHANGES +5 -1
- data/lib/meta_methods/core.rb +50 -77
- data/lib/meta_methods/version.rb +1 -1
- data/spec/meta_methods_spec.rb +31 -6
- metadata +22 -22
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
2.1.4
|
data/CHANGES
CHANGED
data/lib/meta_methods/core.rb
CHANGED
@@ -1,101 +1,74 @@
|
|
1
|
-
module MetaMethods
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
# end
|
6
|
-
object.singleton_class
|
7
|
-
end
|
1
|
+
module MetaMethods
|
2
|
+
module Core
|
3
|
+
def define_attribute(object, key, value)
|
4
|
+
object.singleton_class.send :attr_accessor, key.to_sym # creates accessor
|
8
5
|
|
9
|
-
#
|
10
|
-
|
11
|
-
# define_attribute(type, object, key, value, create_instance)
|
12
|
-
# end
|
13
|
-
# end
|
14
|
-
#
|
15
|
-
# def define_attribute(type, object, key, value, create_instance=true)
|
16
|
-
# if create_instance
|
17
|
-
# metaclass(object).send type, key.to_sym
|
18
|
-
#
|
19
|
-
# object.instance_variable_set("@#{key}".to_sym, value)
|
20
|
-
# else
|
21
|
-
# metaclass(object).class_eval <<-CODE
|
22
|
-
# def #{key}
|
23
|
-
# "#{value}"
|
24
|
-
# end
|
25
|
-
# CODE
|
26
|
-
# end
|
27
|
-
# end
|
28
|
-
|
29
|
-
def define_attribute(object, key, value)
|
30
|
-
object.singleton_class.send :attr_accessor, key.to_sym # creates accessor
|
31
|
-
|
32
|
-
object.send "#{key}=".to_sym, value # sets up value for attribute
|
33
|
-
end
|
6
|
+
object.send "#{key}=".to_sym, value # sets up value for attribute
|
7
|
+
end
|
34
8
|
|
35
|
-
|
36
|
-
|
37
|
-
|
9
|
+
def define_attributes(object, hash)
|
10
|
+
hash.each_pair do |key, value|
|
11
|
+
define_attribute(object, key, value)
|
12
|
+
end
|
38
13
|
end
|
39
|
-
end
|
40
14
|
|
41
|
-
|
42
|
-
|
15
|
+
def locals_to_hash object, content
|
16
|
+
scope = object.instance_eval { binding }
|
43
17
|
|
44
|
-
|
18
|
+
eval(content, scope)
|
45
19
|
|
46
|
-
|
47
|
-
|
20
|
+
extract_values(defined_vars(scope), scope)
|
21
|
+
end
|
48
22
|
|
49
|
-
|
23
|
+
def evaluate_dsl(create_block, destroy_block, execute_block)
|
24
|
+
begin
|
25
|
+
created_object = create_block.kind_of?(Proc) ? create_block.call : create_block
|
50
26
|
|
51
|
-
|
52
|
-
created_object = create_block.kind_of?(Proc) ? create_block.call : create_block
|
27
|
+
created_object.instance_variable_set(:@parent, block_parent(execute_block))
|
53
28
|
|
54
|
-
|
29
|
+
def created_object.method_missing(sym, *args, &block)
|
30
|
+
@parent.send sym, *args, &block
|
31
|
+
end
|
55
32
|
|
56
|
-
|
57
|
-
|
58
|
-
|
33
|
+
def created_object.respond_to?(sym, include_private = false)
|
34
|
+
@parent.respond_to? sym, include_private
|
35
|
+
end
|
59
36
|
|
60
|
-
|
61
|
-
|
37
|
+
created_object.instance_eval(&execute_block)
|
38
|
+
ensure
|
39
|
+
destroy_block.call(created_object) if destroy_block && created_object
|
62
40
|
end
|
63
|
-
|
64
|
-
created_object.instance_eval(&execute_block)
|
65
|
-
ensure
|
66
|
-
destroy_block.call(created_object) if destroy_block && created_object
|
67
41
|
end
|
68
|
-
end
|
69
42
|
|
70
|
-
|
71
|
-
|
43
|
+
def block_parent block
|
44
|
+
block_binding = block.binding
|
72
45
|
|
73
|
-
|
74
|
-
|
46
|
+
block_binding.eval 'self'
|
47
|
+
end
|
75
48
|
|
76
|
-
|
49
|
+
private
|
77
50
|
|
78
|
-
|
79
|
-
|
80
|
-
|
51
|
+
def defined_vars scope
|
52
|
+
eval("local_variables", scope) - Kernel.local_variables - %w(content)
|
53
|
+
end
|
81
54
|
|
82
|
-
|
83
|
-
|
55
|
+
def extract_values vars_list, scope
|
56
|
+
vars_values_list = vars_list.collect { |name| [name, get_property(name, scope)] }
|
84
57
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
58
|
+
hash = {}
|
59
|
+
vars_values_list.each do |var, value|
|
60
|
+
hash[var.to_sym] = value
|
61
|
+
end
|
89
62
|
|
90
|
-
|
91
|
-
|
63
|
+
hash
|
64
|
+
end
|
92
65
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
66
|
+
def get_property name, scope
|
67
|
+
begin
|
68
|
+
eval(name.to_s, scope)
|
69
|
+
rescue NameError
|
70
|
+
nil
|
71
|
+
end
|
98
72
|
end
|
99
73
|
end
|
100
|
-
|
101
74
|
end
|
data/lib/meta_methods/version.rb
CHANGED
data/spec/meta_methods_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'meta_methods'
|
1
|
+
require 'meta_methods/core'
|
2
2
|
|
3
3
|
class MetaMethodsDemo
|
4
4
|
include MetaMethods::Core
|
@@ -19,9 +19,9 @@ describe MetaMethodsDemo do
|
|
19
19
|
it "creates new attributes on object" do
|
20
20
|
object = Object.new
|
21
21
|
|
22
|
-
subject.define_attributes
|
22
|
+
subject.define_attributes(object,
|
23
23
|
{new_attribute1: "new_attribute_value1",
|
24
|
-
new_attribute2: "new_attribute_value2" }
|
24
|
+
new_attribute2: "new_attribute_value2" })
|
25
25
|
|
26
26
|
expect(object.new_attribute1).to eq("new_attribute_value1")
|
27
27
|
expect(object.new_attribute2).to eq("new_attribute_value2")
|
@@ -34,9 +34,34 @@ describe MetaMethodsDemo do
|
|
34
34
|
|
35
35
|
hash = subject.locals_to_hash subject, s
|
36
36
|
|
37
|
-
hash[:a].
|
38
|
-
hash[:b].
|
39
|
-
hash[:c].
|
37
|
+
expect(hash[:a]).to eq 1
|
38
|
+
expect(hash[:b]).to eq 2
|
39
|
+
expect(hash[:c]).to be_nil
|
40
40
|
end
|
41
41
|
end
|
42
|
+
|
43
|
+
describe "#evaluate_dsl" do
|
44
|
+
it "calls all lambdas" do
|
45
|
+
create_block = lambda { created }
|
46
|
+
destroy_block = lambda {|object| p destroyed(object) }
|
47
|
+
|
48
|
+
execute_block = lambda {|object| p executed(object) }
|
49
|
+
|
50
|
+
subject.evaluate_dsl(create_block, destroy_block, execute_block)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def created
|
57
|
+
p "created"
|
58
|
+
end
|
59
|
+
|
60
|
+
def destroyed object
|
61
|
+
p "destroyed: #{object}"
|
62
|
+
end
|
63
|
+
|
64
|
+
def executed object
|
65
|
+
p "executed: #{object}"
|
66
|
+
end
|
42
67
|
end
|
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: meta_methods
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Shvets
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03
|
11
|
+
date: 2014-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gemspec_deps_gen
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: gemcutter
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
description: Collection of methods for easing meta-programming
|
@@ -44,19 +44,19 @@ executables: []
|
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
-
- .gitignore
|
48
|
-
- .idea/.name
|
49
|
-
- .idea/.rakeTasks
|
50
|
-
- .idea/cssxfire.xml
|
51
|
-
- .idea/encodings.xml
|
52
|
-
- .idea/meta_methods.iml
|
53
|
-
- .idea/misc.xml
|
54
|
-
- .idea/modules.xml
|
55
|
-
- .idea/scopes/scope_settings.xml
|
56
|
-
- .idea/vcs.xml
|
57
|
-
- .idea/workspace.xml
|
58
|
-
- .ruby-gemset
|
59
|
-
- .ruby-version
|
47
|
+
- ".gitignore"
|
48
|
+
- ".idea/.name"
|
49
|
+
- ".idea/.rakeTasks"
|
50
|
+
- ".idea/cssxfire.xml"
|
51
|
+
- ".idea/encodings.xml"
|
52
|
+
- ".idea/meta_methods.iml"
|
53
|
+
- ".idea/misc.xml"
|
54
|
+
- ".idea/modules.xml"
|
55
|
+
- ".idea/scopes/scope_settings.xml"
|
56
|
+
- ".idea/vcs.xml"
|
57
|
+
- ".idea/workspace.xml"
|
58
|
+
- ".ruby-gemset"
|
59
|
+
- ".ruby-version"
|
60
60
|
- CHANGES
|
61
61
|
- Gemfile
|
62
62
|
- LICENSE
|
@@ -77,17 +77,17 @@ require_paths:
|
|
77
77
|
- lib
|
78
78
|
required_ruby_version: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
84
|
requirements:
|
85
|
-
- -
|
85
|
+
- - ">="
|
86
86
|
- !ruby/object:Gem::Version
|
87
87
|
version: '0'
|
88
88
|
requirements: []
|
89
89
|
rubyforge_project:
|
90
|
-
rubygems_version: 2.
|
90
|
+
rubygems_version: 2.4.2
|
91
91
|
signing_key:
|
92
92
|
specification_version: 4
|
93
93
|
summary: Collection of methods for easing meta-programming
|