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 Classes: BasicObject, Function, Memo, Object

Constant Summary collapse

VERSION =
"0.5.1"

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



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ryo.rb', line 186

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:



56
57
58
# File 'lib/ryo/basic_object.rb', line 56

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:



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

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

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



71
72
73
# File 'lib/ryo.rb', line 71

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

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

Creates a Ryo object by recursively walking a Hash object.

Parameters:

Returns:



86
87
88
# File 'lib/ryo.rb', line 86

def self.from(props, prototype = nil)
  Ryo::Object.from(props, prototype)
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:



56
57
58
# File 'lib/ryo/object.rb', line 56

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

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

Returns a Ryo object extended by mod.

Parameters:

Returns:



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

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

Instance Method Details

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

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

Returns:



94
95
96
# File 'lib/ryo.rb', line 94

def __proto__
  @_proto
end

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

Note:

This method will first try to read property from self, and if it is not found on self the chain of prototypes will be traversed through instead.

Returns the value at property, or nil.

Parameters:

  • property (String)

    A property name.

Returns:



109
110
111
112
113
114
115
116
117
118
# File 'lib/ryo.rb', line 109

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) ⇒ void

This method returns an undefined value.

Assigns a property to self.

Parameters:



130
131
132
# File 'lib/ryo.rb', line 130

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

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

Returns true other is equal to self.

Parameters:

Returns:

  • (Boolean)

    Returns true other is equal to self.



140
141
142
143
144
145
146
147
148
# File 'lib/ryo.rb', line 140

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

#inspectString

Returns a String representation of a Ryo object.

Returns:

  • (String)

    Returns a String representation of a Ryo object.



154
155
156
# File 'lib/ryo.rb', line 154

def inspect
  Ryo.inspect_object(self)
end

#to_hHash Also known as: to_hash

Returns the hash table used by a Ryo object.

Returns:

  • (Hash)

    Returns the hash table used by a Ryo object.



161
162
163
# File 'lib/ryo.rb', line 161

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