Class: ImageFlux::Attribute
- Inherits:
-
Object
- Object
- ImageFlux::Attribute
- Defined in:
- lib/image_flux/attribute.rb
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #expand(value) ⇒ Object
-
#initialize(name, type, default: nil, aliases: [], enum: nil, query: true) ⇒ Attribute
constructor
A new instance of Attribute.
- #querize(value, ignore_default: true) ⇒ Object
- #validate(&block) ⇒ Object
- #validate!(value) ⇒ Object
- #validate_type(value) ⇒ Object
Constructor Details
#initialize(name, type, default: nil, aliases: [], enum: nil, query: true) ⇒ Attribute
Returns a new instance of Attribute.
7 8 9 10 11 12 13 14 15 |
# File 'lib/image_flux/attribute.rb', line 7 def initialize(name, type, default: nil, aliases: [], enum: nil, query: true) @name = name.to_s.to_sym @type = type @default = default @aliases = aliases.flatten.map { |a| a.to_s.to_sym }.uniq @enum = enum @validators = [] @query = query end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
16 17 18 |
# File 'lib/image_flux/attribute.rb', line 16 def aliases @aliases end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
16 17 18 |
# File 'lib/image_flux/attribute.rb', line 16 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/image_flux/attribute.rb', line 16 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
16 17 18 |
# File 'lib/image_flux/attribute.rb', line 16 def type @type end |
Instance Method Details
#expand(value) ⇒ Object
22 23 24 |
# File 'lib/image_flux/attribute.rb', line 22 def (value) @enum ? @enum.fetch(value) { value } : value end |
#querize(value, ignore_default: true) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/image_flux/attribute.rb', line 26 def querize(value, ignore_default: true) value = (value) return nil if !@query || (ignore_default && default == value) value = case type when :string value.to_s when :integer value.to_i.to_s when :float value.to_f.to_s when :integer_array value.map(&:to_i).join(':') when :float_array value.map(&:to_f).join(':') when :boolean value ? '1' : '0' else value end "#{name}=#{value}" end |
#validate(&block) ⇒ Object
18 19 20 |
# File 'lib/image_flux/attribute.rb', line 18 def validate(&block) @validators.push(block) end |
#validate!(value) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/image_flux/attribute.rb', line 50 def validate!(value) errors = [] value = (value) errors.push(validate_type(value)) @validators.each do |validator| errors.push(validator.call(value)) end errors.reject(&:nil?) end |
#validate_type(value) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/image_flux/attribute.rb', line 60 def validate_type(value) check = case type when :string value.is_a?(String) || value.respond_to?(:to_s) when :integer value.is_a?(Integer) || value.respond_to?(:to_i) when :float value.is_a?(Float) || value.respond_to?(:to_f) when :integer_array value.is_a?(Array) && value.all? { |elem| elem.is_a?(Integer) || elem.respond_to?(:to_i) } when :float_array value.is_a?(Array) && value.all? { |elem| elem.is_a?(Float) || elem.respond_to?(:to_i) } when :boolean value.is_a?(TrueClass) || value.is_a?(FalseClass) else true end return "#{name} is invalid type(expected be a #{type})" unless check end |