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.
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
-
.BasicObject(props, prototype = nil) ⇒ Ryo::BasicObject
Returns an instance of Ryo::BasicObject.
-
.dup(ryo) ⇒ <Ryo::Object, Ryo::BasicObject>
Duplicates a Ryo object, and its prototype(s).
-
.memo(&b) ⇒ Ryo::Memo
(also: lazy)
Creates a memoized Ryo value.
-
.from(props, prototype = nil) ⇒ Ryo::Object
Creates a Ryo object by recursively walking a Hash object.
-
.Object(props, prototype = nil) ⇒ Ryo::Object
Returns an instance of Ryo::Object.
-
.extend!(ryo, mod) ⇒ <Ryo::Object, Ryo::BasicObject>
Returns a Ryo object extended by mod.
Instance Method Summary collapse
-
#__proto__ ⇒ <Ryo::Object, Ryo::BasicObject>?
Returns the prototype of self, or "nil" if self has no prototype.
-
#[](property) ⇒ <Object, BasicObject>?
Returns the value at property, or nil.
-
#[]=(property, value) ⇒ void
Assigns a property to self.
-
#==(other) ⇒ Boolean
(also: #eql?)
Returns true other is equal to self.
-
#inspect ⇒ String
Returns a String representation of a Ryo object.
-
#to_h ⇒ Hash
(also: #to_hash)
Returns the hash table used by a Ryo object.
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
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.
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).
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.
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.
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.
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.
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.
94 95 96 |
# File 'lib/ryo.rb', line 94 def __proto__ @_proto end |
#[](property) ⇒ <Object, BasicObject>?
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.
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.
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.
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 |
#inspect ⇒ 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 |