Skip to content

Commit

Permalink
Merge pull request #85 from iiif-prezi/development2
Browse files Browse the repository at this point in the history
Add v3 models
  • Loading branch information
thatbudakguy authored Oct 25, 2023
2 parents 9c64a8b + 0548b16 commit 156747e
Show file tree
Hide file tree
Showing 50 changed files with 4,849 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
coverage/
pkg/
Gemfile.lock
.pry_history

4 changes: 4 additions & 0 deletions lib/iiif/presentation/canvas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def int_only_keys
super + %w{ width height }
end

def numeric_only_keys
super + %w{ duration }
end

def legal_viewing_hint_values
super + %w{ non-paged }
end
Expand Down
137 changes: 36 additions & 101 deletions lib/iiif/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ def array_only_keys; %w{ }; end
def abstract_resource_only_keys; %w{ }; end
def hash_only_keys; %w{ }; end
def int_only_keys; %w{ }; end
def numeric_only_keys; %w{ }; end

def initialize(hsh={})
@data = IIIF::OrderedHash[hsh]
self.define_methods_for_any_type_keys
self.define_methods_for_array_only_keys
self.define_methods_for_string_only_keys
self.define_methods_for_int_only_keys
self.define_methods_for_numeric_only_keys
self.define_methods_for_abstract_resource_only_keys
self.snakeize_keys
end
Expand Down Expand Up @@ -255,56 +257,22 @@ def data
end

def define_methods_for_any_type_keys
define_accessor_methods(*any_type_keys)

# override the getter defined by define_accessor_methods to avoid returning
# an array for empty values.
any_type_keys.each do |key|
# Setters
define_singleton_method("#{key}=") do |arg|
self.send('[]=', key, arg)
end
if key.camelize(:lower) != key
define_singleton_method("#{key.camelize(:lower)}=") do |arg|
self.send('[]=', key, arg)
end
end
# Getters
define_singleton_method(key) do
self.send('[]', key)
end
if key.camelize(:lower) != key
define_singleton_method(key.camelize(:lower)) do
self.send('[]', key)
end
end
end
end

def define_methods_for_array_only_keys
array_only_keys.each do |key|
# Setters
define_singleton_method("#{key}=") do |arg|
unless arg.kind_of?(Array)
m = "#{key} must be an Array."
raise IIIF::Presentation::IllegalValueError, m
end
self.send('[]=', key, arg)
end
if key.camelize(:lower) != key
define_singleton_method("#{key.camelize(:lower)}=") do |arg|
unless arg.kind_of?(Array)
m = "#{key} must be an Array."
raise IIIF::Presentation::IllegalValueError, m
end
self.send('[]=', key, arg)
end
end
# Getters
define_singleton_method(key) do
self[key] ||= []
self[key]
end
if key.camelize(:lower) != key
define_singleton_method(key.camelize(:lower)) do
self.send('[]', key)
end
define_accessor_methods(*array_only_keys) do |key, arg|
unless arg.kind_of?(Array)
m = "#{key} must be an Array."
raise IIIF::Presentation::IllegalValueError, m
end
end
end
Expand All @@ -314,85 +282,53 @@ def define_methods_for_abstract_resource_only_keys
abstract_resource_only_keys.each do |hsh|
key = hsh[:key]
type = hsh[:type]
# Setters
define_singleton_method("#{key}=") do |arg|

define_accessor_methods(key) do |key, arg|
unless arg.kind_of?(type)
m = "#{key} must be an #{type}."
raise IIIF::Presentation::IllegalValueError, m
end
self.send('[]=', key, arg)
end
if key.camelize(:lower) != key
define_singleton_method("#{key.camelize(:lower)}=") do |arg|
unless arg.kind_of?(type)
m = "#{key} must be an #{type}."
raise IIIF::Presentation::IllegalValueError, m
end
self.send('[]=', key, arg)
end
end
# Getters
define_singleton_method(key) do
self[key] ||= []
self[key]
end
if key.camelize(:lower) != key
define_singleton_method(key.camelize(:lower)) do
self.send('[]', key)
end
end
end
end


def define_methods_for_string_only_keys
string_only_keys.each do |key|
# Setter
define_singleton_method("#{key}=") do |arg|
unless arg.kind_of?(String)
m = "#{key} must be an String."
raise IIIF::Presentation::IllegalValueError, m
end
self.send('[]=', key, arg)
end
if key.camelize(:lower) != key
define_singleton_method("#{key.camelize(:lower)}=") do |arg|
unless arg.kind_of?(String)
m = "#{key} must be an String."
raise IIIF::Presentation::IllegalValueError, m
end
self.send('[]=', key, arg)
end
define_accessor_methods(*string_only_keys) do |key, arg|
unless arg.kind_of?(String)
m = "#{key} must be an String."
raise IIIF::Presentation::IllegalValueError, m
end
# Getter
define_singleton_method(key) do
self[key] ||= []
self[key]
end
end

def define_methods_for_int_only_keys
define_accessor_methods(*int_only_keys) do |key, arg|
unless arg.kind_of?(Integer) && arg > 0
m = "#{key} must be a positive Integer."
raise IIIF::Presentation::IllegalValueError, m
end
if key.camelize(:lower) != key
define_singleton_method(key.camelize(:lower)) do
self.send('[]', key)
end
end
end

def define_methods_for_numeric_only_keys
define_accessor_methods(*numeric_only_keys) do |key, arg|
unless arg.kind_of?(Numeric) && arg > 0
m = "#{key} must be a positive Integer or Float."
raise IIIF::Presentation::IllegalValueError, m
end
end
end

def define_methods_for_int_only_keys
int_only_keys.each do |key|
def define_accessor_methods(*keys, &validation)
keys.each do |key|
# Setter
define_singleton_method("#{key}=") do |arg|
unless arg.kind_of?(Integer) && arg > 0
m = "#{key} must be a positive Integer."
raise IIIF::Presentation::IllegalValueError, m
end
validation.call(key, arg) if block_given?
self.send('[]=', key, arg)
end
if key.camelize(:lower) != key
define_singleton_method("#{key.camelize(:lower)}=") do |arg|
unless arg.kind_of?(Integer) && arg > 0
m = "#{key} must be a positive Integer."
raise IIIF::Presentation::IllegalValueError, m
end
validation.call(key, arg) if block_given?
self.send('[]=', key, arg)
end
end
Expand All @@ -408,6 +344,5 @@ def define_methods_for_int_only_keys
end
end
end

end
end
Loading

0 comments on commit 156747e

Please sign in to comment.