_id
stringlengths
2
6
title
stringlengths
9
130
partition
stringclasses
3 values
text
stringlengths
30
4.3k
language
stringclasses
1 value
meta_information
dict
q12200
AttrEncrypted.InstanceMethods.encrypt
train
def encrypt(attribute, value) encrypted_attributes[attribute.to_sym][:operation] = :encrypting encrypted_attributes[attribute.to_sym][:value_present]
ruby
{ "resource": "" }
q12201
AttrEncrypted.InstanceMethods.encrypted_attributes
train
def encrypted_attributes @encrypted_attributes ||= begin duplicated= {} self.class.encrypted_attributes.map
ruby
{ "resource": "" }
q12202
Colorize.InstanceMethods.colorize
train
def colorize(params) return self if self.class.disable_colorization require_windows_libs scan_for_colors.inject(self.class.new) do |str, match| colors_from_params(match, params)
ruby
{ "resource": "" }
q12203
Colorize.InstanceMethods.colorized?
train
def colorized? scan_for_colors.inject([]) do |colors, match|
ruby
{ "resource": "" }
q12204
Colorize.InstanceMethods.colors_from_params
train
def colors_from_params(match, params) case params when Hash then colors_from_hash(match, params) when
ruby
{ "resource": "" }
q12205
Colorize.InstanceMethods.colors_from_hash
train
def colors_from_hash(match, hash) match[0] = mode(hash[:mode]) if mode(hash[:mode]) match[1] = color(hash[:color])
ruby
{ "resource": "" }
q12206
Colorize.ClassMethods.color_methods
train
def color_methods colors.each do |key| next if key == :default define_method key do
ruby
{ "resource": "" }
q12207
Racc.GrammarFileParser.parse_user_code
train
def parse_user_code epilogue = @scanner.epilogue return unless epilogue.text epilogue.text.scan(/^----([^\n\r]*)(?:\n|\r\n|\r)(.*?)(?=^----|\Z)/m) do label = canonical_label($~[1])
ruby
{ "resource": "" }
q12208
Racc.SimulatedAutomaton.consume!
train
def consume!(token) return self if @error action = @state.action[token] || @state.defact case action when Shift @sstack.push(@state) @state = action.goto_state shifted(token) when Reduce reduce_by!(action.rule) consume!(token)
ruby
{ "resource": "" }
q12209
Racc.States.path
train
def path(state, rule) rule.symbols.each_with_object([]) do |tok, path| goto = state.gotos[tok]
ruby
{ "resource": "" }
q12210
Racc.States.walk_graph
train
def walk_graph(bitmap, graph) index = Array.new(graph.size, nil) traversed = Set.new graph.nodes do |node|
ruby
{ "resource": "" }
q12211
Racc.States.detailed_transition_graph
train
def detailed_transition_graph @dtgraph ||= each_with_object(Graph::Labeled.new(size)) do |s, graph| s.gotos.each do |tok, goto| path = if tok.terminal? [tok] else
ruby
{ "resource": "" }
q12212
Racc.Rule.replace
train
def replace(placeholder, actual) raise 'wrong placeholder' if placeholder != @target @target.heads.delete(ptrs[0]) if @target @target = actual @target.heads
ruby
{ "resource": "" }
q12213
Racc.Sym.expand
train
def expand @expand ||= Racc.set_closure(@heads.dup) do |ptr|
ruby
{ "resource": "" }
q12214
Refile.AttachmentHelper.attachment_image_tag
train
def attachment_image_tag(record, name, *args, fallback: nil, host: nil, prefix: nil, format: nil, **options) file = record && record.public_send(name) classes = ["attachment", (record.class.model_name.singular if record), name, *options[:class]] if file
ruby
{ "resource": "" }
q12215
Refile.AttachmentHelper.attachment_cache_field
train
def attachment_cache_field(object_name, method, object:, **options) options[:data] ||= {} options[:data][:reference] ||= SecureRandom.hex attacher_value = object.send("#{method}_data") hidden_options = { multiple: options[:multiple], value: attacher_value.try(:to_json), ...
ruby
{ "resource": "" }
q12216
Refile.Attachment.accepts_attachments_for
train
def accepts_attachments_for(collection_name, collection_class:, accessor_prefix:, attachment: :file, append: false) include MultipleAttachments.new( collection_name, collection_class:
ruby
{ "resource": "" }
q12217
Refile.File.download
train
def download return io if io.is_a?(Tempfile) Tempfile.new(id, binmode: true).tap
ruby
{ "resource": "" }
q12218
VCR.Response.to_hash
train
def to_hash { 'status' => status.to_hash, 'headers' => headers, 'body' => serializable_body, 'http_version' => http_version
ruby
{ "resource": "" }
q12219
VCR.Configuration.around_http_request
train
def around_http_request(*filters, &block) unless VCR.fibers_available? raise Errors::NotSupportedError.new \ "VCR::Configuration#around_http_request requires fibers, " + "which are not available on your ruby intepreter." end fibers = {} fiber_errors = {} hook_a...
ruby
{ "resource": "" }
q12220
Phony.DSL.country
train
def country country_code, definition, options = {} return unless Phony.config.load?(country_code) definition.with country_code, options
ruby
{ "resource": "" }
q12221
Phony.DSL.fixed
train
def fixed length, options = {} options[:zero] = true if
ruby
{ "resource": "" }
q12222
Phony.CountryCodes.add
train
def add country_code, country country_code = country_code.to_s optimized_country_code_access = country_code.size
ruby
{ "resource": "" }
q12223
Phony.CountryCodes.split
train
def split number # Split the number into country, cc, and national part. country, cc, national_number = partial_split number # Split the national
ruby
{ "resource": "" }
q12224
Phony.CountryCodes.format
train
def format number, options = {} country, _, national_number = partial_split number
ruby
{ "resource": "" }
q12225
Phony.CountryCodes.plausible?
train
def plausible? number, hints = {} normalized = clean number # False if it fails the basic check. # return false unless (4..16) === normalized.size country, cc, rest = partial_split normalized # Country code plausible? # cc_needed = hints[:cc]
ruby
{ "resource": "" }
q12226
Phony.CountryCodes.vanity?
train
def vanity? number country, _, national = partial_split number
ruby
{ "resource": "" }
q12227
Phony.CountryCodes.partial_split
train
def partial_split number cc = '' 1.upto(3) do |i| cc << number.slice!(0..0) country = countries[i][cc] return [country, cc,
ruby
{ "resource": "" }
q12228
Phony.Country.split
train
def split national_number _, trunk, ndc, *rest = internal_split
ruby
{ "resource": "" }
q12229
Phony.Country.format
train
def format national_number, options = {} type = options[:format] || @format space = options[:spaces] || @space || @@default_space local_space = options[:local_spaces] || @local_space || space || @@default_local_space parentheses = options[:parentheses...
ruby
{ "resource": "" }
q12230
Phony.Country.normalize
train
def normalize national_number clean! national_number normalized = @codes.reduce national_number do |number, code| result = code.normalize number
ruby
{ "resource": "" }
q12231
Phony.Country.plausible?
train
def plausible? rest, hints = {} local, _, ndc, *rest = internal_split rest # Element based checking. # # Note: ndc == false means the country has none. # return false if ndc.nil? return false if ndc && ndc.empty? return false if @invalid_ndcs && @invalid_ndcs === ndc ...
ruby
{ "resource": "" }
q12232
Mongoid.Criteria.merge!
train
def merge!(other) criteria = other.to_criteria selector.merge!(criteria.selector) options.merge!(criteria.options) self.documents = criteria.documents.dup unless criteria.documents.empty?
ruby
{ "resource": "" }
q12233
Mongoid.Criteria.only
train
def only(*args) return clone if args.flatten.empty? args = args.flatten if (args & Fields::IDS).empty? args.unshift(:_id) end
ruby
{ "resource": "" }
q12234
Mongoid.Criteria.read
train
def read(value = nil) clone.tap do |criteria|
ruby
{ "resource": "" }
q12235
Mongoid.Criteria.respond_to?
train
def respond_to?(name, include_private = false) super
ruby
{ "resource": "" }
q12236
Mongoid.Criteria.check_for_missing_documents!
train
def check_for_missing_documents!(result, ids) if (result.size < ids.size) && Mongoid.raise_not_found_error raise
ruby
{ "resource": "" }
q12237
Mongoid.Criteria.initialize_copy
train
def initialize_copy(other) @inclusions = other.inclusions.dup @scoping_options = other.scoping_options @documents
ruby
{ "resource": "" }
q12238
Mongoid.Criteria.type_selection
train
def type_selection klasses = klass._types if klasses.size > 1 { _type: { "$in" => klass._types }}
ruby
{ "resource": "" }
q12239
Mongoid.Positional.positionally
train
def positionally(selector, operations, processed = {}) if selector.size == 1 || selector.values.any? { |val| val.nil? } return operations end keys = selector.keys.map{
ruby
{ "resource": "" }
q12240
Mongoid.Touchable.define_touchable!
train
def define_touchable!(association) name = association.name method_name = define_relation_touch_method(name, association) association.inverse_class.tap do |klass| klass.after_save
ruby
{ "resource": "" }
q12241
Mongoid.Loggable.default_logger
train
def default_logger logger = Logger.new($stdout)
ruby
{ "resource": "" }
q12242
Mongoid.Copyable.process_localized_attributes
train
def process_localized_attributes(klass, attrs) klass.localized_fields.keys.each do |name| if value = attrs.delete(name) attrs["#{name}_translations"] = value end end klass.embedded_relations.each do |_, association| next unless attrs.present? && attrs[association.key]...
ruby
{ "resource": "" }
q12243
Mongoid.Shardable.shard_key_selector
train
def shard_key_selector selector = {} shard_key_fields.each do |field| selector[field.to_s]
ruby
{ "resource": "" }
q12244
Mongoid.Reloadable.reload
train
def reload reloaded = _reload if Mongoid.raise_not_found_error && reloaded.empty? raise Errors::DocumentNotFound.new(self.class, _id, _id) end @attributes = reloaded @attributes_before_type_cast = {} changed_attributes.clear reset_readonly apply_defaults
ruby
{ "resource": "" }
q12245
Mongoid.Reloadable.reload_root_document
train
def reload_root_document {}.merge(collection.find({ _id: _id }, session:
ruby
{ "resource": "" }
q12246
Mongoid.Reloadable.reload_embedded_document
train
def reload_embedded_document extract_embedded_attributes({}.merge( collection(_root).find(_id:
ruby
{ "resource": "" }
q12247
Mongoid.Reloadable.extract_embedded_attributes
train
def extract_embedded_attributes(attributes) atomic_position.split(".").inject(attributes) do |attrs, part|
ruby
{ "resource": "" }
q12248
Mongoid.Attributes.attribute_present?
train
def attribute_present?(name) attribute = read_raw_attribute(name) !attribute.blank? || attribute == false
ruby
{ "resource": "" }
q12249
Mongoid.Attributes.read_attribute
train
def read_attribute(name) field = fields[name.to_s] raw = read_raw_attribute(name)
ruby
{ "resource": "" }
q12250
Mongoid.Attributes.read_attribute_before_type_cast
train
def read_attribute_before_type_cast(name) attr = name.to_s if attributes_before_type_cast.key?(attr)
ruby
{ "resource": "" }
q12251
Mongoid.Attributes.remove_attribute
train
def remove_attribute(name) as_writable_attribute!(name) do |access| _assigning do attribute_will_change!(access)
ruby
{ "resource": "" }
q12252
Mongoid.Attributes.write_attribute
train
def write_attribute(name, value) access = database_field_name(name) if attribute_writable?(access) _assigning do validate_attribute_value(access, value) localized = fields[access].try(:localized?) attributes_before_type_cast[name.to_s] = value
ruby
{ "resource": "" }
q12253
Mongoid.Attributes.attribute_missing?
train
def attribute_missing?(name) selection = __selected_fields return false unless selection field = fields[name]
ruby
{ "resource": "" }
q12254
Mongoid.Attributes.typed_value_for
train
def typed_value_for(key, value) fields.key?(key)
ruby
{ "resource": "" }
q12255
Mongoid.Attributes.validate_attribute_value
train
def validate_attribute_value(access, value) return unless fields[access] && value validatable_types = [ Hash, Array ] if validatable_types.include? fields[access].type unless value.is_a? fields[access].type
ruby
{ "resource": "" }
q12256
Mongoid.Factory.build
train
def build(klass, attributes = nil) attributes ||= {} type = attributes[TYPE] || attributes[TYPE.to_sym] if type && klass._types.include?(type)
ruby
{ "resource": "" }
q12257
Mongoid.Factory.from_db
train
def from_db(klass, attributes = nil, criteria = nil, selected_fields = nil) if criteria selected_fields ||= criteria.options[:fields] end type = (attributes || {})[TYPE] if type.blank? obj = klass.instantiate(attributes, selected_fields) if criteria && criteria.associatio...
ruby
{ "resource": "" }
q12258
Mongoid.Atomic.add_atomic_pull
train
def add_atomic_pull(document) document.flagged_for_destroy
ruby
{ "resource": "" }
q12259
Mongoid.Atomic.add_atomic_unset
train
def add_atomic_unset(document) document.flagged_for_destroy
ruby
{ "resource": "" }
q12260
Mongoid.Atomic.atomic_updates
train
def atomic_updates(_use_indexes = false) process_flagged_destroys mods = Modifiers.new generate_atomic_updates(mods, self) _children.each do |child|
ruby
{ "resource": "" }
q12261
Mongoid.Atomic.atomic_pulls
train
def atomic_pulls pulls = {} delayed_atomic_pulls.each_pair do |_, docs| path = nil ids = docs.map do |doc| path ||= doc.flag_as_destroyed doc._id end
ruby
{ "resource": "" }
q12262
Mongoid.Atomic.atomic_unsets
train
def atomic_unsets unsets = [] delayed_atomic_unsets.each_pair do |name, docs| path = nil docs.each do |doc|
ruby
{ "resource": "" }
q12263
Mongoid.Atomic.generate_atomic_updates
train
def generate_atomic_updates(mods, doc) mods.unset(doc.atomic_unsets) mods.pull(doc.atomic_pulls) mods.set(doc.atomic_sets)
ruby
{ "resource": "" }
q12264
Mongoid.PersistenceContext.collection
train
def collection(parent = nil) parent ? parent.collection.with(client_options)
ruby
{ "resource": "" }
q12265
Mongoid.PersistenceContext.client
train
def client @client ||= (client = Clients.with_name(client_name) client =
ruby
{ "resource": "" }
q12266
Mongoid.Changeable.changes
train
def changes _changes = {} changed.each do |attr| change = attribute_change(attr) _changes[attr]
ruby
{ "resource": "" }
q12267
Mongoid.Changeable.move_changes
train
def move_changes @previous_changes = changes Atomic::UPDATES.each do |update|
ruby
{ "resource": "" }
q12268
Mongoid.Changeable.attribute_change
train
def attribute_change(attr) attr = database_field_name(attr)
ruby
{ "resource": "" }
q12269
Mongoid.Changeable.attribute_changed?
train
def attribute_changed?(attr) attr = database_field_name(attr) return false unless changed_attributes.key?(attr)
ruby
{ "resource": "" }
q12270
Mongoid.Changeable.attribute_changed_from_default?
train
def attribute_changed_from_default?(attr) field = fields[attr]
ruby
{ "resource": "" }
q12271
Mongoid.Changeable.attribute_was
train
def attribute_was(attr) attr = database_field_name(attr)
ruby
{ "resource": "" }
q12272
Mongoid.Changeable.attribute_will_change!
train
def attribute_will_change!(attr) unless changed_attributes.key?(attr) changed_attributes[attr]
ruby
{ "resource": "" }
q12273
Mongoid.Changeable.reset_attribute!
train
def reset_attribute!(attr) attr = database_field_name(attr)
ruby
{ "resource": "" }
q12274
Rails.Mongoid.load_models
train
def load_models(app) app.config.paths["app/models"].expanded.each do |path| preload = ::Mongoid.preload_models if preload.resizable? files = preload.map { |model| "#{path}/#{model.underscore}.rb" } else files = Dir.glob("#{path}/**/*.rb") end
ruby
{ "resource": "" }
q12275
Rails.Mongoid.load_model
train
def load_model(file) begin require_dependency(file) rescue Exception => e
ruby
{ "resource": "" }
q12276
Mongoid.Fields.apply_default
train
def apply_default(name) unless attributes.key?(name) if field = fields[name] default = field.eval_default(self) unless default.nil? || field.lazy?
ruby
{ "resource": "" }
q12277
Mongoid.Persistable.post_process_persist
train
def post_process_persist(result, options = {}) post_persist unless result == false
ruby
{ "resource": "" }
q12278
Mongoid.Persistable.process_atomic_operations
train
def process_atomic_operations(operations) operations.each do |field, value| access = database_field_name(field) yield(access, value)
ruby
{ "resource": "" }
q12279
Mongoid.Persistable.persist_or_delay_atomic_operation
train
def persist_or_delay_atomic_operation(operation) if executing_atomically? operation.each do |(name, hash)| @atomic_context[name] ||= {}
ruby
{ "resource": "" }
q12280
Mongoid.Persistable.persist_atomic_operations
train
def persist_atomic_operations(operations) if persisted? && operations && !operations.empty? selector = atomic_selector
ruby
{ "resource": "" }
q12281
Mongoid.Serializable.serializable_hash
train
def serializable_hash(options = nil) options ||= {} attrs = {} names = field_names(options) method_names = Array.wrap(options[:methods]).map do |name| name.to_s if respond_to?(name) end.compact (names + method_names).each do |name| without_autobuild do
ruby
{ "resource": "" }
q12282
Mongoid.Serializable.field_names
train
def field_names(options) names = (as_attributes.keys + attribute_names).uniq.sort only = Array.wrap(options[:only]).map(&:to_s) except = Array.wrap(options[:except]).map(&:to_s) except |= ['_type'] unless Mongoid.include_type_for_serialization
ruby
{ "resource": "" }
q12283
Mongoid.Serializable.serialize_attribute
train
def serialize_attribute(attrs, name, names, options) if relations.key?(name) value = send(name) attrs[name] = value ? value.serializable_hash(options) : nil elsif names.include?(name) && !fields.key?(name)
ruby
{ "resource": "" }
q12284
Mongoid.Serializable.serialize_relations
train
def serialize_relations(attributes = {}, options = {}) inclusions = options[:include] relation_names(inclusions).each do |name| association = relations[name.to_s] if association && relation = send(association.name)
ruby
{ "resource": "" }
q12285
Mongoid.Serializable.relation_names
train
def relation_names(inclusions) inclusions.is_a?(Hash) ? inclusions.keys
ruby
{ "resource": "" }
q12286
Mongoid.Serializable.relation_options
train
def relation_options(inclusions, options, name) if inclusions.is_a?(Hash) inclusions[name] else
ruby
{ "resource": "" }
q12287
Mongoid.Threaded.current_scope
train
def current_scope(klass = nil) if klass && Thread.current[CURRENT_SCOPE_KEY].respond_to?(:keys) Thread.current[CURRENT_SCOPE_KEY][
ruby
{ "resource": "" }
q12288
Mongoid.Threaded.set_current_scope
train
def set_current_scope(scope, klass) if scope.nil? if Thread.current[CURRENT_SCOPE_KEY] Thread.current[CURRENT_SCOPE_KEY].delete(klass)
ruby
{ "resource": "" }
q12289
Mongoid.Scopable.apply_default_scoping
train
def apply_default_scoping if default_scoping default_scoping.call.selector.each do
ruby
{ "resource": "" }
q12290
Mongoid.Validatable.read_attribute_for_validation
train
def read_attribute_for_validation(attr) attribute = database_field_name(attr) if relations.key?(attribute) begin_validate relation = without_autobuild
ruby
{ "resource": "" }
q12291
Mongoid.Traversable.collect_children
train
def collect_children children = [] embedded_relations.each_pair do |name, association| without_autobuild do child = send(name) Array.wrap(child).each do |doc| children.push(doc)
ruby
{ "resource": "" }
q12292
Mongoid.Traversable.remove_child
train
def remove_child(child) name = child.association_name if child.embedded_one? remove_ivar(name) else
ruby
{ "resource": "" }
q12293
Mongoid.Traversable.reset_persisted_children
train
def reset_persisted_children _children.each do |child| child.move_changes
ruby
{ "resource": "" }
q12294
Mongoid.Document.becomes
train
def becomes(klass) unless klass.include?(Mongoid::Document) raise ArgumentError, "A class which includes Mongoid::Document is expected" end became = klass.new(clone_document) became._id = _id became.instance_variable_set(:@changed_attributes, changed_attributes) became.insta...
ruby
{ "resource": "" }
q12295
Mongoid.Association.reload_relations
train
def reload_relations relations.each_pair do |name, meta| if instance_variable_defined?("@_#{name}") if _parent.nil? || instance_variable_get("@_#{name}") != _parent
ruby
{ "resource": "" }
q12296
Mongoid.Inspectable.inspect_fields
train
def inspect_fields fields.map do |name, field| unless name == "_id" as = field.options[:as] "#{name}#{as ? "(#{as})" :
ruby
{ "resource": "" }
q12297
Mongoid.Interceptable.run_callbacks
train
def run_callbacks(kind, *args, &block) cascadable_children(kind).each do |child| if child.run_callbacks(child_callback_type(kind, child), *args) == false return false
ruby
{ "resource": "" }
q12298
Mongoid.Interceptable.cascadable_children
train
def cascadable_children(kind, children = Set.new) embedded_relations.each_pair do |name, association| next unless association.cascading_callbacks? without_autobuild do delayed_pulls = delayed_atomic_pulls[name] delayed_unsets = delayed_atomic_unsets[name]
ruby
{ "resource": "" }
q12299
Mongoid.Interceptable.cascadable_child?
train
def cascadable_child?(kind, child, association) return false if kind == :initialize || kind == :find || kind == :touch return false if kind == :validate && association.validate?
ruby
{ "resource": "" }