Class: Ribbon::Raw
- Inherits:
- BasicObject
- Defined in:
- lib/ribbon/raw.rb
Overview
Ribbon with the least amount of helper methods defined.
Class Method Summary collapse
-
.convert(object) ⇒ Object
Converts hashes to ribbons.
-
.convert_all!(ribbon) ⇒ Ribbon, Ribbon::Raw
Converts all values inside the given ribbon.
-
.default_value_proc ⇒ Proc
Proc used to store a new ribbon instance as the value of a missing key.
Instance Method Summary collapse
-
#[](key, &block) ⇒ Object
Fetches the value associated with the given key.
-
#[]=(key, *values) ⇒ Object
Associates the given values with the given key.
-
#__hash__ ⇒ Hash
private
The hash used internally.
-
#initialize(hash = {}, &block) ⇒ Raw
constructor
Initializes a new raw ribbon with the given values.
-
#method_missing(method, *arguments, &block) ⇒ Object
Handles the following cases:.
-
#to_s(options = {}) ⇒ String
(also: #inspect)
Generates a simple and customizable human-readable string representation of this raw ribbon.
Constructor Details
#initialize(hash = {}, &block) ⇒ Raw
Initializes a new raw ribbon with the given values.
If given a block, the raw ribbon will be yielded to it. If the block doesn’t take any arguments, it will be evaluated in the context of the raw ribbon.
All objects inside the hash will be converted.
35 36 37 38 39 |
# File 'lib/ribbon/raw.rb', line 35 def initialize(hash = {}, &block) __hash__.merge! ::Ribbon.extract_hash_from hash __yield_or_eval__ &block ::Ribbon::Raw.convert_all! self end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments, &block) ⇒ Object
Handles the following cases:
ribbon.method => ribbon[:method]
ribbon.method value => ribbon[:method] = value
ribbon.method &block => ribbon[:method, &block]
ribbon.method value, &block => ribbon[:method] = value
ribbon[:method, &block]
ribbon.method = value => ribbon[:method] = value
ribbon.method! value => ribbon[:method] = value
self
ribbon.method! &block => block.call ribbon[:method] if ribbon.__hash__.include? :method
self
ribbon.method! value, &block => ribbon[:method] = value
block.call ribbon[:method]
self
ribbon.method? => ribbon.__hash__.fetch :method
ribbon.method? value => ribbon.__hash__.fetch :method, value
ribbon.method? &block => ribbon.__hash__.fetch :method, &block
ribbon.method? value, &block => ribbon.__hash__.fetch :method, value, &block
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ribbon/raw.rb', line 97 def method_missing(method, *arguments, &block) method_name = method.to_s key = method_name.strip.gsub(/[=?!]$/, '').strip.intern case method_name[-1] when ?= __send__ :[]=, key, *arguments when ?! __send__ :[]=, key, *arguments unless arguments.empty? block.call self[key] if block and __hash__.include? key self when ?? begin __hash__.fetch key, *arguments, &block rescue ::KeyError; nil end else __send__ :[]=, key, *arguments unless arguments.empty? self[key, &block] end end |
Class Method Details
.convert(object) ⇒ Object
Converts hashes to ribbons. Will look inside arrays.
170 171 172 173 174 175 176 |
# File 'lib/ribbon/raw.rb', line 170 def convert(object) case object when Hash then Ribbon.new object when Array then object.map { |element| convert element } else object end end |
.convert_all!(ribbon) ⇒ Ribbon, Ribbon::Raw
Converts all values inside the given ribbon.
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/ribbon/raw.rb', line 184 def convert_all!(ribbon) ribbon.__hash__.each do |key, value| ribbon[key] = case value when Ribbon then convert_all! value.raw when Ribbon::Raw then convert_all! value else convert value end end ribbon end |
Instance Method Details
#[](key, &block) ⇒ Object
Fetches the value associated with the given key.
If given a block, the value will be yielded to it. If the block doesn’t take any arguments, it will be evaluated in the context of the value.
49 50 51 52 53 |
# File 'lib/ribbon/raw.rb', line 49 def [](key, &block) value = ::Ribbon::Raw.convert __hash__[key] value.__yield_or_eval__ &block self[key] = value end |
#[]=(key, *values) ⇒ Object
Associates the given values with the given key.
70 71 72 73 |
# File 'lib/ribbon/raw.rb', line 70 def []=(key, *values) raise ArgumentError, 'Raw Ribbons can not be used as hash keys' if ::Ribbon.raw? key __hash__[key] = if values.size == 1 then values.first else values end end |
#__hash__ ⇒ Hash
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.
The hash used internally.
21 22 23 |
# File 'lib/ribbon/raw.rb', line 21 def __hash__ @hash ||= (::Hash.new &::Ribbon::Raw.default_value_proc) end |
#to_s(options = {}) ⇒ String Also known as: inspect
Generates a simple and customizable human-readable string representation of this raw ribbon.
125 126 127 |
# File 'lib/ribbon/raw.rb', line 125 def to_s( = {}) __to_s_recursive__ ::Ribbon.extract_hash_from() end |