hard-boiled 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ class Array
2
+ # Synonym for #empty?
3
+ def blank?
4
+ self.empty?
5
+ end
6
+ end
7
+
8
+ class NilClass
9
+ # Synonym for #nil?
10
+ def blank?
11
+ self.nil?
12
+ end
13
+ end
@@ -1,5 +1,7 @@
1
1
  module HardBoiled
2
+ # Boilerplate
2
3
  require File.dirname(__FILE__)+'/extract_options' unless {}.respond_to?(:extractable_options?)
4
+ require File.dirname(__FILE__)+'/blank' unless nil.respond_to?(:blank?)
3
5
 
4
6
  # This class pretty much resembles what Thoughtbot did in
5
7
  # [FactoryGirl's DefinitionProxy](https://github.com/thoughtbot/factory_girl/blob/master/lib/factory_girl/definition_proxy.rb)
@@ -14,19 +16,32 @@ module HardBoiled
14
16
 
15
17
  attr_reader :subject, :parent_subject
16
18
 
17
- def self.define object, parent = nil, &block
19
+ def self.define *args, &block
18
20
  # if I could only remove the duplicate `obj`
19
- obj = new(object, parent)
21
+ obj = new(*args)
20
22
  obj.instance_eval(&block)
21
23
  obj.to_hash
22
24
  end
23
25
 
24
- def initialize subject, parent = nil
25
- @subject = subject
26
- @parent_subject = parent
26
+ def initialize *args
27
+ @options = args.extract_options!
28
+ @subject, @parent_subject = args
27
29
  @hash = {}
28
30
  end
29
31
 
32
+
33
+ # Decide whether the given trait is being needed
34
+ #
35
+ # @param [Symbol] name the identifier for a trait, like :profile
36
+ # @param [Hash{:only => Array, :except => Array}]
37
+ # @return [true, false] dependening on in- or exclusion of this trait
38
+ def with_trait name, &block
39
+ if (@options[:except].blank? || !@options[:except].include?(name)) &&
40
+ (@options[:only].blank? || @options[:only].include?(name))
41
+ self.instance_eval(&block)
42
+ end
43
+ end
44
+
30
45
  def to_hash
31
46
  @hash
32
47
  end
@@ -1,5 +1,5 @@
1
1
  module Hard
2
2
  module Boiled
3
- VERSION = "0.1.2"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -183,4 +183,33 @@ describe HardBoiled::Presenter do
183
183
  }
184
184
  end
185
185
  end
186
+
187
+ context :traits do
188
+ def with_traits obj, options = {}
189
+ Filterable.define(obj, options) {
190
+ with_trait(:instructions) {
191
+ with_trait(:timing) {
192
+ boil_time
193
+ }
194
+ temperature
195
+ }
196
+ with_trait(:presentation) {
197
+ colour
198
+ }
199
+ }
200
+ end
201
+
202
+ it "should just map instructions" do
203
+ with_traits(egg, :only => [:instructions]).should == {
204
+ :temperature => 25
205
+ }
206
+ end
207
+
208
+ it "should map everything except for timing information" do
209
+ with_traits(egg, :except => [:timing]).should == {
210
+ :colour => "white",
211
+ :temperature => 25
212
+ }
213
+ end
214
+ end
186
215
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hard-boiled
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-23 00:00:00.000000000 Z
12
+ date: 2012-04-24 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! "\n HardBoiled helps you reducing your complex models (including
15
15
  their associations)\n down to simple hashes usable for serialization into JSON
@@ -27,6 +27,7 @@ files:
27
27
  - Rakefile
28
28
  - hard-boiled.gemspec
29
29
  - lib/hard-boiled.rb
30
+ - lib/hard-boiled/blank.rb
30
31
  - lib/hard-boiled/extract_options.rb
31
32
  - lib/hard-boiled/presenter.rb
32
33
  - lib/hard-boiled/version.rb