winch 0.0.3 → 0.0.4

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.
Files changed (3) hide show
  1. data/lib/winch/base.rb +44 -18
  2. data/lib/winch.rb +1 -1
  3. metadata +2 -2
data/lib/winch/base.rb CHANGED
@@ -4,23 +4,40 @@ module Winch::Base
4
4
  #Either :default, or :faux can be anything including false and '', just not nil
5
5
  raise "%s can have either a default or a faux value, but not both." % name unless options.values_at(:default, :faux).select { |v| !v.nil? }.length == 1
6
6
 
7
- self.must_haves = (self.must_haves || []).push(options.merge(:name => name))
7
+ self.must_haves = (self.must_haves || {}).merge(name => options.merge(:name => name))
8
8
  end
9
9
  end
10
10
 
11
11
  module InstanceMethods
12
12
  def initialize_winch(attributes)
13
- (self.must_haves || []).each do |clause|
14
- name = clause[:name]
13
+ (self.must_haves || {}).each do |name, clause|
14
+ set_default_for_attribute(attributes, clause)
15
+ end
16
+ end
15
17
 
16
- next unless attributes[name].nil?
18
+ def preform_type_check
19
+ @broken_attributes ||= []
20
+ attributes.keys.each { |name| find_broken_in_node(name, attributes[name]) }
21
+ @well_typed = @broken_attributes.blank?
17
22
 
18
- if clause[:default].nil?
19
- @broken_attributes = (@broken_attributes || []).push(name)
20
- attributes[name] = clause[:faux]
21
- else
22
- attributes[name] = clause[:default]
23
- end
23
+ return if @well_typed
24
+ return unless self.class.parent == Object
25
+
26
+ raise Winch::TypeError.new(:object => self) if Winch.config.raise_on_broken_attributes
27
+ end
28
+
29
+ private
30
+
31
+ def set_default_for_attribute(attributes, clause)
32
+ name = clause[:name]
33
+
34
+ return unless attributes[name].nil?
35
+
36
+ if clause[:default].nil?
37
+ @broken_attributes = (@broken_attributes || []).push(name)
38
+ attributes[name] = clause[:faux]
39
+ else
40
+ attributes[name] = clause[:default]
24
41
  end
25
42
  end
26
43
 
@@ -41,17 +58,12 @@ module Winch::Base
41
58
  find_broken_in_collection(name, node)
42
59
  end
43
60
  end
44
-
45
- def preform_type_check
46
- @broken_attributes ||= []
47
- attributes.keys.each { |name| find_broken_in_node(name, attributes[name]) }
48
- @well_typed = @broken_attributes.blank?
49
- end
50
61
  end
51
-
62
+
52
63
  def self.included(klass)
53
64
  klass.class_eval do
54
- class_inheritable_array :must_haves, :instance_writer => false
65
+ class_inheritable_hash :must_haves, :instance_writer => false
66
+
55
67
  attr_reader :well_typed, :broken_attributes
56
68
  alias_method :well_typed?, :well_typed
57
69
 
@@ -59,4 +71,18 @@ module Winch::Base
59
71
  include InstanceMethods
60
72
  end
61
73
  end
74
+ end
75
+
76
+ module Winch
77
+ def self.config
78
+ @@config ||= OpenStruct.new(:raise_on_broken_attributes => false)
79
+ end
80
+ end
81
+
82
+ class Winch::TypeError < Exception
83
+ attr_accessor :object
84
+
85
+ def initialize(options={})
86
+ self.object = options[:object]
87
+ end
62
88
  end
data/lib/winch.rb CHANGED
@@ -5,7 +5,7 @@ $:.unshift(File.dirname(__FILE__)) unless
5
5
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
6
6
 
7
7
  module Winch
8
- VERSION = '0.0.3' unless defined?(Winch::VERSION)
8
+ VERSION = '0.0.4' unless defined?(Winch::VERSION)
9
9
  end
10
10
 
11
11
  require 'winch/base'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: winch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vanson Samuel
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-16 00:00:00 -05:00
12
+ date: 2009-12-21 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency