Class: HtmlTo::HtmlToImageSettings
- Inherits:
-
Object
- Object
- HtmlTo::HtmlToImageSettings
- Defined in:
- lib/html_to.rb
Constant Summary collapse
- OPTIONS =
%i[ template width height image_name ].freeze
- DEFAULT_OPTIONS =
{ template: :circle, width: 1200, height: 630, image_name: :meta_image }.freeze
Instance Method Summary collapse
- #add_image(image_name, serializer, options = {}) ⇒ Object
- #additional_images ⇒ Object
- #find_template_path! ⇒ Object
-
#initialize(klass, serializer, options, &block) ⇒ HtmlToImageSettings
constructor
A new instance of HtmlToImageSettings.
- #setup_options ⇒ Object
- #validate_options ⇒ Object
Constructor Details
#initialize(klass, serializer, options, &block) ⇒ HtmlToImageSettings
Returns a new instance of HtmlToImageSettings.
19 20 21 22 23 24 25 26 |
# File 'lib/html_to.rb', line 19 def initialize(klass, serializer, , &block) @klass = klass @options = @serializer = serializer.to_s instance_exec(&block) if block_given? end |
Instance Method Details
#add_image(image_name, serializer, options = {}) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/html_to.rb', line 43 def add_image(image_name, serializer, = {}) @additional_images ||= {} [:image_name] = image_name @klass.class_eval do has_one_attached image_name end @additional_images[image_name] = HtmlToImageSettings.new(@klass, serializer, ) end |
#additional_images ⇒ Object
39 40 41 |
# File 'lib/html_to.rb', line 39 def additional_images @additional_images || {} end |
#find_template_path! ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/html_to.rb', line 52 def find_template_path! user_template = Rails.root.join('app/views/html_to').join("#{template}.html.erb") return user_template.to_s if File.file? user_template gem_template = File.("views/html_to/#{template}.html.erb", __dir__) return gem_template.to_s if File.file? gem_template raise ArgumentError, "html_to error Template file not found #{user_template} #{gem_template} " end |
#setup_options ⇒ Object
32 33 34 35 36 37 |
# File 'lib/html_to.rb', line 32 def OPTIONS.each do |k| instance_variable_set("@#{k}", @options[k] || DEFAULT_OPTIONS[k]) end find_template_path! end |
#validate_options ⇒ Object
28 29 30 |
# File 'lib/html_to.rb', line 28 def @options.each_key { |opt| raise ArgumentError, "html_to error #{opt} is unknown option" unless OPTIONS.include? opt } end |