Sha256: eece02bb1617cbda94a8dd9033fc05deb2c9b59e8b69fa251fbd0a99d1ebcd57

Contents?: true

Size: 1.44 KB

Versions: 44

Compression:

Stored size: 1.44 KB

Contents

require "active_model"

module Lookbook
  class StringValueCaster < Service
    def initialize(value, type = "string")
      @value = value.to_s
      @type = type.to_s.downcase
      @cast_method = :"cast_to_#{@type}"

      unless respond_to?(@cast_method)
        raise ArgumentError.new "'#{@type}' is not a valid value type to cast to."
      end
    end

    def call
      return @nil if @value.empty?
      public_send(@cast_method)
    rescue => exception
      Lookbook.logger.debug "Failed to parse '#{@value}' into a '#{@type}' [#{exception}]"
      raise exception
    end

    def cast_to_string
      @value
    end

    def cast_to_symbol
      @value.delete_prefix(":").to_sym if @value.present?
    end

    def cast_to_hash
      result = YamlParser.call(@value)
      unless result.is_a?(Hash)
        raise ParserError.new "'#{@value}' is not a YAML Hash"
      end
      result
    end

    def cast_to_array
      result = YamlParser.call(@value)
      unless result.is_a?(Array)
        raise ParserError.new "'#{@value}' is not a YAML Array"
      end
      result
    end

    def cast_to_datetime
      DateTime.parse(@value)
    end

    def active_model_cast
      type_class = "ActiveModel::Type::#{@type.camelize}".constantize
      type_class.new.cast(@value)
    end

    alias_method :cast_to_boolean, :active_model_cast
    alias_method :cast_to_integer, :active_model_cast
    alias_method :cast_to_float, :active_model_cast
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
lookbook-2.3.4 lib/lookbook/services/string_value_caster.rb
lookbook-2.3.3 lib/lookbook/services/string_value_caster.rb
lookbook-2.3.2 lib/lookbook/services/string_value_caster.rb
lookbook-2.3.0 lib/lookbook/services/string_value_caster.rb
lookbook-2.2.2 lib/lookbook/services/string_value_caster.rb
lookbook-2.2.1 lib/lookbook/services/string_value_caster.rb
lookbook-2.2.0 lib/lookbook/services/string_value_caster.rb
lookbook-2.1.1 lib/lookbook/services/string_value_caster.rb
lookbook-2.1.0 lib/lookbook/services/string_value_caster.rb
lookbook-2.0.5 lib/lookbook/services/string_value_caster.rb
lookbook-2.0.4 lib/lookbook/services/string_value_caster.rb
lookbook-2.0.3 lib/lookbook/services/string_value_caster.rb
lookbook-2.0.2 lib/lookbook/services/string_value_caster.rb
lookbook-2.0.1 lib/lookbook/services/string_value_caster.rb
lookbook-2.0.0 lib/lookbook/services/string_value_caster.rb
lookbook-2.0.0.rc.3 lib/lookbook/services/string_value_caster.rb
lookbook-1.5.5 lib/lookbook/services/string_value_caster.rb
lookbook-1.5.4 lib/lookbook/services/string_value_caster.rb
lookbook-2.0.0.rc.2 lib/lookbook/services/string_value_caster.rb
lookbook-2.0.0.rc.1 lib/lookbook/services/string_value_caster.rb