Class: Foobara::BuiltinTypes::Datetime::Casters::Hash

Inherits:
TypeDeclarations::Caster show all
Defined in:
foobara-0.0.110/projects/builtin_types/src/datetime/casters/hash.rb

Overview

TODO: there seems to be an issue with the design here. We cast first and casters are transformers (don’t return errors.) However, it would probably give a better error if we could indicate exactly what was wrong with this hash instead of a catch-all error mixed with all the other casters errors. The obvious solution is to inherit Value::Processor instead. Maybe that would just work without any other changes needed. TODO: try that

Instance Attribute Summary

Attributes inherited from Value::Processor

#created_in_namespace, #declaration_data, #parent_declaration_data

Instance Method Summary collapse

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods inherited from Value::Caster

create, foobara_manifest, requires_declaration_data?, subclass, #transform

Methods inherited from Value::Transformer

create, error_classes, foobara_manifest, #process_value, subclass, #transform

Methods inherited from Value::Processor

#always_applicable?, #attribute_name, #build_error, default_declaration_data, #dup_processor, error_class, error_classes, #error_context, #error_message, #error_path, foobara_manifest, #foobara_manifest, #initialize, #inspect, instance, #method_missing, #name, new_with_agnostic_args, #possible_errors, #priority, #process_outcome, #process_outcome!, #process_value, #process_value!, processor_name, requires_declaration_data?, requires_parent_declaration_data?, #respond_to_missing?, #runner, symbol

Methods included from IsManifestable

#foobara_domain, #foobara_manifest, #foobara_organization, #scoped_clear_caches

Constructor Details

This class inherits a constructor from Foobara::Value::Processor

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::Value::Processor

Instance Method Details

#applicable?(hash) ⇒ Boolean

Returns:



12
13
14
# File 'foobara-0.0.110/projects/builtin_types/src/datetime/casters/hash.rb', line 12

def applicable?(hash)
  hash.is_a?(::Hash) && valid_hash?(hash)
end

#applies_messageObject



16
17
18
# File 'foobara-0.0.110/projects/builtin_types/src/datetime/casters/hash.rb', line 16

def applies_message
  "be a Hash with :year, :month, :day, :hours, :minutes, :seconds, :milliseconds, and :zone keys"
end

#cast(hash) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'foobara-0.0.110/projects/builtin_types/src/datetime/casters/hash.rb', line 20

def cast(hash)
  hash = datetime_attributes_type.process_value!(hash)

  year = hash[:year]
  month = hash[:month]
  day = hash[:day]
  hours = hash[:hours]
  minutes = hash[:minutes]
  seconds = hash[:seconds]
  milliseconds = hash[:milliseconds]
  zone = hash[:zone]

  if milliseconds
    seconds = seconds.to_r + (milliseconds.to_r / 1000)
  end

  ::Time.new(year, month, day, hours, minutes, seconds, zone)
end

#datetime_attributes_typeObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'foobara-0.0.110/projects/builtin_types/src/datetime/casters/hash.rb', line 39

def datetime_attributes_type
  @datetime_attributes_type ||= type_for_declaration(
    year: :integer,
    month: { type: :integer, min: 1, max: 12 },
    day: { type: :integer, min: 1, max: 31 },
    hours: { type: :integer, min: 0, max: 23, required: false, default: 0 },
    minutes: { type: :integer, min: 0, max: 59, required: false, default: 0 },
    seconds: { type: :big_decimal, min: 0, max: 59, required: false, default: 0 },
    milliseconds: { type: :integer, min: 0, max: 1000, required: false },
    zone: { type: :string, required: false }
  )
end