binding_of_callers 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c104632117ee66eed941eac6ba75ebb65734dd03
4
- data.tar.gz: 7111e3effbb414c204befba6a1d3d7dd1a771ce3
3
+ metadata.gz: 98a33f0abb9a14254ae796927900e61810aadefd
4
+ data.tar.gz: e2df4fff7ee93687741d486b6927772b4ceb2f73
5
5
  SHA512:
6
- metadata.gz: 3edc8812120b03664675aa54f2042f839df11b81aaad18a4e67583c1c67320a538691a3052f0488e1ee602bb03928c16bb83e5521f8c4d5803157f017e4cbf01
7
- data.tar.gz: 994d5a4ce6b3d2c711fb5a04043565fa6b07ef98c5554d6817d43814993d369c629514767c1f85b5f2a1794e8701be7a8bfd3ad17a0bdd80a43374d30d9802c6
6
+ metadata.gz: 7dcdc3b70560aaeb573c87f918def90c380888ce8e872e0cb63bc66ef54a2961e9636add2da9efd627f16a200fe51faea64ae272dc0944093b51b7726a349f27
7
+ data.tar.gz: a4ca52690b5abcb684923ac116f76ab00688257cbbfea1602faf02697fa310091beed1f14165b0366aee9ea4dfb0d62e4a2612c151aca809c0dac6afda52cbcc
@@ -1,6 +1,11 @@
1
1
  module BindingOfCallers
2
2
  module Reveal
3
3
 
4
+ Klass = Kernel.instance_method(:class)
5
+ InstanceVariables = Kernel.instance_method(:instance_variables)
6
+ InstanceVariableGet = Kernel.instance_method(:instance_variable_get)
7
+ InstanceVariableSet = Kernel.instance_method(:instance_variable_set)
8
+
4
9
  def _binding
5
10
  instance_variable_defined?(:@_binding) ? @_binding : self
6
11
  end
@@ -32,13 +37,15 @@ module BindingOfCallers
32
37
  end
33
38
 
34
39
  def klass
35
- @klass ||= _binding.eval(singleton_method? ? 'self' : 'self.class')
40
+ return @klass if instance_variable_defined? :@klass
41
+ determine_klass
42
+ @klass
36
43
  end
37
44
 
38
45
  def singleton_method?
39
46
  return @sm if instance_variable_defined? :@sm
40
- class_name = _binding.eval 'self.class.name'
41
- @sm = (class_name == 'Module' or class_name == 'Class')
47
+ determine_klass
48
+ @sm
42
49
  end
43
50
 
44
51
  def call_symbol
@@ -63,24 +70,57 @@ module BindingOfCallers
63
70
 
64
71
  private
65
72
 
73
+ def determine_klass
74
+ itself = binding_self
75
+ @from_object = (Object === itself)
76
+ binding_class = (@from_object ? itself.class : Klass.bind(itself).call)
77
+ class_name = binding_class.name
78
+ if class_name == 'Module' || class_name == 'Class'
79
+ @sm = true
80
+ @klass = itself
81
+ else
82
+ @sm = false
83
+ @klass = binding_class
84
+ end
85
+ end
86
+
66
87
  def all_iv
67
- _binding.eval <<-EOS
68
- instance_variables.each_with_object({}) do |iv_name, vars|
69
- vars[iv_name] = instance_variable_get(iv_name)
88
+ determine_klass unless instance_variable_defined? :@from_object
89
+ if @from_object
90
+ _binding.eval <<-EOS
91
+ instance_variables.each_with_object({}) do |iv_name, vars|
92
+ vars[iv_name] = instance_variable_get(iv_name)
93
+ end
94
+ EOS
95
+ else
96
+ itself = binding_self
97
+ _instance_variable_get = InstanceVariableGet.bind(itself)
98
+ InstanceVariables.bind(itself).call.each_with_object({}) do |iv_name, vars|
99
+ vars[iv_name] = _instance_variable_get.call iv_name
70
100
  end
71
- EOS
101
+ end
72
102
  end
73
103
 
74
104
  def the_iv name
75
- binding_self.instance_variable_get name
105
+ determine_klass unless instance_variable_defined? :@from_object
106
+ if @from_object
107
+ binding_self.instance_variable_get name
108
+ else
109
+ InstanceVariableGet.bind(binding_self).call name
110
+ end
76
111
  end
77
112
 
78
113
  def set_iv name, value
79
- binding_self.instance_variable_set name, value
114
+ determine_klass unless instance_variable_defined? :@from_object
115
+ if @from_object
116
+ binding_self.instance_variable_set name, value
117
+ else
118
+ InstanceVariableSet.bind(binding_self).call name, value
119
+ end
80
120
  end
81
121
 
82
122
  def binding_self
83
- _binding.eval "self"
123
+ _binding.eval "instance_eval('self')"
84
124
  end
85
125
 
86
126
  def all_lv
@@ -1,3 +1,3 @@
1
1
  module BindingOfCallers
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binding_of_callers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - ken
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-31 00:00:00.000000000 Z
11
+ date: 2018-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  version: '0'
123
123
  requirements: []
124
124
  rubyforge_project:
125
- rubygems_version: 2.6.8
125
+ rubygems_version: 2.2.2
126
126
  signing_key:
127
127
  specification_version: 4
128
128
  summary: a wrapper of binding_of_caller, for easy use