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 36 37 38 39 40 41 |
# 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) ⇒ Boolean
Two Attribute-based objects are equal if they are the same object, or if all their attributes are equal using #eql?.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# 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 ⇒ Fixnum
Returns a hash value based on the object’s attributes.
123 124 125 126 127 128 129 130 |
# File 'lib/citeproc/attributes.rb', line 123 def hash digest = size attributes.each do |attribute| digest ^= attribute.hash end digest end |
#merge(other) ⇒ Object Also known as: update
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# 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 |
#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
75 76 77 |
# File 'lib/citeproc/attributes.rb', line 75 def reverse_merge(other) fail "not implemented yet" end |
#to_citeproc ⇒ Hash
Returns a hash-based representation of the attributes.
84 85 86 87 88 |
# 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 ⇒ Object
79 80 81 |
# File 'lib/citeproc/attributes.rb', line 79 def to_hash attributes.deep_copy end |
#to_json ⇒ String
Returns a JSON string representation of the attributes.
91 92 93 |
# File 'lib/citeproc/attributes.rb', line 91 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 |