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.



5
6
7
# File 'lib/code/ruby.rb', line 5

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

Class Method Details

.from_code(raw) ⇒ Object



13
14
15
# File 'lib/code/ruby.rb', line 13

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

.to_code(raw) ⇒ Object



9
10
11
# File 'lib/code/ruby.rb', line 9

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

Instance Method Details

#from_codeObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/code/ruby.rb', line 55

def from_code
  if code?
    if code_nothing? || code_boolean? || code_decimal? || code_integer? ||
         code_range? || 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



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
52
53
# File 'lib/code/ruby.rb', line 17

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::Dictionary.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