metahash-rb 1.0.2 → 1.0.3

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: 1dd0495140177117ee39d5dfab630bd2b2883b81
4
- data.tar.gz: cece44198668cd884390321748306ca80b9e4764
3
+ metadata.gz: ed909cd2a13513e1c8e01ba8acfe0da1e50032c8
4
+ data.tar.gz: 6949a93b1f88c044f1fac9c6337aec8e7a0a1c41
5
5
  SHA512:
6
- metadata.gz: 19fdb4d1e1ca9ee1a2201bf8ca810f34268217ce5502053aa14958225299911c16855296b5641fdd96025b41e880d624044c223dd584c4c381f7e6e51dd5267a
7
- data.tar.gz: 68bd91fbb923d0af5d4374cefab6b046b6db2d0e2168d3ef4fe9c8e6c3694061c56fd2655652926e10d74d6fd29972e4be174a67eea74566d1d6735c754ddec7
6
+ metadata.gz: 385f4ac6de7ee6a9403fb1727257fa46fad234922abf3a7658f5334ee5fe6c023a95cb9468266ccfbe9fbee50e0e7cd56de46b2dd7f3417658cc2021481ed9da
7
+ data.tar.gz: e5c52277e77242e0ed97e2a921e436894b928a56ab5a4b88b091b080268474d77b7adb047b86cd22d76dc40776114f2be143013c66b94f2c40fe05ad27a911b2
data/README.md CHANGED
@@ -31,7 +31,7 @@ Provides a subclass of Hash and a wrapper around Rails' serialize attribute for
31
31
 
32
32
  #### In your Gemfile
33
33
 
34
- gem "metahash-rb"
34
+ gem "metahash-rb", require: "metahash"
35
35
 
36
36
  #### in your ActiveRecord model
37
37
 
@@ -1,3 +1,4 @@
1
+ require "metahash/metaclass"
1
2
  require "metahash/metadata"
2
3
  require "metahash/version"
3
4
  require 'active_record'
@@ -0,0 +1,19 @@
1
+ # http://reference.jumpingmonkey.org/programming_languages/ruby/ruby-metaprogramming.html
2
+ # allows the adding of methods to instances,
3
+ # but not the entire set of instances for a
4
+ # particular class
5
+ module Metaclass
6
+ # The hidden singleton lurks behind everyone
7
+ def metaclass; class << self; self; end; end
8
+ def meta_eval &blk; metaclass.instance_eval &blk; end
9
+
10
+ # Adds methods to a metaclass
11
+ def meta_def( name, &blk )
12
+ meta_eval { define_method name, &blk }
13
+ end
14
+
15
+ # Defines an instance method within a class
16
+ def class_def( name, &blk )
17
+ class_eval { define_method name, &blk }
18
+ end
19
+ end
@@ -20,6 +20,10 @@
20
20
  # h[:inner][:key] == "value"
21
21
  #
22
22
  class Metadata < Hash
23
+ include Metaclass
24
+ # in the event we are overriding a method, have a way to
25
+ # get back to the original
26
+ METHOD_BACKUP_KEY = "metadata_original_"
23
27
 
24
28
  # the hash being passed in will have all its subhashes converted to
25
29
  # metadata hashes.
@@ -39,9 +43,6 @@ class Metadata < Hash
39
43
  elsif hash.is_a?(Hash)
40
44
  # recursively create nested metadata objects
41
45
  hash.each do |key, value|
42
- if not valid_key?(key)
43
- raise ArgumentError.new("Not Allowed. '#{key}' is a reserved method.")
44
- end
45
46
 
46
47
  self[key] = (
47
48
  if value.is_a?(Hash)
@@ -68,18 +69,21 @@ class Metadata < Hash
68
69
  # check for assignment
69
70
  if (key = method_name.to_s).include?("=")
70
71
  key = key.chop.to_sym
71
- if not self.valid_key?(key)
72
- raise ArgumentError.new("Not Allowed. '#{key}' is a reserved method.")
73
- end
74
72
 
73
+ deepest_metadata = self
75
74
  if not @empty_nested_hashes.empty?
76
- deepest_metadata = self
77
75
  @empty_nested_hashes.each do |key|
78
76
  deepest_metadata = deepest_metadata[key] = Metadata.new
79
77
  end
80
78
  @empty_nested_hashes = []
81
79
  deepest_metadata[key] = args[0]
82
- end
80
+ # override any existing method with the key
81
+ deepest_metadata.meta_def(key){ self[key]}
82
+ else
83
+ self[key] = args[0]
84
+ # override any existing method with the key
85
+ self.meta_def(key){ args[0] }
86
+ end
83
87
  else
84
88
  value = self[method_name]
85
89
  if not value
@@ -106,9 +110,7 @@ class Metadata < Hash
106
110
  # @param [Symbol] key
107
111
  # @return [Boolean] whether or not this can be used as a hash key
108
112
  def valid_key?(key)
109
- # second parameter says that we are
110
- # looking at private methods as well
111
- not self.respond_to?(key, true)
113
+ not self.respond_to?(key)
112
114
  end
113
115
 
114
116
  # convert to regular hash, recursively
@@ -1,3 +1,3 @@
1
1
  module MetaHash
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
26
26
  s.add_development_dependency "bundler"
27
27
  s.add_development_dependency "rspec"
28
28
  s.add_development_dependency "sqlite3"
29
+ s.add_development_dependency "awesome_print"
29
30
  s.add_development_dependency "pry-byebug"
30
31
  s.add_development_dependency "codeclimate-test-reporter"
31
32
 
@@ -11,12 +11,17 @@ describe Metadata do
11
11
 
12
12
  it "removes empty hashes" do
13
13
  m.a.b.c
14
- expect(m.send :prune).to eq Metadata.new
14
+ expect(m.a.b.c).to eq Metadata.new
15
15
  end
16
16
 
17
17
  it "does not remove valid hashes" do
18
18
  m.a.b = 2
19
- expect(m.send :prune).to_not eq Metadata.new
19
+ expect(m.a.b).to_not eq Metadata.new
20
+ end
21
+
22
+ it "does not remove valid hashes of long chains" do
23
+ m.a.b.c.d.e = 2
24
+ expect(m.a.b.c.d.e).to eq 2
20
25
  end
21
26
 
22
27
  it "does not pollute itself with empty hashes" do
@@ -25,13 +30,36 @@ describe Metadata do
25
30
  end
26
31
  end
27
32
 
33
+ context "assigns values" do
34
+ let(:m){Metadata.new}
35
+
36
+ it "sets a non-exsiting deep value" do
37
+ m.a.b = 2
38
+ expect(m.a.b).to eq 2
39
+ end
40
+
41
+ it "sets with specific keys" do
42
+ m.a.min = 2
43
+ m.a.max = 3
44
+ expect(m.a.min).to eq 2
45
+ expect(m.a.max).to eq 3
46
+ end
47
+
48
+ it "overrides existing methods if set" do
49
+ m.keys = 2
50
+ expect(m.keys).to eq 2
51
+ end
52
+
53
+ it "overrides existing methods if set to non-simple objects" do
54
+ m.a.b = 2
55
+ m.a.keys = 3
56
+ expect(m.a.keys).to eq 3
57
+ end
28
58
 
29
- it "sets a non-exsiting deep value" do
30
- m = Metadata.new
31
- m.a.b = 2
32
- expect(m.a.b).to eq 2
33
59
  end
34
60
 
61
+
62
+
35
63
  it "has indifferent access" do
36
64
  m = Metadata.new(a: 2)
37
65
  expect(m.a).to eq 2
@@ -49,10 +77,4 @@ describe Metadata do
49
77
  expect(m[:outer][:inner][:k]).to eq h[:outer][:inner][:k]
50
78
  end
51
79
 
52
- it "does not allow hash keys to conflict with the name of a method of the Hash object" do
53
- expect{
54
- Metadata.new( outer: { key: "value" } )
55
- }.to raise_error
56
- end
57
-
58
80
  end
@@ -4,6 +4,7 @@ require "bundler/setup"
4
4
  require "metahash"
5
5
  require "pry-byebug" # binding.pry to debug!
6
6
  require "codeclimate-test-reporter"
7
+ require "awesome_print"
7
8
  CodeClimate::TestReporter.start
8
9
 
9
10
  ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metahash-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - L. Preston Sego III
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: awesome_print
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: pry-byebug
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +122,7 @@ files:
108
122
  - README.md
109
123
  - Rakefile
110
124
  - lib/metahash.rb
125
+ - lib/metahash/metaclass.rb
111
126
  - lib/metahash/metadata.rb
112
127
  - lib/metahash/version.rb
113
128
  - metahash.gemspec
@@ -141,7 +156,7 @@ rubyforge_project:
141
156
  rubygems_version: 2.4.1
142
157
  signing_key:
143
158
  specification_version: 4
144
- summary: MetaHash-1.0.2
159
+ summary: MetaHash-1.0.3
145
160
  test_files:
146
161
  - spec/metadata_spec.rb
147
162
  - spec/metahash_spec.rb