Module: Ryo

Extended by:
Enumerable, Keywords, Reflect
Defined in:
lib/ryo.rb,
lib/ryo/version.rb

Overview

The Ryo module implements most of its behavior as singleton methods that are inherited from the Ryo::Reflect, and Ryo:Keywords modules.

Examples:

# Ryo.delete
point = Ryo(x: 0, y: 0)
Ryo.delete(point, "x")
point.x # => nil

# Ryo.assign
point = Ryo.assign(Ryo({}), {x: 0}, {y: 0})
point.x # => 0

Defined Under Namespace

Modules: Builder, Enumerable, JSON, Keywords, Reflect, Utils, YAML Classes: BasicObject, Function, Memo, Object

Constant Summary collapse

VERSION =
"0.5.6"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Reflect

assign, call_method, class_of, define_property, delete!, equal?, function?, inspect_object, memo?, properties_of, property?, prototype_chain_of, prototype_of, ryo?, set_prototype_of, set_table_of, table_of

Methods included from Keywords

delete, fn, in?

Methods included from Enumerable

all?, any?, each, each_ryo, find, map, map!, reject, reject!, select, select!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &b) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/ryo.rb', line 174

def method_missing(name, *args, &b)
  property = name.to_s
  if property[-1] == "="
    property = property[0..-2]
    self[property] = args.first
  elsif Ryo.property?(self, property)
    self[property]
  elsif @_proto
    Ryo.call_method(@_proto, name, *args, &b)
       .tap { _1.bind!(self) if Ryo.function?(_1) }
  end
end

Class Method Details

.BasicObject(props, prototype = nil) ⇒ Ryo::BasicObject

Returns an instance of Ryo::BasicObject

Examples:

point = Ryo::BasicObject(x: 0, y: 0)
p [point.x, point.y] # => [0, 0]

Parameters:

Returns:



52
53
54
# File 'lib/ryo/basic_object.rb', line 52

def Ryo.BasicObject(props, prototype = nil)
  Ryo::BasicObject.create(props, prototype)
end

.dup(ryo) ⇒ <Ryo::Object, Ryo::BasicObject>

Duplicates a Ryo object, and its prototype(s).

Parameters:

Returns:



50
51
52
53
54
55
56
57
58
59
# File 'lib/ryo.rb', line 50

def self.dup(ryo)
  duplicate = extend!(
    kernel(:dup).bind_call(ryo),
    self
  )
  if proto = prototype_of(duplicate)
    set_prototype_of(duplicate, dup(proto))
  end
  duplicate
end

.extend!(ryo, mod) ⇒ <Ryo::Object, Ryo::BasicObject>

Returns an extended Ryo object

Parameters:

Returns:



39
40
41
# File 'lib/ryo.rb', line 39

def self.extend!(ryo, mod)
  kernel(:extend).bind_call(ryo, mod)
end

.from(props, prototype = nil) ⇒ Ryo::Object

Creates a Ryo object by recursively walking a Hash object

Parameters:

Returns:



82
83
84
# File 'lib/ryo.rb', line 82

def self.from(props, prototype = nil)
  Ryo::Object.from(props, prototype)
end

.memo(&b) ⇒ Ryo::Memo Also known as: lazy

Creates a memoized Ryo value

Parameters:

  • b (Proc)

    A Proc that is memoized after being accessed for the first time

Returns:



68
69
70
# File 'lib/ryo.rb', line 68

def self.memo(&b)
  Ryo::Memo.new(&b)
end

.Object(props, prototype = nil) ⇒ Ryo::Object

Returns an instance of Ryo::Object

Examples:

point = Ryo::Object(x: 0, y: 0)
p [point.x, point.y] # => [0, 0]

Parameters:

Returns:



52
53
54
# File 'lib/ryo/object.rb', line 52

def Ryo.Object(props, prototype = nil)
  Ryo::Object.create(props, prototype)
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Returns true other is equal to self

Parameters:

Returns:

  • (Boolean)

    Returns true other is equal to self



128
129
130
131
132
133
134
135
136
# File 'lib/ryo.rb', line 128

def ==(other)
  if Ryo.ryo?(other)
    @_table == Ryo.table_of(other)
  else
    other = Hash.try_convert(other)
    return false unless other
    @_table == other.map { [_1.to_s, _2] }.to_h
  end
end

#[](property) ⇒ <Object, BasicObject>?

Note:

This method will first query self for a property, and if the property is not found the query is sent to Ryo#proto instead

Returns the property's value, or nil

Parameters:

  • property (String)

    The name of a property

Returns:



102
103
104
105
106
107
108
109
110
111
# File 'lib/ryo.rb', line 102

def [](property)
  property = property.to_s
  if Ryo.property?(self, property)
    v = @_table[property]
    Ryo.memo?(v) ? self[property] = v.call : v
  else
    return unless @_proto
    Ryo.call_method(@_proto, property)
  end
end

#[]=(property, value)

This method returns an undefined value.

Assign a property

Parameters:



120
121
122
# File 'lib/ryo.rb', line 120

def []=(property, value)
  Ryo.define_property(self, property.to_s, value)
end

#__proto__<Ryo::Object, Ryo::BasicObject>?

Returns the prototype of self, or nil if self has no prototype

Returns:



89
90
91
# File 'lib/ryo.rb', line 89

def __proto__
  @_proto
end

#inspectString

Returns a String representation of a Ryo object

Returns:

  • (String)

    Returns a String representation of a Ryo object



142
143
144
# File 'lib/ryo.rb', line 142

def inspect
  Ryo.inspect_object(self)
end

#to_hHash Also known as: to_hash

Returns the lookup table of a Ryo object

Returns:

  • (Hash)

    Returns the lookup table of a Ryo object



149
150
151
# File 'lib/ryo.rb', line 149

def to_h
  Ryo.table_of(self, recursive: true)
end