module CiteProc::Attributes

Public Class Methods

included(base) click to toggle source
# File lib/citeproc/attributes.rb, line 6
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

[](key)
Alias for: read_attribute
[]=(key, value)
Alias for: write_attribute
attribute?(key) click to toggle source
# File lib/citeproc/attributes.rb, line 28
def attribute?(key)
  # this method is used only for conditional type access.
  # When included on an object with read observations, don't count this as an observable read
  if respond_to? :unobservable_read_attribute
    value = unobservable_read_attribute key
  else
    value = read_attribute key
  end

  return false if value.nil?
  return false if value.respond_to?(:empty?) && value.empty?

  value.to_s !~ /^(false|no|never)$/i
end
eql?(other) click to toggle source

Two Attribute-based objects are equal if they are the same object, or if all their attributes are equal using #eql?.

@param other [Object] the other object @return [Boolean] whether or not self and passed-in object are equal

# File lib/citeproc/attributes.rb, line 107
def eql?(other)
  case
  when equal?(other)
    true
  when self.class != other.class, length != other.length
    false
  else
    other.attributes.each_pair do |key, value|
      return false unless attributes[key].eql?(value)
    end

    true
  end
end
hash() click to toggle source

@return [Fixnum] a hash value based on the object's attributes

# File lib/citeproc/attributes.rb, line 123
def hash
  digest = size
  attributes.each do |attribute|
    digest ^= attribute.hash
  end

  digest
end
merge(other) click to toggle source
# File lib/citeproc/attributes.rb, line 53
def merge(other)
  return self if other.nil?

  case
  when other.is_a?(String) && /^\s*\{/ =~ other
    other = ::JSON.parse(other, :symbolize_names => true)
  when other.respond_to?(:each_pair)
    # do nothing
  when other.respond_to?(:to_hash)
    other = other.to_hash
  else
    raise ParseError, "failed to merge attributes and #{other.inspect}"
  end

  other.each_pair do |key, value|
    attributes[filter_key(key)] = filter_value(value, key)
  end

  self
end
Also aliased as: update
read_attribute(key) click to toggle source
# File lib/citeproc/attributes.rb, line 18
def read_attribute(key)
  attributes[filter_key(key)]
end
Also aliased as: []
reverse_merge(other) click to toggle source
# File lib/citeproc/attributes.rb, line 75
def reverse_merge(other)
  fail "not implemented yet"
end
to_citeproc() click to toggle source

@return [Hash] a hash-based representation of the attributes

# File lib/citeproc/attributes.rb, line 84
def to_citeproc
  Hash[attributes.map { |k,v|
    [k.to_s, v.respond_to?(:to_citeproc) ? v.to_citeproc : v.to_s]
  }]
end
to_hash() click to toggle source
# File lib/citeproc/attributes.rb, line 79
def to_hash
  attributes.deep_copy
end
to_json() click to toggle source

@return [String] a JSON string representation of the attributes

# File lib/citeproc/attributes.rb, line 91
def to_json
  ::JSON.dump(to_citeproc)
end
update(other)
Alias for: merge
write_attribute(key, value) click to toggle source
# File lib/citeproc/attributes.rb, line 23
def write_attribute(key, value)
  attributes[filter_key(key)] = filter_value(value, key)
end
Also aliased as: []=

Protected Instance Methods

attributes() click to toggle source
# File lib/citeproc/attributes.rb, line 10
def attributes
  @attributes ||= {}
end

Private Instance Methods

filter_key(key) click to toggle source
# File lib/citeproc/attributes.rb, line 43
def filter_key(key)
  key.to_sym
end
filter_value(value, key = nil) click to toggle source
# File lib/citeproc/attributes.rb, line 47
def filter_value(value, key = nil)
  value.respond_to?(:deep_copy) ? value.deep_copy : value.dup
rescue
  value
end