neo4j-core 0.0.11-java → 0.0.12-java

Sign up to get free protection for your applications and to get access to all the features.
data/lib/neo4j-core.rb CHANGED
@@ -60,6 +60,7 @@ require 'neo4j-core/traversal/traverser'
60
60
 
61
61
  require 'neo4j-core/cypher/cypher'
62
62
  require 'neo4j-core/cypher/result_wrapper'
63
+ require 'neo4j-core/hash_with_indifferent_access'
63
64
 
64
65
  require 'neo4j/algo'
65
66
  require 'neo4j/cypher'
@@ -0,0 +1,165 @@
1
+ module Neo4j
2
+ module Core
3
+ # Stolen from http://as.rubyonrails.org/classes/HashWithIndifferentAccess.html
4
+ # We don't want to depend on active support
5
+ class HashWithIndifferentAccess < Hash
6
+
7
+ # Always returns true, so that <tt>Array#extract_options!</tt> finds members of this class.
8
+ def extractable_options?
9
+ true
10
+ end
11
+
12
+ def with_indifferent_access
13
+ dup
14
+ end
15
+
16
+ def nested_under_indifferent_access
17
+ self
18
+ end
19
+
20
+ def initialize(constructor = {})
21
+ if constructor.is_a?(Hash)
22
+ super()
23
+ update(constructor)
24
+ else
25
+ super(constructor)
26
+ end
27
+ end
28
+
29
+ def default(key = nil)
30
+ if key.is_a?(Symbol) && include?(key = key.to_s)
31
+ self[key]
32
+ else
33
+ super
34
+ end
35
+ end
36
+
37
+ def self.new_from_hash_copying_default(hash)
38
+ new(hash).tap do |new_hash|
39
+ new_hash.default = hash.default
40
+ end
41
+ end
42
+
43
+ alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
44
+ alias_method :regular_update, :update unless method_defined?(:regular_update)
45
+
46
+ # Assigns a new value to the hash:
47
+ #
48
+ # hash = HashWithIndifferentAccess.new
49
+ # hash[:key] = "value"
50
+ #
51
+ def []=(key, value)
52
+ regular_writer(convert_key(key), convert_value(value))
53
+ end
54
+
55
+ alias_method :store, :[]=
56
+
57
+ # Updates the instantized hash with values from the second:
58
+ #
59
+ # hash_1 = HashWithIndifferentAccess.new
60
+ # hash_1[:key] = "value"
61
+ #
62
+ # hash_2 = HashWithIndifferentAccess.new
63
+ # hash_2[:key] = "New Value!"
64
+ #
65
+ # hash_1.update(hash_2) # => {"key"=>"New Value!"}
66
+ #
67
+ def update(other_hash)
68
+ if other_hash.is_a? HashWithIndifferentAccess
69
+ super(other_hash)
70
+ else
71
+ other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
72
+ self
73
+ end
74
+ end
75
+
76
+ alias_method :merge!, :update
77
+
78
+ # Checks the hash for a key matching the argument passed in:
79
+ #
80
+ # hash = HashWithIndifferentAccess.new
81
+ # hash["key"] = "value"
82
+ # hash.key? :key # => true
83
+ # hash.key? "key" # => true
84
+ #
85
+ def key?(key)
86
+ super(convert_key(key))
87
+ end
88
+
89
+ alias_method :include?, :key?
90
+ alias_method :has_key?, :key?
91
+ alias_method :member?, :key?
92
+
93
+ # Fetches the value for the specified key, same as doing hash[key]
94
+ def fetch(key, *extras)
95
+ super(convert_key(key), *extras)
96
+ end
97
+
98
+ # Returns an array of the values at the specified indices:
99
+ #
100
+ # hash = HashWithIndifferentAccess.new
101
+ # hash[:a] = "x"
102
+ # hash[:b] = "y"
103
+ # hash.values_at("a", "b") # => ["x", "y"]
104
+ #
105
+ def values_at(*indices)
106
+ indices.collect {|key| self[convert_key(key)]}
107
+ end
108
+
109
+ # Returns an exact copy of the hash.
110
+ def dup
111
+ self.class.new(self).tap do |new_hash|
112
+ new_hash.default = default
113
+ end
114
+ end
115
+
116
+ # Merges the instantized and the specified hashes together, giving precedence to the values from the second hash.
117
+ # Does not overwrite the existing hash.
118
+ def merge(hash)
119
+ self.dup.update(hash)
120
+ end
121
+
122
+ # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second.
123
+ # This overloaded definition prevents returning a regular hash, if reverse_merge is called on a <tt>HashWithDifferentAccess</tt>.
124
+ def reverse_merge(other_hash)
125
+ super self.class.new_from_hash_copying_default(other_hash)
126
+ end
127
+
128
+ def reverse_merge!(other_hash)
129
+ replace(reverse_merge( other_hash ))
130
+ end
131
+
132
+ # Removes a specified key from the hash.
133
+ def delete(key)
134
+ super(convert_key(key))
135
+ end
136
+
137
+ def stringify_keys!; self end
138
+ def stringify_keys; dup end
139
+ # undef :symbolize_keys!
140
+ def symbolize_keys; to_hash.symbolize_keys end
141
+ def to_options!; self end
142
+
143
+ # Convert to a Hash with String keys.
144
+ def to_hash
145
+ Hash.new(default).merge!(self)
146
+ end
147
+
148
+ protected
149
+ def convert_key(key)
150
+ key.kind_of?(Symbol) ? key.to_s : key
151
+ end
152
+
153
+ def convert_value(value)
154
+ if value.is_a? Hash
155
+ value #.nested_under_indifferent_access
156
+ elsif value.is_a?(Array)
157
+ value.dup.replace(value.map { |e| convert_value(e) })
158
+ else
159
+ value
160
+ end
161
+ end
162
+ end
163
+
164
+ end
165
+ end
@@ -1,5 +1,5 @@
1
1
  module Neo4j
2
2
  module Core
3
- VERSION = "0.0.11"
3
+ VERSION = "0.0.12"
4
4
  end
5
5
  end
data/lib/neo4j/config.rb CHANGED
@@ -37,7 +37,7 @@ module Neo4j
37
37
  # @return [Hash] the default file loaded by yaml
38
38
  def defaults
39
39
  require 'yaml'
40
- @defaults ||= YAML.load_file(default_file)
40
+ @defaults ||= Neo4j::Core::HashWithIndifferentAccess.new(YAML.load_file(default_file))
41
41
  end
42
42
 
43
43
  # @return [String] the expanded path of the Config[:storage_path] property
@@ -56,7 +56,7 @@ module Neo4j
56
56
  # @yield config
57
57
  # @yieldparam [Neo4j::Config] config - this configuration class
58
58
  def use
59
- @configuration ||= {}
59
+ @configuration ||= Neo4j::Core::HashWithIndifferentAccess.new
60
60
  yield @configuration
61
61
  nil
62
62
  end
@@ -125,7 +125,7 @@ module Neo4j
125
125
 
126
126
  # @return The a new configuration using default values as a hash.
127
127
  def setup()
128
- @configuration = {}
128
+ @configuration = Neo4j::Core::HashWithIndifferentAccess.new
129
129
  @configuration.merge!(defaults)
130
130
  @configuration
131
131
  end
data/lib/neo4j/neo4j.rb CHANGED
@@ -71,9 +71,10 @@ module Neo4j
71
71
  db
72
72
  end
73
73
 
74
- # @return [String, nil] the current storage path of a running neo4j database. If the database is not running it returns nil.
74
+ # If the database is not running it returns Neo4j::Config.storage_path otherwise it asks the database
75
+ # @return [String] the current storage path of the database
75
76
  def storage_path
76
- return nil unless db.running?
77
+ return Neo4j::Config.storage_path unless db.running?
77
78
  db.storage_path
78
79
  end
79
80
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: neo4j-core
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.11
5
+ version: 0.0.12
6
6
  platform: java
7
7
  authors:
8
8
  - Andreas Ronge
@@ -48,6 +48,7 @@ files:
48
48
  - lib/neo4j/relationship.rb
49
49
  - lib/neo4j-core/event_handler.rb
50
50
  - lib/neo4j-core/lazy_map.rb
51
+ - lib/neo4j-core/hash_with_indifferent_access.rb
51
52
  - lib/neo4j-core/database.rb
52
53
  - lib/neo4j-core/relationship_set.rb
53
54
  - lib/neo4j-core/version.rb