Class: Code::Ruby

Inherits:
Object
  • Object
show all
Defined in:
lib/code/ruby.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw = {}) ⇒ Ruby

Returns a new instance of Ruby.



3
4
5
# File 'lib/code/ruby.rb', line 3

def initialize(raw = {})
  @raw = raw
end

Class Method Details

.from_code(raw) ⇒ Object



11
12
13
# File 'lib/code/ruby.rb', line 11

def self.from_code(raw)
  new(raw).from_code
end

.to_code(raw) ⇒ Object



7
8
9
# File 'lib/code/ruby.rb', line 7

def self.to_code(raw)
  new(raw).to_code
end

Instance Method Details

#from_codeObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/code/ruby.rb', line 53

def from_code
  if code?
    if code_nothing?
      raw.raw
    elsif code_boolean?
      raw.raw
    elsif code_decimal?
      raw.raw
    elsif code_integer?
      raw.raw
    elsif code_nothing?
      raw.raw
    elsif code_range?
      raw.raw
    elsif code_string?
      raw.raw
    elsif code_dictionnary?
      raw
        .raw
        .map do |key, value|
          [::Code::Ruby.from_code(key), ::Code::Ruby.from_code(value)]
        end
        .to_h
    elsif code_list?
      raw.raw.map { |element| ::Code::Ruby.from_code(element) }
    else
      raise "Unsupported class #{raw.class} for Code to Ruby conversion"
    end
  else
    raw
  end
end

#to_codeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/code/ruby.rb', line 15

def to_code
  if code?
    raw
  elsif nil?
    ::Code::Object::Nothing.new
  elsif true?
    ::Code::Object::Boolean.new(raw)
  elsif false?
    ::Code::Object::Boolean.new(raw)
  elsif string?
    ::Code::Object::String.new(raw)
  elsif symbol?
    ::Code::Object::String.new(raw.to_s)
  elsif integer?
    ::Code::Object::Integer.new(raw)
  elsif float?
    ::Code::Object::Decimal.new(raw.to_s)
  elsif big_decimal?
    ::Code::Object::Decimal.new(raw)
  elsif hash?
    ::Code::Object::Dictionnary.new(
      raw
        .map do |key, value|
          [::Code::Ruby.to_code(key), ::Code::Ruby.to_code(value)]
        end
        .to_h
    )
  elsif array?
    ::Code::Object::List.new(
      raw.map { |element| ::Code::Ruby.to_code(element) }
    )
  elsif proc?
    ::Code::Object::RubyFunction.new(raw)
  else
    raise "Unsupported class #{raw.class} for Ruby to Code conversion"
  end
end