Module: Mmi::PropertyAttributes::ClassMethods

Defined in:
lib/mmi/property_attributes.rb

Defined Under Namespace

Classes: PropertyConfiguration

Instance Method Summary collapse

Instance Method Details

#parse(hash) ⇒ Object



44
45
46
# File 'lib/mmi/property_attributes.rb', line 44

def parse(hash)
	new(hash).parse!
end

#property(method_name, key = method_name.to_s, type: nil, required: nil, default: nil, requires: [], conflicts: [], validate: nil) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mmi/property_attributes.rb', line 22

def property(method_name, key=method_name.to_s, type: nil, required: nil, default: nil, requires: [], conflicts: [], validate: nil)
	raise(ArgumentError, '"method_name" must be a Symbol'         ) if !method_name.is_a?(Symbol)
	raise(ArgumentError, %Q{#{type.inspect} is not a valid "type"}) if !valid_atomic_type?(type) && !valid_hash_type?(type)
	
	requires  = [requires ] if !requires .is_a?(Array)
	conflicts = [conflicts] if !conflicts.is_a?(Array)
	
	raise(ArgumentError, '"required" is mutually exclusive with "requires" and "conflicts"') if required && (requires.any? || conflicts.any?)
	raise(ArgumentError, '"requires" and "conflicts" must not share entries'               ) if requires.intersect?(conflicts)
	raise(ArgumentError, '"required" is mutually exclusive with "default"'                 ) if required && !default.nil?
	
	required = requires.none? && conflicts.none? && default.nil? if required.nil?
	
	raise(ArgumentError, '"validate" must be nil, a symbol or a Proc') if !validate.nil? && !validate.is_a?(Symbol) && !validate.is_a?(Proc)
	
	registered_properties[method_name] = PropertyConfiguration.new(key: key, type: type, required: required, default: default, requires: requires, conflicts: conflicts, validate: validate)
	
	define_method(method_name) do
		parsed_property_store[method_name]
	end
end

#registered_propertiesObject



18
19
20
# File 'lib/mmi/property_attributes.rb', line 18

def registered_properties
	@registered_properties ||= {}
end

#valid_atomic_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/mmi/property_attributes.rb', line 48

def valid_atomic_type?(type)
	type.nil? || type.respond_to?(:parse)
end

#valid_hash_type?(type_hash) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/mmi/property_attributes.rb', line 52

def valid_hash_type?(type_hash)
	type_hash.is_a?(Hash) && type_hash.key?(:field) && type_hash.key?(:types) && type_hash.fetch(:types).is_a?(Hash) && type_hash.fetch(:types).all? do |_, type|
		valid_atomic_type?(type)
	end
end