Module: Ryo::Builder Private

Defined in:
lib/ryo/builder.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

The Ryo::Builder module is responsible for the creation of instances of Ryo::Object, and Ryo::BasicObject. This module is not intended to be used directly.

Class Method Summary collapse

Class Method Details

.build(buildee, props, prototype = nil) ⇒ <Ryo::Object, Ryo::BasicObject>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

When "props" is given as a Ryo object, a duplicate Ryo object is returned in its place

Returns a Ryo object

Parameters:

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ryo/builder.rb', line 19

def self.build(buildee, props, prototype = nil)
  if Ryo.ryo?(props)
    build(buildee, Ryo.table_of(props), prototype || Ryo.prototype_of(props))
  else
    ryo = buildee.new
    Ryo.set_prototype_of(ryo, prototype)
    Ryo.set_table_of(ryo, {})
    Ryo.extend!(ryo, Ryo)
    props.each_pair { ryo[_1] = _2 }
    ryo
  end
end

.recursive_build(buildee, props, prototype = nil) ⇒ <Ryo::Object, Ryo::BasicObject>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

When "props" is given as a Ryo object, a duplicate Ryo object is returned in its place

Creates a Ryo object by recursively walking a Hash object, or an Array of Hash objects

Examples:

objects = Ryo.from([{x: 0, y: 0}, "foo", {point: {x: 0, y: 0}}])
objects[0].x       # => 0
objects[1]         # => "foo"
objects[2].point.x # => 0

Parameters:

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ryo/builder.rb', line 45

def self.recursive_build(buildee, props, prototype = nil)
  if Ryo.ryo?(props)
    recursive_build(buildee, Ryo.table_of(props), prototype || Ryo.prototype_of(props))
  elsif !respond_to_each?(props)
    raise TypeError, "The provided object does not implement #each / #each_pair"
  elsif !props.respond_to?(:each_pair)
    map(props) do
      noop = Ryo.ryo?(_1) || !_1.respond_to?(:each_pair)
      noop ? _1 : recursive_build(buildee, _1)
    end
  else
    visited = {}
    props.each_pair { visited[_1] = map_value(buildee, _2) }
    build(buildee, visited, prototype)
  end
end