vidibus-resource 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.
@@ -6,6 +6,9 @@ module Vidibus::Resource
6
6
  module Mongoid
7
7
  extend ActiveSupport::Concern
8
8
 
9
+ # Valid types in Mongoid
10
+ VALID_TYPES = [Array, BigDecimal, Boolean, Date, DateTime, Float, Hash, Integer, String, Symbol, Time]
11
+
9
12
  included do
10
13
  include Vidibus::Uuid::Mongoid
11
14
 
@@ -52,43 +55,27 @@ module Vidibus::Resource
52
55
  end
53
56
  end
54
57
 
55
- protected
56
-
57
58
  # Populates attributes of instance from
58
59
  # received resource_attributes hash.
59
60
  def set_resource_attributes(force = false)
60
61
  if resource_attributes_changed? or force == true
61
62
  for key, value in resource_attributes
62
- send("#{key}=", value)
63
+
64
+ setter = respond_to?("#{key}=")
65
+ unless setter or respond_to?(key) # Check for setter and getter!
66
+ field_type = VALID_TYPES.include?(value.class) ? value.class : String
67
+ self.class.class_eval do
68
+ field key.to_sym, :type => field_type # Mongoid field
69
+ end
70
+ setter = true
71
+ end
72
+ send("#{key}=", value) if setter
63
73
  end
64
74
  end
65
75
  true # ensure true!
66
76
  end
67
77
 
68
- # Defines attributes of resource_attributes hash as
69
- # methods on consumer instance.
70
- def method_missing(name, *args, &block)
71
- if name.to_s.match(/(.+)\=$/)
72
- writer = true
73
- attribute = $1
74
- name = "#{attribute}="
75
- else
76
- attribute = name.id2name
77
- end
78
- if resource_attributes and (value = resource_attributes[attribute])
79
- self.class.class_eval do
80
- field attribute.to_sym, :type => value.class # Mongoid field
81
- end
82
- if writer
83
- send(name, *args)
84
- else
85
- send("#{attribute}=", value)
86
- send(name)
87
- end
88
- else
89
- super
90
- end
91
- end
78
+ protected
92
79
 
93
80
  # Fix empty arrays
94
81
  def fix_resource_attributes(data)
@@ -1,5 +1,5 @@
1
1
  module Vidibus
2
2
  module Resource
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -10,6 +10,7 @@ describe "Vidibus::Resource::Consumer::Mongoid" do
10
10
  let(:model) do
11
11
  Model.new.tap do |m|
12
12
  m.resource_attributes = {"name" => "Jenny"}
13
+ m.set_resource_attributes(true)
13
14
  end
14
15
  end
15
16
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidibus-resource
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Andr\xC3\xA9 Pankratz"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-17 00:00:00 +01:00
18
+ date: 2011-01-18 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency