Class: ImageFlux::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/image_flux/option.rb

Constant Summary collapse

ALLOWED_FORMATS =

output attributes

%w[auto jpg png gif webp:jpeg webp:png webp:auto]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Option

Returns a new instance of Option.



88
89
90
91
92
93
94
95
96
# File 'lib/image_flux/option.rb', line 88

def initialize(options = {})
  @values = {}

  options.each_pair do |key, val|
    key = key.to_s.to_sym
    attribute = self.class.key_pairs[key]
    @values[attribute] = val if attribute
  end
end

Class Method Details

.attribute(name, type, options = {}, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/image_flux/option.rb', line 16

def self.attribute(name, type, options = {}, &block)
  attribute = ImageFlux::Attribute.new(name, type, options)
  attribute.instance_eval(&block) if block_given?
  key_pairs[attribute.name] = attribute
  attribute.aliases.each do |key|
    key_pairs[key] = attribute
  end

  attributes.push(attribute)
end

.attributesObject



8
9
10
# File 'lib/image_flux/option.rb', line 8

def self.attributes
  @attributes ||= []
end

.key_pairsObject



12
13
14
# File 'lib/image_flux/option.rb', line 12

def self.key_pairs
  @key_pairs ||= {}
end

Instance Method Details

#prefix_pathObject



98
99
100
101
# File 'lib/image_flux/option.rb', line 98

def prefix_path
  prefix_attr = self.class.key_pairs[:prefix]
  @values[prefix_attr]
end

#to_query(ignore_default: true) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/image_flux/option.rb', line 103

def to_query(ignore_default: true)
  errors = []
  queries = @values.to_a.map do |pair|
    attribute = pair.first
    value = pair.last
    errors.concat(attribute.validate!(value))
    attribute.querize(value)
  end
  raise ImageFlux::InvalidOptionError, errors.join(', ') unless errors.length.zero?

  queries.reject(&:nil?).join(',')
end