Module: CiteProc::Attributes
- Extended by:
- Forwardable
- Included in:
- CitationItem, Date, Item, Name
- Defined in:
- lib/citeproc/attributes.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #attribute?(key) ⇒ Boolean
-
#eql?(other) ⇒ Boolean
Two Attribute-based objects are equal if they are the same object, or if all their attributes are equal using #eql?.
-
#hash ⇒ Fixnum
A hash value based on the object’s attributes.
- #merge(other) ⇒ Object (also: #update)
- #read_attribute(key) ⇒ Object (also: #[])
- #reverse_merge(other) ⇒ Object
-
#to_citeproc ⇒ Hash
A hash-based representation of the attributes.
- #to_hash ⇒ Object
-
#to_json ⇒ String
A JSON string representation of the attributes.
- #write_attribute(key, value) ⇒ Object (also: #[]=)
Instance Method Details
#attribute?(key) ⇒ Boolean
28 29 30 31 32 33 34 35 |
# File 'lib/citeproc/attributes.rb', line 28 def attribute?(key) value = read_attribute key return false if value.nil? return false if value.respond_to?(:empty?) && value.empty? value.to_s !~ /^(false|no|never)$/i end |
#eql?(other) ⇒ Boolean
Two Attribute-based objects are equal if they are the same object, or if all their attributes are equal using #eql?.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/citeproc/attributes.rb', line 101 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 ⇒ Fixnum
Returns a hash value based on the object’s attributes.
117 118 119 120 121 122 123 124 |
# File 'lib/citeproc/attributes.rb', line 117 def hash digest = size attributes.each do |attribute| digest ^= attribute.hash end digest end |
#merge(other) ⇒ Object Also known as: update
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/citeproc/attributes.rb', line 47 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 |
#read_attribute(key) ⇒ Object Also known as: []
18 19 20 |
# File 'lib/citeproc/attributes.rb', line 18 def read_attribute(key) attributes[filter_key(key)] end |
#reverse_merge(other) ⇒ Object
69 70 71 |
# File 'lib/citeproc/attributes.rb', line 69 def reverse_merge(other) fail "not implemented yet" end |
#to_citeproc ⇒ Hash
Returns a hash-based representation of the attributes.
78 79 80 81 82 |
# File 'lib/citeproc/attributes.rb', line 78 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 ⇒ Object
73 74 75 |
# File 'lib/citeproc/attributes.rb', line 73 def to_hash attributes.deep_copy end |
#to_json ⇒ String
Returns a JSON string representation of the attributes.
85 86 87 |
# File 'lib/citeproc/attributes.rb', line 85 def to_json ::JSON.dump(to_citeproc) end |
#write_attribute(key, value) ⇒ Object Also known as: []=
23 24 25 |
# File 'lib/citeproc/attributes.rb', line 23 def write_attribute(key, value) attributes[filter_key(key)] = filter_value(value, key) end |