Class: OOP::BasicRules

Inherits:
Object
  • Object
show all
Defined in:
lib/oop/basic_rules.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cast_to_value(arg) ⇒ Object



10
11
12
# File 'lib/oop/basic_rules.rb', line 10

def self.cast_to_value(arg)
  self.instance.cast_to_value(arg)
end

.instanceObject



6
7
8
# File 'lib/oop/basic_rules.rb', line 6

def self.instance
  @instance ||= new
end

Instance Method Details

#cast_to_value(arg) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/oop/basic_rules.rb', line 14

def cast_to_value(arg)
  if arg.respond_to?(:super_mega_private_oop_method_value?) && arg.super_mega_private_oop_method_value?
    return arg
  end

  case arg
  when Value, TrueClass, FalseClass, NilClass, Proc then
    arg
  when Hash then
    Hash[arg.map{|k,v| [cast_to_value(k), cast_to_value(v)] }.to_h].freeze
  when Array then
    arg.map{|v| cast_to_value(v) }.freeze
  when Symbol, Fixnum, String, Float, Time then
    if arg.frozen?
      arg
    else
      arg.freeze
    end
  else
    raise NonValueArg.new(arg.class)
  end
end