Class: Ribbon::Raw

Inherits:
BasicObject
Defined in:
lib/ribbon/raw.rb

Overview

Ribbon with the least amount of helper methods defined.

See Also:

Author:

  • Matheus Afonso Martins Moreira

Since:

  • 0.8.0

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

See Also:

Since:

  • 0.8.0



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

Since:

  • 0.8.0



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.

Parameters:

  • object

    the object to convert

Returns:

  • the converted value

Since:

  • 0.8.0



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.

Parameters:

Returns:

See Also:

Since:

  • 0.8.0



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

.default_value_procProc

Proc used to store a new ribbon instance as the value of a missing key.

Returns:

  • (Proc)

    the proc used when constructing new hashes

Since:

  • 0.8.0



162
163
164
# File 'lib/ribbon/raw.rb', line 162

def default_value_proc
  @default_value_proc ||= (proc { |hash, key| hash[key] = Ribbon.new })
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.

Parameters:

  • key

    the key which identifies the value

Returns:

  • the value associated with the given key

See Also:

Since:

  • 0.8.0



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.

Examples:

ribbon = Ribbon.new

ribbon[:key] = :value
ribbon[:key]
# => :value

ribbon[:key] = :multiple, :values
ribbon[:key]
# => [:multiple, :values]

Parameters:

  • key

    the key that will identify the values

  • values

    the values that will be associated with the key

Raises:

  • (ArgumentError)

    if the given key is a raw ribbon

Since:

  • 0.8.0



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.

Returns:

  • (Hash)

    the hash used by this Ribbon instance to store data

Since:

  • 0.8.0



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.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :separator (#to_s) — default: ': '

    separates the key/value pair

  • :key (#to_sym) — default: :to_s

    will be sent to the key in order to convert it to a string.

  • :value (#to_sym) — default: :inspect

    will be sent to the value in order to convert it to a string.

Returns:

  • (String)

    the string representation of this raw ribbon

Since:

  • 0.8.0



125
126
127
# File 'lib/ribbon/raw.rb', line 125

def to_s(options = {})
  __to_s_recursive__ ::Ribbon.extract_hash_from(options)
end