Skip to content

Commit

Permalink
refactor and clean the model settings code
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Apr 17, 2024
1 parent ddc4caf commit 46631ea
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 45 deletions.
14 changes: 9 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ GEM
docile (1.4.0)
domain_name (0.6.20240107)
eventmachine (1.2.7)
faraday (2.8.1)
base64
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
faraday (2.9.0)
faraday-net_http (>= 2.0, < 3.2)
faraday-net_http (3.1.0)
net-http
http-accept (1.7.0)
http-cookie (1.0.5)
domain_name (~> 0.5)
Expand All @@ -63,6 +62,8 @@ GEM
multi_json (1.15.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
net-http (0.4.1)
uri
net-http-persistent (2.9.4)
netrc (0.11.0)
pry (0.14.2)
Expand Down Expand Up @@ -116,11 +117,14 @@ GEM
thread_safe (0.3.6)
tilt (2.3.0)
tzinfo (0.3.62)
uri (0.13.0)
uuid (2.3.9)
macaddr (~> 1.0)

PLATFORMS
x86_64-darwin-18
x86_64-darwin-23
x86_64-linux

DEPENDENCIES
activesupport
Expand Down
45 changes: 12 additions & 33 deletions lib/goo/base/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def valid?
self.class.attributes.each do |attr|
inst_value = self.instance_variable_get("@#{attr}")
attr_errors = Goo::Validators::Enforce.enforce(self,attr,inst_value)
unless attr_errors.nil?
validation_errors[attr] = attr_errors
end
validation_errors[attr] = attr_errors unless attr_errors.nil?
end

if !@persistent && validation_errors.length == 0
Expand All @@ -70,9 +68,7 @@ def valid?
end

def id=(new_id)
if !@id.nil? and @persistent
raise ArgumentError, "The id of a persistent object cannot be changed."
end
raise ArgumentError, "The id of a persistent object cannot be changed." if !@id.nil? and @persistent
raise ArgumentError, "ID must be an RDF::URI" unless new_id.kind_of?(RDF::URI)
@id = new_id
end
Expand Down Expand Up @@ -128,6 +124,7 @@ def unmapped_set(attribute,value)

def unmmaped_to_array
cpy = {}

@unmapped.each do |attr,v|
cpy[attr] = v.to_a
end
Expand All @@ -136,19 +133,15 @@ def unmmaped_to_array

def delete(*args)
if self.kind_of?(Goo::Base::Enum)
unless args[0] && args[0][:init_enum]
raise ArgumentError, "Enums cannot be deleted"
end
raise ArgumentError, "Enums cannot be deleted" unless args[0] && args[0][:init_enum]
end

raise ArgumentError, "This object is not persistent and cannot be deleted" if !@persistent

if !fully_loaded?
missing = missing_load_attributes
options_load = { models: [ self ], klass: self.class, :include => missing }
if self.class.collection_opts
options_load[:collection] = self.collection
end
options_load[:collection] = self.collection if self.class.collection_opts
Goo::SPARQL::Queries.model_load(options_load)
end

Expand All @@ -164,25 +157,19 @@ def delete(*args)
end
@persistent = false
@modified = true
if self.class.inmutable? && self.class.inm_instances
self.class.load_inmutable_instances
end
self.class.load_inmutable_instances if self.class.inmutable? && self.class.inm_instances
return nil
end

def bring(*opts)
opts.each do |k|
if k.kind_of?(Hash)
k.each do |k2,v|
if self.class.handler?(k2)
raise ArgumentError, "Unable to bring a method based attr #{k2}"
end
raise ArgumentError, "Unable to bring a method based attr #{k2}" if self.class.handler?(k2)
self.instance_variable_set("@#{k2}",nil)
end
else
if self.class.handler?(k)
raise ArgumentError, "Unable to bring a method based attr #{k}"
end
raise ArgumentError, "Unable to bring a method based attr #{k}" if self.class.handler?(k)
self.instance_variable_set("@#{k}",nil)
end
end
Expand All @@ -197,9 +184,7 @@ def bring(*opts)

def graph
opts = self.class.collection_opts
if opts.nil?
return self.class.uri_type
end
return self.class.uri_type if opts.nil?
col = collection
if col.is_a?Array
if col.length == 1
Expand Down Expand Up @@ -281,9 +266,7 @@ def collection
if opts.instance_of?(Symbol)
if self.class.attributes.include?(opts)
value = self.send("#{opts}")
if value.nil?
raise ArgumentError, "Collection `#{opts}` is nil"
end
raise ArgumentError, "Collection `#{opts}` is nil" if value.nil?
return value
else
raise ArgumentError, "Collection `#{opts}` is not an attribute"
Expand Down Expand Up @@ -370,9 +353,7 @@ def save(*opts)

@modified_attributes = Set.new
@persistent = true
if self.class.inmutable? && self.class.inm_instances
self.class.load_inmutable_instances
end
self.class.load_inmutable_instances if self.class.inmutable? && self.class.inm_instances
return self
end

Expand Down Expand Up @@ -410,9 +391,7 @@ def to_hash
end
end
@unmapped.each do |attr,values|
unless all_attr_uris.include?(attr)
attr_hash[attr] = values.map { |v| v.to_s }
end
attr_hash[attr] = values.map { |v| v.to_s } unless all_attr_uris.include?(attr)
end
end
attr_hash[:id] = @id
Expand Down
30 changes: 23 additions & 7 deletions lib/goo/base/settings/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,12 @@ def attribute(*args)
attr_name = attr_name.to_sym
options = options.pop
options = {} if options.nil?
if options[:enforce].nil? or !options[:enforce].include?(:list)
options[:enforce] = options[:enforce] ? (options[:enforce] << :no_list) : [:no_list]
end

options[:enforce] ||= []

set_data_type(options)
set_no_list_by_default(options)

@model_settings[:attributes][attr_name] = options
load_yaml_scheme_options(attr_name)
shape_attribute(attr_name)
Expand Down Expand Up @@ -248,15 +251,13 @@ def shape_attribute(attr)
raise ArgumentError, "Method based attributes cannot be set"
end
if self.class.inverse?(attr) && !(args && args.last.instance_of?(Hash) && args.last[:on_load])
raise ArgumentError,
"`#{attr}` is an inverse attribute. Values cannot be assigned."
raise ArgumentError, "`#{attr}` is an inverse attribute. Values cannot be assigned."
end
@loaded_attributes.add(attr)
value = args[0]
unless args.last.instance_of?(Hash) and args.last[:on_load]
if self.persistent? and self.class.name_with == attr
raise ArgumentError,
"`#{attr}` attribute is used to name this resource and cannot be modified."
raise ArgumentError, "`#{attr}` attribute is used to name this resource and cannot be modified."
end
prev = self.instance_variable_get("@#{attr}")
if !prev.nil? and !@modified_attributes.include?(attr)
Expand Down Expand Up @@ -388,6 +389,21 @@ def read_only(attributes)
instance
end


private

def set_no_list_by_default(options)
if options[:enforce].nil? or !options[:enforce].include?(:list)
options[:enforce] = options[:enforce] ? (options[:enforce] << :no_list) : [:no_list]
end
end
def set_data_type(options)
if options[:type]
options[:enforce] += Array(options[:type])
options[:enforce].uniq!
options.delete :type
end
end
end
end
end
Expand Down

0 comments on commit 46631ea

Please sign in to comment.