Working on adding support for allowing dynamic properties
This commit is contained in:
parent
85cd1308bc
commit
97347e70e3
5 changed files with 43 additions and 7 deletions
|
@ -4,6 +4,7 @@ module CouchRest::Model
|
|||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
include CouchRest::Model::Configuration
|
||||
include CouchRest::Model::AttributeProtection
|
||||
include CouchRest::Model::Attributes
|
||||
include CouchRest::Model::Callbacks
|
||||
|
|
|
@ -9,9 +9,11 @@ module CouchRest
|
|||
|
||||
included do
|
||||
add_config :model_type_key
|
||||
add_config :allow_dynamic_properties
|
||||
|
||||
configure do |config|
|
||||
config.model_type_key = 'model'
|
||||
config.allow_dynamic_properties = false
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ module CouchRest
|
|||
|
||||
def write_attribute(property, value)
|
||||
prop = find_property!(property)
|
||||
self[prop.to_s] = prop.cast(self, value)
|
||||
self[prop.to_s] = prop.is_a?(String) ? value : prop.cast(self, value)
|
||||
end
|
||||
|
||||
def apply_all_property_defaults
|
||||
|
@ -41,10 +41,11 @@ module CouchRest
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
def find_property!(property)
|
||||
prop = property.is_a?(Property) ? property : self.class.properties.detect {|p| p.to_s == property.to_s}
|
||||
raise ArgumentError, "Missing property definition for #{property.to_s}" unless prop
|
||||
prop
|
||||
raise ArgumentError, "Missing property definition for #{property.to_s}" unless allow_dynamic_properties or !prop.nil?
|
||||
prop || property
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue