symbol_decoration 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -5
  3. data/lib/symbol_decoration.rb +26 -4
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50f521f730b37b3aa512481b9a3cd4ac0df32bdd
4
- data.tar.gz: ace420edc4102c58d385212e33a83ce639aca91b
3
+ metadata.gz: a4eccd878da79afc079e852f01a7af59a52dcb71
4
+ data.tar.gz: 9c18b97ad316f76374d79981c1b95b4e8bbc1a02
5
5
  SHA512:
6
- metadata.gz: 447af63c714e8d6d572a9ae75575c1a5d2009515911b4e32bb5fdff36627961c1b62396f72cf8eaf380785712fe27e142ad68bf7fcc9a196409c1329ddee4358
7
- data.tar.gz: f03dd020f3df2dc87547ff4e3aa4ba4df29a75ec8c513bdaf814456e3a03bef3c9bfb92950af387ee3fad166db88cf94febd88037a9e2ec448bf71a4701f47c3
6
+ metadata.gz: eab8bb0207b68c606ad5594b5df2c31d53ddd418d453579ac3edf834a873eb398dfb0c06a46d37b4960ae87470ea9667a168e4dc84721afb8da28dd4e395ce37
7
+ data.tar.gz: f31c9d730c6f8ce22f1bd26b71f8a51069a8472330f266bac410a6bc2dcfacfc19598457a35feaa68b1f7f1f09cd0fbfcd6bc887536e34ddafe01f37958ebb26
data/README.md CHANGED
@@ -37,11 +37,22 @@ Full example:
37
37
  ```ruby
38
38
  Symbol::Decoration.register(:gt)
39
39
 
40
- :field.gt.class == Symbol::Decoration
41
- :field.gt.symbol == :field
42
- :field.gt.decorator == :gt
43
- :field.gt(5).args == [5]
44
- :field.gt { 5 }.block.call == 5
40
+ :field.gt.is_a?(Symbol::Decoration) == true
41
+ :field.gt.symbol == :field
42
+ :field.gt.decorator == :gt
43
+ :field.gt(5).args == [5]
44
+ :field.gt { 5 }.block.call == 5
45
+ ```
46
+
47
+ To allow certain decorators to be chainable, you must use the `:chainable =>
48
+ true` option when registering the decorator. Example:
49
+
50
+ ```ruby
51
+ Symbol::Decoration.register(:any, chainable => true)
52
+ Symbol::Decoration.register(:in)
53
+
54
+ :field.any.in # valid
55
+ :field.in.any # invalid
45
56
  ```
46
57
 
47
58
  License
@@ -1,13 +1,35 @@
1
1
  class Symbol
2
- Decoration = Struct.new(:symbol, :decorator, :args, :block) do
2
+ class Decoration < Struct.new(:symbol, :decorator, :args, :block)
3
+ module Extensions; end
4
+
5
+ class << self; attr_accessor :decorator_options; end
6
+ self.decorator_options = {}
7
+
3
8
  def self.register(*decorators)
4
- Symbol.class_eval do
5
- decorators.map(&:to_sym).each do |decorator|
9
+ options = decorators.last.is_a?(Hash) ? decorators.pop : {}
10
+ decorators.map(&:to_sym).each do |decorator|
11
+ self.decorator_options[decorator] = (self.decorator_options[decorator] || {}).merge(options)
12
+ klass = self.decorator_options[decorator][:chainable] ? ChainableDecoration : Decoration
13
+ Extensions.module_eval do
6
14
  define_method(decorator) do |*args, &block|
7
- Decoration.new(self, decorator, args, block)
15
+ klass.new(self, decorator, args, block)
8
16
  end
9
17
  end
10
18
  end
11
19
  end
20
+
21
+ def inspect
22
+ s = "#{symbol.inspect}.#{decorator}"
23
+ _args = block ? args + [block] : args
24
+ s += "(#{_args.map(&:inspect).join(", ")})" unless _args.empty?
25
+ s
26
+ end
27
+ alias_method :to_s, :inspect
28
+ end
29
+
30
+ class ChainableDecoration < Decoration
31
+ include Decoration::Extensions
12
32
  end
33
+
34
+ include Decoration::Extensions
13
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: symbol_decoration
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Viennot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-14 00:00:00.000000000 Z
11
+ date: 2014-11-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Support for symbol decorations such as where(:field.in => [1,2,3])
14
14
  email:
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
39
  version: '0'
40
40
  requirements: []
41
41
  rubyforge_project:
42
- rubygems_version: 2.2.2
42
+ rubygems_version: 2.4.2
43
43
  signing_key:
44
44
  specification_version: 4
45
45
  summary: Symbol Decoration