hoodie 0.4.9 → 0.5.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 78f652d3c64f70002f3a3b39648d94b5552e8802
4
- data.tar.gz: 8639bdbefbb53ecae2be41440b6b3e2ed741e746
3
+ metadata.gz: 4842b7b808042920de7b468027893f088ccff952
4
+ data.tar.gz: 8fc1fcda8d2a8d862b636d0c7976105d6e7150ed
5
5
  SHA512:
6
- metadata.gz: 7a665927fecac4819fddbf2e848f4cbd07742f3a02dcff18dd3f2a6c82ff66e550bfafa6b8cb345386925f8a903be3417c3bd7d5dd76a412a10b472afee77a45
7
- data.tar.gz: d5d5c2293c27aadefceffdfde01ad1bb5e6a327150b2998fbd11965d182edb3b599303ab3f5403293183ed782490442bdb6aaae463d00b912746eb33f2bfd9bd
6
+ metadata.gz: 87a36667e8746591425c1381ea0157de2d8fde634aad320b61b9ea2e05311d247f8401d4218d5058f5d1e7fac8540eec42c0d40b85dcf7af1609e133fc74c0e4
7
+ data.tar.gz: 6c58763589af072a1c88fee2943128957141677b6cef241e1bec9bfd75cf315db0d8b5548d729c0f1503ceb27643f98b3eca245bce2567fbc8e7b294c22b65a5
@@ -0,0 +1,68 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ # Add #try and #try!
21
+ class Object
22
+ # Invokes the public method whose name goes as first argument just like
23
+ # `public_send` does, except that if the receiver does not respond to it the
24
+ # call returns `nil` rather than raising an exception.
25
+ #
26
+ # @note `try` is defined on `Object`. Therefore, it won't work with instances
27
+ # of classes that do not have `Object` among their ancestors, like direct
28
+ # subclasses of `BasicObject`.
29
+ #
30
+ # @param [String] object
31
+ #
32
+ # @param [Symbol] method
33
+ #
34
+ def try(*a, &b)
35
+ try!(*a, &b) if a.empty? || respond_to?(a.first)
36
+ end
37
+
38
+ # Same as #try, but will raise a NoMethodError exception if the receiver is
39
+ # not `nil` and does not implement the tried method.
40
+ #
41
+ # @raise NoMethodError
42
+ # If the receiver is not `nil` and does not implement the tried method.
43
+ #
44
+ def try!(*a, &b)
45
+ if a.empty? && block_given?
46
+ if b.arity.zero?
47
+ instance_eval(&b)
48
+ else
49
+ yield self
50
+ end
51
+ else
52
+ public_send(*a, &b)
53
+ end
54
+ end
55
+ end
56
+
57
+ class NilClass
58
+ # Calling `try` on `nil` always returns `nil`. It becomes especially helpful
59
+ # when navigating through associations that may return `nil`.
60
+ #
61
+ def try(*args)
62
+ nil
63
+ end
64
+
65
+ def try!(*args)
66
+ nil
67
+ end
68
+ end
@@ -138,6 +138,20 @@ module Hoodie
138
138
  camelize(singularize(table_name.sub(/.*\./, '')))
139
139
  end
140
140
 
141
+ # Create a snake case string with an optional namespace prepended.
142
+ #
143
+ # @param [String] input
144
+ # @param [String] namespace
145
+ # @return [String]
146
+ def snakeify(input, namespace = nil)
147
+ input = input.dup
148
+ input.sub!(/^#{namespace}(\:\:)?/, '') if namespace
149
+ input.gsub!(/[A-Z]/) {|s| "_" + s}
150
+ input.downcase!
151
+ input.sub!(/^\_/, "")
152
+ input
153
+ end
154
+
141
155
  # Test if word is uncountable.
142
156
  #
143
157
  # @param [String] word
@@ -0,0 +1,68 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ module Hoodie
21
+ # Turns object into a proxy which will forward all method missing calls.
22
+ #
23
+ class Proxy < Module
24
+ attr_reader :name
25
+
26
+ def initialize(name, options = {})
27
+ attr_reader name
28
+ ivar = "@#{name}"
29
+
30
+ attr_reader :__proxy_kind__, :__proxy_args__
31
+
32
+ define_method(:initialize) do |proxy_target, *args, &block|
33
+ instance_variable_set(ivar, proxy_target)
34
+
35
+ @__proxy_kind__ = options.fetch(:kind) { proxy_target.class }
36
+ @__proxy_args__ = args
37
+ end
38
+
39
+ define_method(:__proxy_target__) do
40
+ instance_variable_get(ivar)
41
+ end
42
+
43
+ include Methods
44
+ end
45
+
46
+ module Methods
47
+ def respond_to_missing?(method_name, include_private)
48
+ __proxy_target__.respond_to?(method_name, include_private)
49
+ end
50
+
51
+ def method_missing(method_name, *args, &block)
52
+ if __proxy_target__.respond_to?(method_name)
53
+ response = __proxy_target__.public_send(method_name, *args, &block)
54
+
55
+ if response.equal?(__proxy_target__)
56
+ self
57
+ elsif response.kind_of?(__proxy_kind__)
58
+ self.class.new(*[response]+__proxy_args__)
59
+ else
60
+ response
61
+ end
62
+ else
63
+ super
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -18,5 +18,5 @@
18
18
  #
19
19
 
20
20
  module Hoodie
21
- VERSION = '0.4.9'
21
+ VERSION = '0.5.0'
22
22
  end
data/lib/hoodie.rb CHANGED
@@ -70,6 +70,7 @@ module Hoodie
70
70
  end
71
71
 
72
72
  require 'hoodie/core_ext/string'
73
+ require 'hoodie/core_ext/object'
73
74
  require 'hoodie/core_ext/blank'
74
75
  require 'hoodie/core_ext/hash'
75
76
 
@@ -83,5 +84,6 @@ require 'hoodie/logging'
83
84
  require 'hoodie/version'
84
85
  require 'hoodie/timers'
85
86
  require 'hoodie/utils'
87
+ require 'hoodie/proxy'
86
88
  require 'hoodie/stash'
87
89
  require 'hoodie/os'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hoodie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.9
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Harding
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-01 00:00:00.000000000 Z
11
+ date: 2015-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hitimes
@@ -127,6 +127,7 @@ files:
127
127
  - lib/hoodie/core_ext/blank.rb
128
128
  - lib/hoodie/core_ext/hash.rb
129
129
  - lib/hoodie/core_ext/string.rb
130
+ - lib/hoodie/core_ext/try.rb
130
131
  - lib/hoodie/identity_map.rb
131
132
  - lib/hoodie/inflections.rb
132
133
  - lib/hoodie/inflections/defaults.rb
@@ -137,6 +138,7 @@ files:
137
138
  - lib/hoodie/obfuscate.rb
138
139
  - lib/hoodie/observable.rb
139
140
  - lib/hoodie/os.rb
141
+ - lib/hoodie/proxy.rb
140
142
  - lib/hoodie/rash.rb
141
143
  - lib/hoodie/stash.rb
142
144
  - lib/hoodie/stash/disk_store.rb
@@ -164,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
166
  version: '0'
165
167
  requirements: []
166
168
  rubyforge_project:
167
- rubygems_version: 2.4.1
169
+ rubygems_version: 2.4.2
168
170
  signing_key:
169
171
  specification_version: 4
170
172
  summary: Pragmatic hoodie concurrency hipster with ruby