class ChupaText::Attributes

Attributes of data.

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/chupa-text/attributes.rb, line 26
def initialize
  super
  @extra_data = {}
end

Public Instance Methods

[](name) click to toggle source

Gets the value of attribute named `name`.

@param [Symbol, String] name The attribute name. @return [Object] The attribute value.

Calls superclass method
# File lib/chupa-text/attributes.rb, line 57
def [](name)
  name = normalize_name(name)
  if members.include?(name)
    super
  else
    @extra_data[name]
  end
end
[]=(name, value) click to toggle source

Sets `value` as the value of attribute named `name`.

@param [Symbol, String] name The attribute name. @param [Object] value The attribute value.

# File lib/chupa-text/attributes.rb, line 70
def []=(name, value)
  name = normalize_name(name)
  if members.include?(name)
    send("#{name}=", value)
  else
    @extra_data[name] = value
  end
end
created_time=(time) click to toggle source

Sets `time` as the `created_time` attribute value.

@param [String, Integer, Time, nil] time The created time.

If `time` is `Integer`, it is used as UNIX time.
Calls superclass method
# File lib/chupa-text/attributes.rb, line 90
def created_time=(time)
  super(normalize_time(time))
end
each(&block) click to toggle source

@yield [name, value] Gives attribute name and value to the block. @yieldparam [Symbol] name The attribute name. @yieldparam [Object] value The attribute value.

# File lib/chupa-text/attributes.rb, line 44
def each(&block)
  if block.nil?
    Enumerator.new(self, :each)
  else
    each_pair(&block)
    @extra_data.each_pair(&block)
  end
end
encoding=(encoding) click to toggle source

Sets `encoding` as the `encoding` attribute value.

@param [String, Encoding, nil] encoding The encoding.

Calls superclass method
# File lib/chupa-text/attributes.rb, line 82
def encoding=(encoding)
  super(normalize_encoding(encoding))
end
inspect() click to toggle source
Calls superclass method
# File lib/chupa-text/attributes.rb, line 35
def inspect
  super.gsub(/>\z/) do
    " @extra_data=#{@extra_data.inspect}>"
  end
end
modified_time=(time) click to toggle source

Sets `time` as the `modified_time` attribute value.

@param [String, Integer, Time, nil] time The modified time.

If `time` is `Integer`, it is used as UNIX time.
Calls superclass method
# File lib/chupa-text/attributes.rb, line 98
def modified_time=(time)
  super(normalize_time(time))
end
to_h() click to toggle source
Calls superclass method
# File lib/chupa-text/attributes.rb, line 31
def to_h
  super.merge(@extra_data)
end

Private Instance Methods

normalize_encoding(encoding) click to toggle source
# File lib/chupa-text/attributes.rb, line 107
def normalize_encoding(encoding)
  if encoding.is_a?(String)
    encoding = Encoding.find(encoding)
  end
  encoding
end
normalize_name(name) click to toggle source
# File lib/chupa-text/attributes.rb, line 103
def normalize_name(name)
  name.to_sym
end
normalize_time(time) click to toggle source
# File lib/chupa-text/attributes.rb, line 114
def normalize_time(time)
  case time
  when String
    Time.parse(time)
  when Integer
    Time.at(time).utc
  else
    time
  end
end