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



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
183
184
185
186
187
188
# File 'lib/citeproc/attributes.rb', line 157

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



151
152
153
154
155
# File 'lib/citeproc/attributes.rb', line 151

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



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

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



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

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

#create!(parameters) ⇒ Object



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

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