Module: CiteProc::Attributes::ClassMethods

Defined in:
lib/citeproc/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attr_field(field, default = nil, predicate = false) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/citeproc/attributes.rb', line 151

def attr_field(field, default = nil, predicate = false)
  method_id = field.to_s.downcase.gsub(/[-\s]+/, '_')

  unless instance_methods.include?(method_id)
    if default
      define_method(method_id) do
        read_attribute field
      end
    else
      define_method(method_id) do
        attributes[filter_key(field)] ||= default
      end
    end
  end

  writer_id = [method_id,'='].join
  unless instance_methods.include?(writer_id)
    define_method(writer_id) do |value|
      write_attribute field, value
    end
  end

  predicate_id = [method_id, '?'].join
  if predicate && !instance_methods.include?(predicate_id)
    define_method(predicate_id) do
      attribute?(field)
    end

    has_predicate = ['has_', predicate_id].join
    alias_method(has_predicate, predicate_id) unless instance_methods.include?(has_predicate)
  end
end

#attr_fields(*arguments) ⇒ Object



145
146
147
148
149
# File 'lib/citeproc/attributes.rb', line 145

def attr_fields(*arguments)
  arguments.flatten.each do |field|
    attr_field(*(field.is_a?(Hash) ? field.to_a.flatten : [field]))
  end
end

#attr_predicates(*arguments) ⇒ Object



138
139
140
141
142
143
# File 'lib/citeproc/attributes.rb', line 138

def attr_predicates(*arguments)
  arguments.flatten.each do |field|
    field, default = *(field.is_a?(Hash) ? field.to_a.flatten : [field])
    attr_field(field, default, true)
  end
end

#create(parameters) ⇒ Object



128
129
130
131
132
# File 'lib/citeproc/attributes.rb', line 128

def create(parameters)
  create!(parameters)
rescue
  nil
end

#create!(parameters) ⇒ Object



134
135
136
# File 'lib/citeproc/attributes.rb', line 134

def create!(parameters)
  new.merge(parameters)
end