Sha256: ae8b5c3634d37b0cf6544cd7cd096488fc65573ddeb046818b09d625c25a94bf

Contents?: true

Size: 918 Bytes

Versions: 2

Compression:

Stored size: 918 Bytes

Contents

require 'hashie'
require 'json'

module LinkThumbnailer
  class Object < Hashie::Mash

    def method_missing(method_name, *args, &block)
      method_name = method_name.to_s

      if method_name.end_with?('?')
        method_name.chop!
        !self[method_name].nil?
      else
        self[method_name]
      end
    end

    def valid?
      return false if keys.empty?
      LinkThumbnailer.configuration.mandatory_attributes.each { |a| return false if self[a].nil? || self[a].empty? } if LinkThumbnailer.configuration.strict
      true
    end

    def to_hash
      if images.none? { |i| i.is_a?(String) }
        super.merge('images' => images.map(&:to_hash))
      else
        super
      end
    end

    def to_json
      if images.none? { |i| i.is_a?(String) }
        JSON.generate(to_hash.merge('images' => images.map(&:to_hash)))
      else
        JSON.generate(to_hash)
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
link_thumbnailer-1.1.1 lib/link_thumbnailer/object.rb
link_thumbnailer-1.1.0 lib/link_thumbnailer/object.rb