id
int32 0
24.9k
| repo
stringlengths 5
58
| path
stringlengths 9
168
| func_name
stringlengths 9
130
| original_string
stringlengths 66
10.5k
| language
stringclasses 1
value | code
stringlengths 66
10.5k
| code_tokens
list | docstring
stringlengths 8
16k
| docstring_tokens
list | sha
stringlengths 40
40
| url
stringlengths 94
266
|
---|---|---|---|---|---|---|---|---|---|---|---|
21,500 |
ondra-m/ruby-spark
|
lib/spark/rdd.rb
|
Spark.RDD.subtract
|
def subtract(other, num_partitions=nil)
mapping_function = 'lambda{|x| [x,nil]}'
self.map(mapping_function)
.subtract_by_key(other.map(mapping_function), num_partitions)
.keys
end
|
ruby
|
def subtract(other, num_partitions=nil)
mapping_function = 'lambda{|x| [x,nil]}'
self.map(mapping_function)
.subtract_by_key(other.map(mapping_function), num_partitions)
.keys
end
|
[
"def",
"subtract",
"(",
"other",
",",
"num_partitions",
"=",
"nil",
")",
"mapping_function",
"=",
"'lambda{|x| [x,nil]}'",
"self",
".",
"map",
"(",
"mapping_function",
")",
".",
"subtract_by_key",
"(",
"other",
".",
"map",
"(",
"mapping_function",
")",
",",
"num_partitions",
")",
".",
"keys",
"end"
] |
Return an RDD with the elements from self that are not in other.
== Example:
rdd1 = $sc.parallelize([["a", 1], ["a", 2], ["b", 3], ["c", 4]])
rdd2 = $sc.parallelize([["a", 2], ["c", 6]])
rdd1.subtract(rdd2).collect
# => [["a", 1], ["b", 3], ["c", 4]]
|
[
"Return",
"an",
"RDD",
"with",
"the",
"elements",
"from",
"self",
"that",
"are",
"not",
"in",
"other",
"."
] |
d1b9787642fe582dee906de3c6bb9407ded27145
|
https://github.com/ondra-m/ruby-spark/blob/d1b9787642fe582dee906de3c6bb9407ded27145/lib/spark/rdd.rb#L1094-L1100
|
21,501 |
ondra-m/ruby-spark
|
lib/spark/rdd.rb
|
Spark.RDD.sort_by
|
def sort_by(key_function=nil, ascending=true, num_partitions=nil)
key_function ||= 'lambda{|x| x}'
num_partitions ||= default_reduce_partitions
command_klass = Spark::Command::SortByKey
# Allow spill data to disk due to memory limit
# spilling = config['spark.shuffle.spill'] || false
spilling = false
memory = ''
# Set spilling to false if worker has unlimited memory
if memory.empty?
spilling = false
memory = nil
else
memory = to_memory_size(memory)
end
# Sorting should do one worker
if num_partitions == 1
rdd = self
rdd = rdd.coalesce(1) if partitions_size > 1
return rdd.new_rdd_from_command(command_klass, key_function, ascending, spilling, memory, serializer)
end
# Compute boundary of collection
# Collection should be evenly distributed
# 20.0 is from scala RangePartitioner (for roughly balanced output partitions)
count = self.count
sample_size = num_partitions * 20.0
fraction = [sample_size / [count, 1].max, 1.0].min
samples = self.sample(false, fraction, 1).map(key_function).collect
samples.sort!
# Reverse is much faster than reverse sort_by
samples.reverse! if !ascending
# Determine part bounds
bounds = determine_bounds(samples, num_partitions)
shuffled = _partition_by(num_partitions, Spark::Command::PartitionBy::Sorting, key_function, bounds, ascending, num_partitions)
shuffled.new_rdd_from_command(command_klass, key_function, ascending, spilling, memory, serializer)
end
|
ruby
|
def sort_by(key_function=nil, ascending=true, num_partitions=nil)
key_function ||= 'lambda{|x| x}'
num_partitions ||= default_reduce_partitions
command_klass = Spark::Command::SortByKey
# Allow spill data to disk due to memory limit
# spilling = config['spark.shuffle.spill'] || false
spilling = false
memory = ''
# Set spilling to false if worker has unlimited memory
if memory.empty?
spilling = false
memory = nil
else
memory = to_memory_size(memory)
end
# Sorting should do one worker
if num_partitions == 1
rdd = self
rdd = rdd.coalesce(1) if partitions_size > 1
return rdd.new_rdd_from_command(command_klass, key_function, ascending, spilling, memory, serializer)
end
# Compute boundary of collection
# Collection should be evenly distributed
# 20.0 is from scala RangePartitioner (for roughly balanced output partitions)
count = self.count
sample_size = num_partitions * 20.0
fraction = [sample_size / [count, 1].max, 1.0].min
samples = self.sample(false, fraction, 1).map(key_function).collect
samples.sort!
# Reverse is much faster than reverse sort_by
samples.reverse! if !ascending
# Determine part bounds
bounds = determine_bounds(samples, num_partitions)
shuffled = _partition_by(num_partitions, Spark::Command::PartitionBy::Sorting, key_function, bounds, ascending, num_partitions)
shuffled.new_rdd_from_command(command_klass, key_function, ascending, spilling, memory, serializer)
end
|
[
"def",
"sort_by",
"(",
"key_function",
"=",
"nil",
",",
"ascending",
"=",
"true",
",",
"num_partitions",
"=",
"nil",
")",
"key_function",
"||=",
"'lambda{|x| x}'",
"num_partitions",
"||=",
"default_reduce_partitions",
"command_klass",
"=",
"Spark",
"::",
"Command",
"::",
"SortByKey",
"# Allow spill data to disk due to memory limit",
"# spilling = config['spark.shuffle.spill'] || false",
"spilling",
"=",
"false",
"memory",
"=",
"''",
"# Set spilling to false if worker has unlimited memory",
"if",
"memory",
".",
"empty?",
"spilling",
"=",
"false",
"memory",
"=",
"nil",
"else",
"memory",
"=",
"to_memory_size",
"(",
"memory",
")",
"end",
"# Sorting should do one worker",
"if",
"num_partitions",
"==",
"1",
"rdd",
"=",
"self",
"rdd",
"=",
"rdd",
".",
"coalesce",
"(",
"1",
")",
"if",
"partitions_size",
">",
"1",
"return",
"rdd",
".",
"new_rdd_from_command",
"(",
"command_klass",
",",
"key_function",
",",
"ascending",
",",
"spilling",
",",
"memory",
",",
"serializer",
")",
"end",
"# Compute boundary of collection",
"# Collection should be evenly distributed",
"# 20.0 is from scala RangePartitioner (for roughly balanced output partitions)",
"count",
"=",
"self",
".",
"count",
"sample_size",
"=",
"num_partitions",
"*",
"20.0",
"fraction",
"=",
"[",
"sample_size",
"/",
"[",
"count",
",",
"1",
"]",
".",
"max",
",",
"1.0",
"]",
".",
"min",
"samples",
"=",
"self",
".",
"sample",
"(",
"false",
",",
"fraction",
",",
"1",
")",
".",
"map",
"(",
"key_function",
")",
".",
"collect",
"samples",
".",
"sort!",
"# Reverse is much faster than reverse sort_by",
"samples",
".",
"reverse!",
"if",
"!",
"ascending",
"# Determine part bounds",
"bounds",
"=",
"determine_bounds",
"(",
"samples",
",",
"num_partitions",
")",
"shuffled",
"=",
"_partition_by",
"(",
"num_partitions",
",",
"Spark",
"::",
"Command",
"::",
"PartitionBy",
"::",
"Sorting",
",",
"key_function",
",",
"bounds",
",",
"ascending",
",",
"num_partitions",
")",
"shuffled",
".",
"new_rdd_from_command",
"(",
"command_klass",
",",
"key_function",
",",
"ascending",
",",
"spilling",
",",
"memory",
",",
"serializer",
")",
"end"
] |
Sorts this RDD by the given key_function
This is a different implementation than spark. Sort by doesn't use
key_by method first. It can be slower but take less memory and
you can always use map.sort_by_key
== Example:
rdd = $sc.parallelize(["aaaaaaa", "cc", "b", "eeee", "ddd"])
rdd.sort_by.collect
# => ["aaaaaaa", "b", "cc", "ddd", "eeee"]
rdd.sort_by(lambda{|x| x.size}).collect
# => ["b", "cc", "ddd", "eeee", "aaaaaaa"]
|
[
"Sorts",
"this",
"RDD",
"by",
"the",
"given",
"key_function"
] |
d1b9787642fe582dee906de3c6bb9407ded27145
|
https://github.com/ondra-m/ruby-spark/blob/d1b9787642fe582dee906de3c6bb9407ded27145/lib/spark/rdd.rb#L1139-L1181
|
21,502 |
ondra-m/ruby-spark
|
lib/spark/rdd.rb
|
Spark.RDD._reduce
|
def _reduce(klass, seq_op, comb_op, zero_value=nil)
if seq_op.nil?
# Partitions are already reduced
rdd = self
else
rdd = new_rdd_from_command(klass, seq_op, zero_value)
end
# Send all results to one worker and combine results
rdd = rdd.coalesce(1).compact
# Add the same function to new RDD
comm = rdd.add_command(klass, comb_op, zero_value)
comm.deserializer = @command.serializer
# Value is returned in array
PipelinedRDD.new(rdd, comm).collect[0]
end
|
ruby
|
def _reduce(klass, seq_op, comb_op, zero_value=nil)
if seq_op.nil?
# Partitions are already reduced
rdd = self
else
rdd = new_rdd_from_command(klass, seq_op, zero_value)
end
# Send all results to one worker and combine results
rdd = rdd.coalesce(1).compact
# Add the same function to new RDD
comm = rdd.add_command(klass, comb_op, zero_value)
comm.deserializer = @command.serializer
# Value is returned in array
PipelinedRDD.new(rdd, comm).collect[0]
end
|
[
"def",
"_reduce",
"(",
"klass",
",",
"seq_op",
",",
"comb_op",
",",
"zero_value",
"=",
"nil",
")",
"if",
"seq_op",
".",
"nil?",
"# Partitions are already reduced",
"rdd",
"=",
"self",
"else",
"rdd",
"=",
"new_rdd_from_command",
"(",
"klass",
",",
"seq_op",
",",
"zero_value",
")",
"end",
"# Send all results to one worker and combine results",
"rdd",
"=",
"rdd",
".",
"coalesce",
"(",
"1",
")",
".",
"compact",
"# Add the same function to new RDD",
"comm",
"=",
"rdd",
".",
"add_command",
"(",
"klass",
",",
"comb_op",
",",
"zero_value",
")",
"comm",
".",
"deserializer",
"=",
"@command",
".",
"serializer",
"# Value is returned in array",
"PipelinedRDD",
".",
"new",
"(",
"rdd",
",",
"comm",
")",
".",
"collect",
"[",
"0",
"]",
"end"
] |
This is base method for reduce operation. Is used by reduce, fold and aggregation.
Only difference is that fold has zero value.
|
[
"This",
"is",
"base",
"method",
"for",
"reduce",
"operation",
".",
"Is",
"used",
"by",
"reduce",
"fold",
"and",
"aggregation",
".",
"Only",
"difference",
"is",
"that",
"fold",
"has",
"zero",
"value",
"."
] |
d1b9787642fe582dee906de3c6bb9407ded27145
|
https://github.com/ondra-m/ruby-spark/blob/d1b9787642fe582dee906de3c6bb9407ded27145/lib/spark/rdd.rb#L1301-L1318
|
21,503 |
ondra-m/ruby-spark
|
lib/spark/rdd.rb
|
Spark.RDD._combine_by_key
|
def _combine_by_key(combine, merge, num_partitions)
num_partitions ||= default_reduce_partitions
# Combine key
combined = new_rdd_from_command(combine.shift, *combine)
# Merge items
shuffled = combined.partition_by(num_partitions)
merge_comm = shuffled.add_command(merge.shift, *merge)
PipelinedRDD.new(shuffled, merge_comm)
end
|
ruby
|
def _combine_by_key(combine, merge, num_partitions)
num_partitions ||= default_reduce_partitions
# Combine key
combined = new_rdd_from_command(combine.shift, *combine)
# Merge items
shuffled = combined.partition_by(num_partitions)
merge_comm = shuffled.add_command(merge.shift, *merge)
PipelinedRDD.new(shuffled, merge_comm)
end
|
[
"def",
"_combine_by_key",
"(",
"combine",
",",
"merge",
",",
"num_partitions",
")",
"num_partitions",
"||=",
"default_reduce_partitions",
"# Combine key",
"combined",
"=",
"new_rdd_from_command",
"(",
"combine",
".",
"shift",
",",
"combine",
")",
"# Merge items",
"shuffled",
"=",
"combined",
".",
"partition_by",
"(",
"num_partitions",
")",
"merge_comm",
"=",
"shuffled",
".",
"add_command",
"(",
"merge",
".",
"shift",
",",
"merge",
")",
"PipelinedRDD",
".",
"new",
"(",
"shuffled",
",",
"merge_comm",
")",
"end"
] |
For using a different combine_by_key
== Used for:
* combine_by_key
* fold_by_key (with zero value)
|
[
"For",
"using",
"a",
"different",
"combine_by_key"
] |
d1b9787642fe582dee906de3c6bb9407ded27145
|
https://github.com/ondra-m/ruby-spark/blob/d1b9787642fe582dee906de3c6bb9407ded27145/lib/spark/rdd.rb#L1341-L1352
|
21,504 |
ondra-m/ruby-spark
|
lib/spark/logger.rb
|
Spark.Logger.disable
|
def disable
jlogger.setLevel(level_off)
JLogger.getLogger('org').setLevel(level_off)
JLogger.getLogger('akka').setLevel(level_off)
JLogger.getRootLogger.setLevel(level_off)
end
|
ruby
|
def disable
jlogger.setLevel(level_off)
JLogger.getLogger('org').setLevel(level_off)
JLogger.getLogger('akka').setLevel(level_off)
JLogger.getRootLogger.setLevel(level_off)
end
|
[
"def",
"disable",
"jlogger",
".",
"setLevel",
"(",
"level_off",
")",
"JLogger",
".",
"getLogger",
"(",
"'org'",
")",
".",
"setLevel",
"(",
"level_off",
")",
"JLogger",
".",
"getLogger",
"(",
"'akka'",
")",
".",
"setLevel",
"(",
"level_off",
")",
"JLogger",
".",
"getRootLogger",
".",
"setLevel",
"(",
"level_off",
")",
"end"
] |
Disable all Spark log
|
[
"Disable",
"all",
"Spark",
"log"
] |
d1b9787642fe582dee906de3c6bb9407ded27145
|
https://github.com/ondra-m/ruby-spark/blob/d1b9787642fe582dee906de3c6bb9407ded27145/lib/spark/logger.rb#L18-L23
|
21,505 |
ondra-m/ruby-spark
|
lib/spark/context.rb
|
Spark.Context.accumulator
|
def accumulator(value, accum_param=:+, zero_value=0)
Spark::Accumulator.new(value, accum_param, zero_value)
end
|
ruby
|
def accumulator(value, accum_param=:+, zero_value=0)
Spark::Accumulator.new(value, accum_param, zero_value)
end
|
[
"def",
"accumulator",
"(",
"value",
",",
"accum_param",
"=",
":+",
",",
"zero_value",
"=",
"0",
")",
"Spark",
"::",
"Accumulator",
".",
"new",
"(",
"value",
",",
"accum_param",
",",
"zero_value",
")",
"end"
] |
Create an Accumulator with the given initial value, using a given
accum_param helper object to define how to add values of the
data type if provided.
== Example:
accum = $sc.accumulator(7)
rdd = $sc.parallelize(0..5, 4)
rdd = rdd.bind(accum: accum)
rdd = rdd.map_partitions(lambda{|_| accum.add(1) })
rdd = rdd.collect
accum.value
# => 11
|
[
"Create",
"an",
"Accumulator",
"with",
"the",
"given",
"initial",
"value",
"using",
"a",
"given",
"accum_param",
"helper",
"object",
"to",
"define",
"how",
"to",
"add",
"values",
"of",
"the",
"data",
"type",
"if",
"provided",
"."
] |
d1b9787642fe582dee906de3c6bb9407ded27145
|
https://github.com/ondra-m/ruby-spark/blob/d1b9787642fe582dee906de3c6bb9407ded27145/lib/spark/context.rb#L188-L190
|
21,506 |
ondra-m/ruby-spark
|
lib/spark/context.rb
|
Spark.Context.parallelize
|
def parallelize(data, num_slices=nil, serializer=nil)
num_slices ||= default_parallelism
serializer ||= default_serializer
serializer.check_each(data)
# Through file
file = Tempfile.new('to_parallelize', temp_dir)
serializer.dump_to_io(data, file)
file.close # not unlink
jrdd = RubyRDD.readRDDFromFile(jcontext, file.path, num_slices)
Spark::RDD.new(jrdd, self, serializer)
ensure
file && file.unlink
end
|
ruby
|
def parallelize(data, num_slices=nil, serializer=nil)
num_slices ||= default_parallelism
serializer ||= default_serializer
serializer.check_each(data)
# Through file
file = Tempfile.new('to_parallelize', temp_dir)
serializer.dump_to_io(data, file)
file.close # not unlink
jrdd = RubyRDD.readRDDFromFile(jcontext, file.path, num_slices)
Spark::RDD.new(jrdd, self, serializer)
ensure
file && file.unlink
end
|
[
"def",
"parallelize",
"(",
"data",
",",
"num_slices",
"=",
"nil",
",",
"serializer",
"=",
"nil",
")",
"num_slices",
"||=",
"default_parallelism",
"serializer",
"||=",
"default_serializer",
"serializer",
".",
"check_each",
"(",
"data",
")",
"# Through file",
"file",
"=",
"Tempfile",
".",
"new",
"(",
"'to_parallelize'",
",",
"temp_dir",
")",
"serializer",
".",
"dump_to_io",
"(",
"data",
",",
"file",
")",
"file",
".",
"close",
"# not unlink",
"jrdd",
"=",
"RubyRDD",
".",
"readRDDFromFile",
"(",
"jcontext",
",",
"file",
".",
"path",
",",
"num_slices",
")",
"Spark",
"::",
"RDD",
".",
"new",
"(",
"jrdd",
",",
"self",
",",
"serializer",
")",
"ensure",
"file",
"&&",
"file",
".",
"unlink",
"end"
] |
Distribute a local Ruby collection to form an RDD
Direct method can be slow so be careful, this method update data inplace
== Parameters:
data:: Range or Array
num_slices:: number of slice
serializer:: custom serializer (default: serializer based on configuration)
== Examples:
$sc.parallelize(["1", "2", "3"]).map(lambda{|x| x.to_i}).collect
#=> [1, 2, 3]
$sc.parallelize(1..3).map(:to_s).collect
#=> ["1", "2", "3"]
|
[
"Distribute",
"a",
"local",
"Ruby",
"collection",
"to",
"form",
"an",
"RDD",
"Direct",
"method",
"can",
"be",
"slow",
"so",
"be",
"careful",
"this",
"method",
"update",
"data",
"inplace"
] |
d1b9787642fe582dee906de3c6bb9407ded27145
|
https://github.com/ondra-m/ruby-spark/blob/d1b9787642fe582dee906de3c6bb9407ded27145/lib/spark/context.rb#L207-L222
|
21,507 |
ondra-m/ruby-spark
|
lib/spark/context.rb
|
Spark.Context.run_job
|
def run_job(rdd, f, partitions=nil, allow_local=false)
run_job_with_command(rdd, partitions, allow_local, Spark::Command::MapPartitions, f)
end
|
ruby
|
def run_job(rdd, f, partitions=nil, allow_local=false)
run_job_with_command(rdd, partitions, allow_local, Spark::Command::MapPartitions, f)
end
|
[
"def",
"run_job",
"(",
"rdd",
",",
"f",
",",
"partitions",
"=",
"nil",
",",
"allow_local",
"=",
"false",
")",
"run_job_with_command",
"(",
"rdd",
",",
"partitions",
",",
"allow_local",
",",
"Spark",
"::",
"Command",
"::",
"MapPartitions",
",",
"f",
")",
"end"
] |
Executes the given partition function f on the specified set of partitions,
returning the result as an array of elements.
If partitions is not specified, this will run over all partitions.
== Example:
rdd = $sc.parallelize(0..10, 5)
$sc.run_job(rdd, lambda{|x| x.to_s}, [0,2])
# => ["[0, 1]", "[4, 5]"]
|
[
"Executes",
"the",
"given",
"partition",
"function",
"f",
"on",
"the",
"specified",
"set",
"of",
"partitions",
"returning",
"the",
"result",
"as",
"an",
"array",
"of",
"elements",
"."
] |
d1b9787642fe582dee906de3c6bb9407ded27145
|
https://github.com/ondra-m/ruby-spark/blob/d1b9787642fe582dee906de3c6bb9407ded27145/lib/spark/context.rb#L278-L280
|
21,508 |
ondra-m/ruby-spark
|
lib/spark/context.rb
|
Spark.Context.run_job_with_command
|
def run_job_with_command(rdd, partitions, allow_local, command, *args)
if !partitions.nil? && !partitions.is_a?(Array)
raise Spark::ContextError, 'Partitions must be nil or Array'
end
partitions_size = rdd.partitions_size
# Execute all parts
if partitions.nil?
partitions = (0...partitions_size).to_a
end
# Can happend when you use coalesce
partitions.delete_if {|part| part >= partitions_size}
# Rjb represent Fixnum as Integer but Jruby as Long
partitions = to_java_array_list(convert_to_java_int(partitions))
# File for result
file = Tempfile.new('collect', temp_dir)
mapped = rdd.new_rdd_from_command(command, *args)
RubyRDD.runJob(rdd.context.sc, mapped.jrdd, partitions, allow_local, file.path)
mapped.collect_from_file(file)
end
|
ruby
|
def run_job_with_command(rdd, partitions, allow_local, command, *args)
if !partitions.nil? && !partitions.is_a?(Array)
raise Spark::ContextError, 'Partitions must be nil or Array'
end
partitions_size = rdd.partitions_size
# Execute all parts
if partitions.nil?
partitions = (0...partitions_size).to_a
end
# Can happend when you use coalesce
partitions.delete_if {|part| part >= partitions_size}
# Rjb represent Fixnum as Integer but Jruby as Long
partitions = to_java_array_list(convert_to_java_int(partitions))
# File for result
file = Tempfile.new('collect', temp_dir)
mapped = rdd.new_rdd_from_command(command, *args)
RubyRDD.runJob(rdd.context.sc, mapped.jrdd, partitions, allow_local, file.path)
mapped.collect_from_file(file)
end
|
[
"def",
"run_job_with_command",
"(",
"rdd",
",",
"partitions",
",",
"allow_local",
",",
"command",
",",
"*",
"args",
")",
"if",
"!",
"partitions",
".",
"nil?",
"&&",
"!",
"partitions",
".",
"is_a?",
"(",
"Array",
")",
"raise",
"Spark",
"::",
"ContextError",
",",
"'Partitions must be nil or Array'",
"end",
"partitions_size",
"=",
"rdd",
".",
"partitions_size",
"# Execute all parts",
"if",
"partitions",
".",
"nil?",
"partitions",
"=",
"(",
"0",
"...",
"partitions_size",
")",
".",
"to_a",
"end",
"# Can happend when you use coalesce",
"partitions",
".",
"delete_if",
"{",
"|",
"part",
"|",
"part",
">=",
"partitions_size",
"}",
"# Rjb represent Fixnum as Integer but Jruby as Long",
"partitions",
"=",
"to_java_array_list",
"(",
"convert_to_java_int",
"(",
"partitions",
")",
")",
"# File for result",
"file",
"=",
"Tempfile",
".",
"new",
"(",
"'collect'",
",",
"temp_dir",
")",
"mapped",
"=",
"rdd",
".",
"new_rdd_from_command",
"(",
"command",
",",
"args",
")",
"RubyRDD",
".",
"runJob",
"(",
"rdd",
".",
"context",
".",
"sc",
",",
"mapped",
".",
"jrdd",
",",
"partitions",
",",
"allow_local",
",",
"file",
".",
"path",
")",
"mapped",
".",
"collect_from_file",
"(",
"file",
")",
"end"
] |
Execute the given command on specific set of partitions.
|
[
"Execute",
"the",
"given",
"command",
"on",
"specific",
"set",
"of",
"partitions",
"."
] |
d1b9787642fe582dee906de3c6bb9407ded27145
|
https://github.com/ondra-m/ruby-spark/blob/d1b9787642fe582dee906de3c6bb9407ded27145/lib/spark/context.rb#L284-L309
|
21,509 |
ondra-m/ruby-spark
|
lib/spark/stat_counter.rb
|
Spark.StatCounter.merge
|
def merge(other)
if other.is_a?(Spark::StatCounter)
merge_stat_counter(other)
elsif other.respond_to?(:each)
merge_array(other)
else
merge_value(other)
end
self
end
|
ruby
|
def merge(other)
if other.is_a?(Spark::StatCounter)
merge_stat_counter(other)
elsif other.respond_to?(:each)
merge_array(other)
else
merge_value(other)
end
self
end
|
[
"def",
"merge",
"(",
"other",
")",
"if",
"other",
".",
"is_a?",
"(",
"Spark",
"::",
"StatCounter",
")",
"merge_stat_counter",
"(",
"other",
")",
"elsif",
"other",
".",
"respond_to?",
"(",
":each",
")",
"merge_array",
"(",
"other",
")",
"else",
"merge_value",
"(",
"other",
")",
"end",
"self",
"end"
] |
min of our values
|
[
"min",
"of",
"our",
"values"
] |
d1b9787642fe582dee906de3c6bb9407ded27145
|
https://github.com/ondra-m/ruby-spark/blob/d1b9787642fe582dee906de3c6bb9407ded27145/lib/spark/stat_counter.rb#L20-L30
|
21,510 |
magnetis/caze
|
lib/caze.rb
|
Caze.ClassMethods.raise_use_case_error
|
def raise_use_case_error(use_case, error)
name = error.class.name.split('::').last
klass = define_use_case_error(use_case, name)
wrapped = klass.new(error.message)
wrapped.set_backtrace(error.backtrace)
raise wrapped
end
|
ruby
|
def raise_use_case_error(use_case, error)
name = error.class.name.split('::').last
klass = define_use_case_error(use_case, name)
wrapped = klass.new(error.message)
wrapped.set_backtrace(error.backtrace)
raise wrapped
end
|
[
"def",
"raise_use_case_error",
"(",
"use_case",
",",
"error",
")",
"name",
"=",
"error",
".",
"class",
".",
"name",
".",
"split",
"(",
"'::'",
")",
".",
"last",
"klass",
"=",
"define_use_case_error",
"(",
"use_case",
",",
"name",
")",
"wrapped",
"=",
"klass",
".",
"new",
"(",
"error",
".",
"message",
")",
"wrapped",
".",
"set_backtrace",
"(",
"error",
".",
"backtrace",
")",
"raise",
"wrapped",
"end"
] |
This method intends to inject the error inside the context of the use case,
so we can identify the use case from it was raised
|
[
"This",
"method",
"intends",
"to",
"inject",
"the",
"error",
"inside",
"the",
"context",
"of",
"the",
"use",
"case",
"so",
"we",
"can",
"identify",
"the",
"use",
"case",
"from",
"it",
"was",
"raised"
] |
04a827f1efa1ad7a5ab91ce7cd2644f92b5621e0
|
https://github.com/magnetis/caze/blob/04a827f1efa1ad7a5ab91ce7cd2644f92b5621e0/lib/caze.rb#L66-L74
|
21,511 |
mbryzek/schema-evolution-manager
|
lib/schema-evolution-manager/db.rb
|
SchemaEvolutionManager.Db.bootstrap!
|
def bootstrap!
scripts = Scripts.new(self, Scripts::BOOTSTRAP_SCRIPTS)
dir = File.join(Library.base_dir, "scripts")
scripts.each_pending(dir) do |filename, path|
psql_file(filename, path)
scripts.record_as_run!(filename)
end
end
|
ruby
|
def bootstrap!
scripts = Scripts.new(self, Scripts::BOOTSTRAP_SCRIPTS)
dir = File.join(Library.base_dir, "scripts")
scripts.each_pending(dir) do |filename, path|
psql_file(filename, path)
scripts.record_as_run!(filename)
end
end
|
[
"def",
"bootstrap!",
"scripts",
"=",
"Scripts",
".",
"new",
"(",
"self",
",",
"Scripts",
"::",
"BOOTSTRAP_SCRIPTS",
")",
"dir",
"=",
"File",
".",
"join",
"(",
"Library",
".",
"base_dir",
",",
"\"scripts\"",
")",
"scripts",
".",
"each_pending",
"(",
"dir",
")",
"do",
"|",
"filename",
",",
"path",
"|",
"psql_file",
"(",
"filename",
",",
"path",
")",
"scripts",
".",
"record_as_run!",
"(",
"filename",
")",
"end",
"end"
] |
Installs schema_evolution_manager. Automatically upgrades schema_evolution_manager.
|
[
"Installs",
"schema_evolution_manager",
".",
"Automatically",
"upgrades",
"schema_evolution_manager",
"."
] |
eda4f9bd653c2248492b43256daf5e2a41bfff7e
|
https://github.com/mbryzek/schema-evolution-manager/blob/eda4f9bd653c2248492b43256daf5e2a41bfff7e/lib/schema-evolution-manager/db.rb#L19-L26
|
21,512 |
mbryzek/schema-evolution-manager
|
lib/schema-evolution-manager/db.rb
|
SchemaEvolutionManager.Db.psql_command
|
def psql_command(sql_command)
Preconditions.assert_class(sql_command, String)
command = "psql --no-align --tuples-only --no-psqlrc --command \"%s\" %s" % [sql_command, @url]
Library.system_or_error(command)
end
|
ruby
|
def psql_command(sql_command)
Preconditions.assert_class(sql_command, String)
command = "psql --no-align --tuples-only --no-psqlrc --command \"%s\" %s" % [sql_command, @url]
Library.system_or_error(command)
end
|
[
"def",
"psql_command",
"(",
"sql_command",
")",
"Preconditions",
".",
"assert_class",
"(",
"sql_command",
",",
"String",
")",
"command",
"=",
"\"psql --no-align --tuples-only --no-psqlrc --command \\\"%s\\\" %s\"",
"%",
"[",
"sql_command",
",",
"@url",
"]",
"Library",
".",
"system_or_error",
"(",
"command",
")",
"end"
] |
executes a simple sql command.
|
[
"executes",
"a",
"simple",
"sql",
"command",
"."
] |
eda4f9bd653c2248492b43256daf5e2a41bfff7e
|
https://github.com/mbryzek/schema-evolution-manager/blob/eda4f9bd653c2248492b43256daf5e2a41bfff7e/lib/schema-evolution-manager/db.rb#L29-L33
|
21,513 |
mbryzek/schema-evolution-manager
|
lib/schema-evolution-manager/migration_file.rb
|
SchemaEvolutionManager.MigrationFile.parse_attribute_values
|
def parse_attribute_values
values = []
each_property do |name, value|
values << AttributeValue.new(name, value)
end
DEFAULTS.each do |default|
if values.find { |v| v.attribute.name == default.attribute.name }.nil?
values << default
end
end
values
end
|
ruby
|
def parse_attribute_values
values = []
each_property do |name, value|
values << AttributeValue.new(name, value)
end
DEFAULTS.each do |default|
if values.find { |v| v.attribute.name == default.attribute.name }.nil?
values << default
end
end
values
end
|
[
"def",
"parse_attribute_values",
"values",
"=",
"[",
"]",
"each_property",
"do",
"|",
"name",
",",
"value",
"|",
"values",
"<<",
"AttributeValue",
".",
"new",
"(",
"name",
",",
"value",
")",
"end",
"DEFAULTS",
".",
"each",
"do",
"|",
"default",
"|",
"if",
"values",
".",
"find",
"{",
"|",
"v",
"|",
"v",
".",
"attribute",
".",
"name",
"==",
"default",
".",
"attribute",
".",
"name",
"}",
".",
"nil?",
"values",
"<<",
"default",
"end",
"end",
"values",
"end"
] |
Returns a list of AttributeValues from the file itself,
including all defaults set by SEM. AttributeValues are defined
in comments in the file.
|
[
"Returns",
"a",
"list",
"of",
"AttributeValues",
"from",
"the",
"file",
"itself",
"including",
"all",
"defaults",
"set",
"by",
"SEM",
".",
"AttributeValues",
"are",
"defined",
"in",
"comments",
"in",
"the",
"file",
"."
] |
eda4f9bd653c2248492b43256daf5e2a41bfff7e
|
https://github.com/mbryzek/schema-evolution-manager/blob/eda4f9bd653c2248492b43256daf5e2a41bfff7e/lib/schema-evolution-manager/migration_file.rb#L60-L73
|
21,514 |
mbryzek/schema-evolution-manager
|
lib/schema-evolution-manager/scripts.rb
|
SchemaEvolutionManager.Scripts.each_pending
|
def each_pending(dir)
files = {}
Scripts.all(dir).each do |path|
name = File.basename(path)
files[name] = path
end
scripts_previously_run(files.keys).each do |filename|
files.delete(filename)
end
files.keys.sort.each do |filename|
## We have to recheck if this script is still pending. Some
## upgrade scripts may modify the scripts table themselves. This
## is actually useful in cases like when we migrated gilt from
## util_schema => schema_evolution_manager schema
if !has_run?(filename)
yield filename, files[filename]
end
end
end
|
ruby
|
def each_pending(dir)
files = {}
Scripts.all(dir).each do |path|
name = File.basename(path)
files[name] = path
end
scripts_previously_run(files.keys).each do |filename|
files.delete(filename)
end
files.keys.sort.each do |filename|
## We have to recheck if this script is still pending. Some
## upgrade scripts may modify the scripts table themselves. This
## is actually useful in cases like when we migrated gilt from
## util_schema => schema_evolution_manager schema
if !has_run?(filename)
yield filename, files[filename]
end
end
end
|
[
"def",
"each_pending",
"(",
"dir",
")",
"files",
"=",
"{",
"}",
"Scripts",
".",
"all",
"(",
"dir",
")",
".",
"each",
"do",
"|",
"path",
"|",
"name",
"=",
"File",
".",
"basename",
"(",
"path",
")",
"files",
"[",
"name",
"]",
"=",
"path",
"end",
"scripts_previously_run",
"(",
"files",
".",
"keys",
")",
".",
"each",
"do",
"|",
"filename",
"|",
"files",
".",
"delete",
"(",
"filename",
")",
"end",
"files",
".",
"keys",
".",
"sort",
".",
"each",
"do",
"|",
"filename",
"|",
"## We have to recheck if this script is still pending. Some",
"## upgrade scripts may modify the scripts table themselves. This",
"## is actually useful in cases like when we migrated gilt from",
"## util_schema => schema_evolution_manager schema",
"if",
"!",
"has_run?",
"(",
"filename",
")",
"yield",
"filename",
",",
"files",
"[",
"filename",
"]",
"end",
"end",
"end"
] |
For each sql script that needs to be applied to this database,
yields a pair of |filename, fullpath| in proper order
db = Db.new(host, user, name)
scripts = Scripts.new(db)
scripts.each_pending do |filename, path|
puts filename
end
|
[
"For",
"each",
"sql",
"script",
"that",
"needs",
"to",
"be",
"applied",
"to",
"this",
"database",
"yields",
"a",
"pair",
"of",
"|filename",
"fullpath|",
"in",
"proper",
"order"
] |
eda4f9bd653c2248492b43256daf5e2a41bfff7e
|
https://github.com/mbryzek/schema-evolution-manager/blob/eda4f9bd653c2248492b43256daf5e2a41bfff7e/lib/schema-evolution-manager/scripts.rb#L41-L61
|
21,515 |
mbryzek/schema-evolution-manager
|
lib/schema-evolution-manager/scripts.rb
|
SchemaEvolutionManager.Scripts.has_run?
|
def has_run?(filename)
if @db.schema_schema_evolution_manager_exists?
query = "select count(*) from %s.%s where filename = '%s'" % [Db.schema_name, @table_name, filename]
@db.psql_command(query).to_i > 0
else
false
end
end
|
ruby
|
def has_run?(filename)
if @db.schema_schema_evolution_manager_exists?
query = "select count(*) from %s.%s where filename = '%s'" % [Db.schema_name, @table_name, filename]
@db.psql_command(query).to_i > 0
else
false
end
end
|
[
"def",
"has_run?",
"(",
"filename",
")",
"if",
"@db",
".",
"schema_schema_evolution_manager_exists?",
"query",
"=",
"\"select count(*) from %s.%s where filename = '%s'\"",
"%",
"[",
"Db",
".",
"schema_name",
",",
"@table_name",
",",
"filename",
"]",
"@db",
".",
"psql_command",
"(",
"query",
")",
".",
"to_i",
">",
"0",
"else",
"false",
"end",
"end"
] |
True if this script has already been applied to the db. False
otherwise.
|
[
"True",
"if",
"this",
"script",
"has",
"already",
"been",
"applied",
"to",
"the",
"db",
".",
"False",
"otherwise",
"."
] |
eda4f9bd653c2248492b43256daf5e2a41bfff7e
|
https://github.com/mbryzek/schema-evolution-manager/blob/eda4f9bd653c2248492b43256daf5e2a41bfff7e/lib/schema-evolution-manager/scripts.rb#L65-L72
|
21,516 |
mbryzek/schema-evolution-manager
|
lib/schema-evolution-manager/scripts.rb
|
SchemaEvolutionManager.Scripts.record_as_run!
|
def record_as_run!(filename)
Preconditions.check_state(filename.match(/^\d\d\d\d\d\d+\-\d\d\d\d\d\d\.sql$/),
"Invalid filename[#{filename}]. Must be like: 20120503-173242.sql")
command = "insert into %s.%s (filename) select '%s' where not exists (select 1 from %s.%s where filename = '%s')" % [Db.schema_name, @table_name, filename, Db.schema_name, @table_name, filename]
@db.psql_command(command)
end
|
ruby
|
def record_as_run!(filename)
Preconditions.check_state(filename.match(/^\d\d\d\d\d\d+\-\d\d\d\d\d\d\.sql$/),
"Invalid filename[#{filename}]. Must be like: 20120503-173242.sql")
command = "insert into %s.%s (filename) select '%s' where not exists (select 1 from %s.%s where filename = '%s')" % [Db.schema_name, @table_name, filename, Db.schema_name, @table_name, filename]
@db.psql_command(command)
end
|
[
"def",
"record_as_run!",
"(",
"filename",
")",
"Preconditions",
".",
"check_state",
"(",
"filename",
".",
"match",
"(",
"/",
"\\d",
"\\d",
"\\d",
"\\d",
"\\d",
"\\d",
"\\-",
"\\d",
"\\d",
"\\d",
"\\d",
"\\d",
"\\d",
"\\.",
"/",
")",
",",
"\"Invalid filename[#{filename}]. Must be like: 20120503-173242.sql\"",
")",
"command",
"=",
"\"insert into %s.%s (filename) select '%s' where not exists (select 1 from %s.%s where filename = '%s')\"",
"%",
"[",
"Db",
".",
"schema_name",
",",
"@table_name",
",",
"filename",
",",
"Db",
".",
"schema_name",
",",
"@table_name",
",",
"filename",
"]",
"@db",
".",
"psql_command",
"(",
"command",
")",
"end"
] |
Inserts a record to indiciate that we have loaded the specified file.
|
[
"Inserts",
"a",
"record",
"to",
"indiciate",
"that",
"we",
"have",
"loaded",
"the",
"specified",
"file",
"."
] |
eda4f9bd653c2248492b43256daf5e2a41bfff7e
|
https://github.com/mbryzek/schema-evolution-manager/blob/eda4f9bd653c2248492b43256daf5e2a41bfff7e/lib/schema-evolution-manager/scripts.rb#L75-L80
|
21,517 |
mbryzek/schema-evolution-manager
|
lib/schema-evolution-manager/scripts.rb
|
SchemaEvolutionManager.Scripts.scripts_previously_run
|
def scripts_previously_run(scripts)
if scripts.empty? || [email protected]_schema_evolution_manager_exists?
[]
else
sql = "select filename from %s.%s where filename in (%s)" % [Db.schema_name, @table_name, "'" + scripts.join("', '") + "'"]
@db.psql_command(sql).strip.split
end
end
|
ruby
|
def scripts_previously_run(scripts)
if scripts.empty? || [email protected]_schema_evolution_manager_exists?
[]
else
sql = "select filename from %s.%s where filename in (%s)" % [Db.schema_name, @table_name, "'" + scripts.join("', '") + "'"]
@db.psql_command(sql).strip.split
end
end
|
[
"def",
"scripts_previously_run",
"(",
"scripts",
")",
"if",
"scripts",
".",
"empty?",
"||",
"!",
"@db",
".",
"schema_schema_evolution_manager_exists?",
"[",
"]",
"else",
"sql",
"=",
"\"select filename from %s.%s where filename in (%s)\"",
"%",
"[",
"Db",
".",
"schema_name",
",",
"@table_name",
",",
"\"'\"",
"+",
"scripts",
".",
"join",
"(",
"\"', '\"",
")",
"+",
"\"'\"",
"]",
"@db",
".",
"psql_command",
"(",
"sql",
")",
".",
"strip",
".",
"split",
"end",
"end"
] |
Fetch the list of scripts that have already been applied to this
database.
|
[
"Fetch",
"the",
"list",
"of",
"scripts",
"that",
"have",
"already",
"been",
"applied",
"to",
"this",
"database",
"."
] |
eda4f9bd653c2248492b43256daf5e2a41bfff7e
|
https://github.com/mbryzek/schema-evolution-manager/blob/eda4f9bd653c2248492b43256daf5e2a41bfff7e/lib/schema-evolution-manager/scripts.rb#L85-L92
|
21,518 |
mbryzek/schema-evolution-manager
|
lib/schema-evolution-manager/install_template.rb
|
SchemaEvolutionManager.InstallTemplate.generate
|
def generate
template = Template.new
template.add('timestamp', Time.now.to_s)
template.add('lib_dir', @lib_dir)
template.add('bin_dir', @bin_dir)
template.parse(TEMPLATE)
end
|
ruby
|
def generate
template = Template.new
template.add('timestamp', Time.now.to_s)
template.add('lib_dir', @lib_dir)
template.add('bin_dir', @bin_dir)
template.parse(TEMPLATE)
end
|
[
"def",
"generate",
"template",
"=",
"Template",
".",
"new",
"template",
".",
"add",
"(",
"'timestamp'",
",",
"Time",
".",
"now",
".",
"to_s",
")",
"template",
".",
"add",
"(",
"'lib_dir'",
",",
"@lib_dir",
")",
"template",
".",
"add",
"(",
"'bin_dir'",
",",
"@bin_dir",
")",
"template",
".",
"parse",
"(",
"TEMPLATE",
")",
"end"
] |
Generates the actual contents of the install file
|
[
"Generates",
"the",
"actual",
"contents",
"of",
"the",
"install",
"file"
] |
eda4f9bd653c2248492b43256daf5e2a41bfff7e
|
https://github.com/mbryzek/schema-evolution-manager/blob/eda4f9bd653c2248492b43256daf5e2a41bfff7e/lib/schema-evolution-manager/install_template.rb#L12-L18
|
21,519 |
yohasebe/wp2txt
|
lib/wp2txt.rb
|
Wp2txt.Runner.fill_buffer
|
def fill_buffer
while true do
begin
new_lines = @file_pointer.read(10485760)
rescue => e
return nil
end
return nil unless new_lines
# temp_buf is filled with text split by "\n"
temp_buf = []
ss = StringScanner.new(new_lines)
while ss.scan(/.*?\n/m)
temp_buf << ss[0]
end
temp_buf << ss.rest unless ss.eos?
new_first_line = temp_buf.shift
if new_first_line[-1, 1] == "\n" # new_first_line.index("\n")
@buffer.last << new_first_line
@buffer << ""
else
@buffer.last << new_first_line
end
@buffer += temp_buf unless temp_buf.empty?
if @buffer.last[-1, 1] == "\n" # @buffer.last.index("\n")
@buffer << ""
end
break if @buffer.size > 1
end
return true
end
|
ruby
|
def fill_buffer
while true do
begin
new_lines = @file_pointer.read(10485760)
rescue => e
return nil
end
return nil unless new_lines
# temp_buf is filled with text split by "\n"
temp_buf = []
ss = StringScanner.new(new_lines)
while ss.scan(/.*?\n/m)
temp_buf << ss[0]
end
temp_buf << ss.rest unless ss.eos?
new_first_line = temp_buf.shift
if new_first_line[-1, 1] == "\n" # new_first_line.index("\n")
@buffer.last << new_first_line
@buffer << ""
else
@buffer.last << new_first_line
end
@buffer += temp_buf unless temp_buf.empty?
if @buffer.last[-1, 1] == "\n" # @buffer.last.index("\n")
@buffer << ""
end
break if @buffer.size > 1
end
return true
end
|
[
"def",
"fill_buffer",
"while",
"true",
"do",
"begin",
"new_lines",
"=",
"@file_pointer",
".",
"read",
"(",
"10485760",
")",
"rescue",
"=>",
"e",
"return",
"nil",
"end",
"return",
"nil",
"unless",
"new_lines",
"# temp_buf is filled with text split by \"\\n\"\r",
"temp_buf",
"=",
"[",
"]",
"ss",
"=",
"StringScanner",
".",
"new",
"(",
"new_lines",
")",
"while",
"ss",
".",
"scan",
"(",
"/",
"\\n",
"/m",
")",
"temp_buf",
"<<",
"ss",
"[",
"0",
"]",
"end",
"temp_buf",
"<<",
"ss",
".",
"rest",
"unless",
"ss",
".",
"eos?",
"new_first_line",
"=",
"temp_buf",
".",
"shift",
"if",
"new_first_line",
"[",
"-",
"1",
",",
"1",
"]",
"==",
"\"\\n\"",
"# new_first_line.index(\"\\n\")\r",
"@buffer",
".",
"last",
"<<",
"new_first_line",
"@buffer",
"<<",
"\"\"",
"else",
"@buffer",
".",
"last",
"<<",
"new_first_line",
"end",
"@buffer",
"+=",
"temp_buf",
"unless",
"temp_buf",
".",
"empty?",
"if",
"@buffer",
".",
"last",
"[",
"-",
"1",
",",
"1",
"]",
"==",
"\"\\n\"",
"# @buffer.last.index(\"\\n\")\r",
"@buffer",
"<<",
"\"\"",
"end",
"break",
"if",
"@buffer",
".",
"size",
">",
"1",
"end",
"return",
"true",
"end"
] |
read text data from bz2 compressed file by 1 megabyte
|
[
"read",
"text",
"data",
"from",
"bz2",
"compressed",
"file",
"by",
"1",
"megabyte"
] |
0938ddd3b076611ce37c8f9decc58cf97589c06c
|
https://github.com/yohasebe/wp2txt/blob/0938ddd3b076611ce37c8f9decc58cf97589c06c/lib/wp2txt.rb#L146-L177
|
21,520 |
chrisvfritz/language_filter
|
lib/language_filter.rb
|
LanguageFilter.Filter.replace
|
def replace(word)
case @replacement
when :vowels then word.gsub(/[aeiou]/i, '*')
when :stars then '*' * word.size
when :nonconsonants then word.gsub(/[^bcdfghjklmnpqrstvwxyz]/i, '*')
when :default, :garbled then '$@!#%'
else raise LanguageFilter::UnknownReplacement.new("#{@replacement} is not a known replacement type.")
end
end
|
ruby
|
def replace(word)
case @replacement
when :vowels then word.gsub(/[aeiou]/i, '*')
when :stars then '*' * word.size
when :nonconsonants then word.gsub(/[^bcdfghjklmnpqrstvwxyz]/i, '*')
when :default, :garbled then '$@!#%'
else raise LanguageFilter::UnknownReplacement.new("#{@replacement} is not a known replacement type.")
end
end
|
[
"def",
"replace",
"(",
"word",
")",
"case",
"@replacement",
"when",
":vowels",
"then",
"word",
".",
"gsub",
"(",
"/",
"/i",
",",
"'*'",
")",
"when",
":stars",
"then",
"'*'",
"*",
"word",
".",
"size",
"when",
":nonconsonants",
"then",
"word",
".",
"gsub",
"(",
"/",
"/i",
",",
"'*'",
")",
"when",
":default",
",",
":garbled",
"then",
"'$@!#%'",
"else",
"raise",
"LanguageFilter",
"::",
"UnknownReplacement",
".",
"new",
"(",
"\"#{@replacement} is not a known replacement type.\"",
")",
"end",
"end"
] |
This was moved to private because users should just use sanitize for any content
|
[
"This",
"was",
"moved",
"to",
"private",
"because",
"users",
"should",
"just",
"use",
"sanitize",
"for",
"any",
"content"
] |
084fe0654e4cb18e0a145fad1006dcf98bf4011c
|
https://github.com/chrisvfritz/language_filter/blob/084fe0654e4cb18e0a145fad1006dcf98bf4011c/lib/language_filter.rb#L258-L266
|
21,521 |
benbalter/jekyll-readme-index
|
lib/jekyll/static_file_ext.rb
|
Jekyll.StaticFile.to_page
|
def to_page
page = Jekyll::Page.new(@site, @base, @dir, @name)
page.data["permalink"] = File.dirname(url) + "/"
page
end
|
ruby
|
def to_page
page = Jekyll::Page.new(@site, @base, @dir, @name)
page.data["permalink"] = File.dirname(url) + "/"
page
end
|
[
"def",
"to_page",
"page",
"=",
"Jekyll",
"::",
"Page",
".",
"new",
"(",
"@site",
",",
"@base",
",",
"@dir",
",",
"@name",
")",
"page",
".",
"data",
"[",
"\"permalink\"",
"]",
"=",
"File",
".",
"dirname",
"(",
"url",
")",
"+",
"\"/\"",
"page",
"end"
] |
Convert this static file to a Page
|
[
"Convert",
"this",
"static",
"file",
"to",
"a",
"Page"
] |
19897e9250c3e410c5fc461b6f5737902bb7208f
|
https://github.com/benbalter/jekyll-readme-index/blob/19897e9250c3e410c5fc461b6f5737902bb7208f/lib/jekyll/static_file_ext.rb#L6-L10
|
21,522 |
sue445/apple_system_status
|
lib/apple_system_status/crawler.rb
|
AppleSystemStatus.Crawler.perform
|
def perform(country: nil, title: nil)
@session.visit(apple_url(country))
response = {
title: @session.find(".section-date .date-copy").text.strip,
services: [],
}
MAX_RETRY_COUNT.times do
services = fetch_services
if services.empty?
# wait until the page is fully loaded
sleep 1
else
response[:services] = services
break
end
end
raise "Not found services" if response[:services].empty?
unless self.class.blank_string?(title)
response[:services].select! { |service| service[:title] == title }
end
response[:services].sort_by! { |service| service[:title] }
response
end
|
ruby
|
def perform(country: nil, title: nil)
@session.visit(apple_url(country))
response = {
title: @session.find(".section-date .date-copy").text.strip,
services: [],
}
MAX_RETRY_COUNT.times do
services = fetch_services
if services.empty?
# wait until the page is fully loaded
sleep 1
else
response[:services] = services
break
end
end
raise "Not found services" if response[:services].empty?
unless self.class.blank_string?(title)
response[:services].select! { |service| service[:title] == title }
end
response[:services].sort_by! { |service| service[:title] }
response
end
|
[
"def",
"perform",
"(",
"country",
":",
"nil",
",",
"title",
":",
"nil",
")",
"@session",
".",
"visit",
"(",
"apple_url",
"(",
"country",
")",
")",
"response",
"=",
"{",
"title",
":",
"@session",
".",
"find",
"(",
"\".section-date .date-copy\"",
")",
".",
"text",
".",
"strip",
",",
"services",
":",
"[",
"]",
",",
"}",
"MAX_RETRY_COUNT",
".",
"times",
"do",
"services",
"=",
"fetch_services",
"if",
"services",
".",
"empty?",
"# wait until the page is fully loaded",
"sleep",
"1",
"else",
"response",
"[",
":services",
"]",
"=",
"services",
"break",
"end",
"end",
"raise",
"\"Not found services\"",
"if",
"response",
"[",
":services",
"]",
".",
"empty?",
"unless",
"self",
".",
"class",
".",
"blank_string?",
"(",
"title",
")",
"response",
"[",
":services",
"]",
".",
"select!",
"{",
"|",
"service",
"|",
"service",
"[",
":title",
"]",
"==",
"title",
"}",
"end",
"response",
"[",
":services",
"]",
".",
"sort_by!",
"{",
"|",
"service",
"|",
"service",
"[",
":title",
"]",
"}",
"response",
"end"
] |
crawl apple system status page
@param country [String] country code. (e.g. jp, ca, fr. default. us)
@param title [String] If specified, narrow the service title
@return [Hash]
@example response format
{
title: ,
services: [
{ title: , description: , status: }
]
}
|
[
"crawl",
"apple",
"system",
"status",
"page"
] |
9675c02e2157081f7e7a607e740430c67b0a603a
|
https://github.com/sue445/apple_system_status/blob/9675c02e2157081f7e7a607e740430c67b0a603a/lib/apple_system_status/crawler.rb#L55-L84
|
21,523 |
14113/fio_api
|
lib/base/list.rb
|
FioAPI.List.fetch_and_deserialize_response
|
def fetch_and_deserialize_response(path)
self.request = FioAPI::Request.get(path, parser: ListResponseDeserializer)
self.response = request.parsed_response
request
end
|
ruby
|
def fetch_and_deserialize_response(path)
self.request = FioAPI::Request.get(path, parser: ListResponseDeserializer)
self.response = request.parsed_response
request
end
|
[
"def",
"fetch_and_deserialize_response",
"(",
"path",
")",
"self",
".",
"request",
"=",
"FioAPI",
"::",
"Request",
".",
"get",
"(",
"path",
",",
"parser",
":",
"ListResponseDeserializer",
")",
"self",
".",
"response",
"=",
"request",
".",
"parsed_response",
"request",
"end"
] |
Create request object ot provided uri and instantiate list deserializer.
Request uri and deserialize response to response object with account info and transactions list.
== Parameters:
args::
Parts of uri
== Returns:
List insatnce with account info and transactions list
|
[
"Create",
"request",
"object",
"ot",
"provided",
"uri",
"and",
"instantiate",
"list",
"deserializer",
".",
"Request",
"uri",
"and",
"deserialize",
"response",
"to",
"response",
"object",
"with",
"account",
"info",
"and",
"transactions",
"list",
"."
] |
f982c27332ef629929aff03f93e8df9c7516414c
|
https://github.com/14113/fio_api/blob/f982c27332ef629929aff03f93e8df9c7516414c/lib/base/list.rb#L88-L92
|
21,524 |
archangel/archangel
|
app/helpers/archangel/flash_helper.rb
|
Archangel.FlashHelper.flash_class_for
|
def flash_class_for(flash_type)
flash_type = flash_type.to_s.downcase.parameterize
{
success: "success", error: "danger", alert: "warning", notice: "info"
}.fetch(flash_type.to_sym, flash_type)
end
|
ruby
|
def flash_class_for(flash_type)
flash_type = flash_type.to_s.downcase.parameterize
{
success: "success", error: "danger", alert: "warning", notice: "info"
}.fetch(flash_type.to_sym, flash_type)
end
|
[
"def",
"flash_class_for",
"(",
"flash_type",
")",
"flash_type",
"=",
"flash_type",
".",
"to_s",
".",
"downcase",
".",
"parameterize",
"{",
"success",
":",
"\"success\"",
",",
"error",
":",
"\"danger\"",
",",
"alert",
":",
"\"warning\"",
",",
"notice",
":",
"\"info\"",
"}",
".",
"fetch",
"(",
"flash_type",
".",
"to_sym",
",",
"flash_type",
")",
"end"
] |
Converts Rails flash message type to Bootstrap flash message type
@param flash_type [String,Symbol] the flash message type
@return [String] flash message type
|
[
"Converts",
"Rails",
"flash",
"message",
"type",
"to",
"Bootstrap",
"flash",
"message",
"type"
] |
f84a9e3a0003f64a6057f8f4db1e05163a7d43b6
|
https://github.com/archangel/archangel/blob/f84a9e3a0003f64a6057f8f4db1e05163a7d43b6/app/helpers/archangel/flash_helper.rb#L14-L20
|
21,525 |
artsy/momentum
|
lib/momentum/opsworks.rb
|
Momentum::OpsWorks.Deployer.execute_recipe!
|
def execute_recipe!(stack_name, layer, recipe, app_name = Momentum.config[:app_base_name])
raise "No recipe provided" unless recipe
stack = Momentum::OpsWorks.get_stack(@ow, stack_name)
app = Momentum::OpsWorks.get_app(@ow, stack, app_name)
layer_names = layer ? [layer] : Momentum.config[:app_layers]
layers = Momentum::OpsWorks.get_layers(@ow, stack, layer_names)
instance_ids = layers.inject([]) { |ids, l| ids + Momentum::OpsWorks.get_online_instance_ids(@ow, layer_id: l[:layer_id]) }
raise 'No online instances found!' if instance_ids.empty?
@ow.create_deployment(
stack_id: stack[:stack_id],
app_id: app[:app_id],
command: {
name: 'execute_recipes',
args: {
'recipes' => [recipe.to_s]
}
},
instance_ids: instance_ids
)
end
|
ruby
|
def execute_recipe!(stack_name, layer, recipe, app_name = Momentum.config[:app_base_name])
raise "No recipe provided" unless recipe
stack = Momentum::OpsWorks.get_stack(@ow, stack_name)
app = Momentum::OpsWorks.get_app(@ow, stack, app_name)
layer_names = layer ? [layer] : Momentum.config[:app_layers]
layers = Momentum::OpsWorks.get_layers(@ow, stack, layer_names)
instance_ids = layers.inject([]) { |ids, l| ids + Momentum::OpsWorks.get_online_instance_ids(@ow, layer_id: l[:layer_id]) }
raise 'No online instances found!' if instance_ids.empty?
@ow.create_deployment(
stack_id: stack[:stack_id],
app_id: app[:app_id],
command: {
name: 'execute_recipes',
args: {
'recipes' => [recipe.to_s]
}
},
instance_ids: instance_ids
)
end
|
[
"def",
"execute_recipe!",
"(",
"stack_name",
",",
"layer",
",",
"recipe",
",",
"app_name",
"=",
"Momentum",
".",
"config",
"[",
":app_base_name",
"]",
")",
"raise",
"\"No recipe provided\"",
"unless",
"recipe",
"stack",
"=",
"Momentum",
"::",
"OpsWorks",
".",
"get_stack",
"(",
"@ow",
",",
"stack_name",
")",
"app",
"=",
"Momentum",
"::",
"OpsWorks",
".",
"get_app",
"(",
"@ow",
",",
"stack",
",",
"app_name",
")",
"layer_names",
"=",
"layer",
"?",
"[",
"layer",
"]",
":",
"Momentum",
".",
"config",
"[",
":app_layers",
"]",
"layers",
"=",
"Momentum",
"::",
"OpsWorks",
".",
"get_layers",
"(",
"@ow",
",",
"stack",
",",
"layer_names",
")",
"instance_ids",
"=",
"layers",
".",
"inject",
"(",
"[",
"]",
")",
"{",
"|",
"ids",
",",
"l",
"|",
"ids",
"+",
"Momentum",
"::",
"OpsWorks",
".",
"get_online_instance_ids",
"(",
"@ow",
",",
"layer_id",
":",
"l",
"[",
":layer_id",
"]",
")",
"}",
"raise",
"'No online instances found!'",
"if",
"instance_ids",
".",
"empty?",
"@ow",
".",
"create_deployment",
"(",
"stack_id",
":",
"stack",
"[",
":stack_id",
"]",
",",
"app_id",
":",
"app",
"[",
":app_id",
"]",
",",
"command",
":",
"{",
"name",
":",
"'execute_recipes'",
",",
"args",
":",
"{",
"'recipes'",
"=>",
"[",
"recipe",
".",
"to_s",
"]",
"}",
"}",
",",
"instance_ids",
":",
"instance_ids",
")",
"end"
] |
wait up to 15 minutes
|
[
"wait",
"up",
"to",
"15",
"minutes"
] |
61b9926a783c3a3fc4f105334d1e291273132392
|
https://github.com/artsy/momentum/blob/61b9926a783c3a3fc4f105334d1e291273132392/lib/momentum/opsworks.rb#L68-L87
|
21,526 |
fixrb/fix
|
lib/fix/on.rb
|
Fix.On.it
|
def it(*, &spec)
i = It.new(described, challenges, helpers)
result = i.verify(&spec)
if configuration.fetch(:verbose, true)
print result.to_char(configuration.fetch(:color, false))
end
results << result
end
|
ruby
|
def it(*, &spec)
i = It.new(described, challenges, helpers)
result = i.verify(&spec)
if configuration.fetch(:verbose, true)
print result.to_char(configuration.fetch(:color, false))
end
results << result
end
|
[
"def",
"it",
"(",
"*",
",",
"&",
"spec",
")",
"i",
"=",
"It",
".",
"new",
"(",
"described",
",",
"challenges",
",",
"helpers",
")",
"result",
"=",
"i",
".",
"verify",
"(",
"spec",
")",
"if",
"configuration",
".",
"fetch",
"(",
":verbose",
",",
"true",
")",
"print",
"result",
".",
"to_char",
"(",
"configuration",
".",
"fetch",
"(",
":color",
",",
"false",
")",
")",
"end",
"results",
"<<",
"result",
"end"
] |
Add it method to the DSL.
@api public
@example It must eql "FOO"
it { MUST equal 'FOO' }
@param spec [Proc] A spec to compare against the computed actual value.
@return [Array] List of results.
|
[
"Add",
"it",
"method",
"to",
"the",
"DSL",
"."
] |
7023dce0c28eb92db7a2dbafe56e81246f215e31
|
https://github.com/fixrb/fix/blob/7023dce0c28eb92db7a2dbafe56e81246f215e31/lib/fix/on.rb#L62-L72
|
21,527 |
fixrb/fix
|
lib/fix/on.rb
|
Fix.On.on
|
def on(method_name, *args, &block)
o = On.new(described,
results,
(challenges + [Defi.send(method_name, *args)]),
helpers.dup,
configuration)
o.instance_eval(&block)
end
|
ruby
|
def on(method_name, *args, &block)
o = On.new(described,
results,
(challenges + [Defi.send(method_name, *args)]),
helpers.dup,
configuration)
o.instance_eval(&block)
end
|
[
"def",
"on",
"(",
"method_name",
",",
"*",
"args",
",",
"&",
"block",
")",
"o",
"=",
"On",
".",
"new",
"(",
"described",
",",
"results",
",",
"(",
"challenges",
"+",
"[",
"Defi",
".",
"send",
"(",
"method_name",
",",
"args",
")",
"]",
")",
",",
"helpers",
".",
"dup",
",",
"configuration",
")",
"o",
".",
"instance_eval",
"(",
"block",
")",
"end"
] |
Add on method to the DSL.
@api public
@example On +2, it must equal 44.
on(:+, 2) do
it { MUST equal 44 }
end
@param method_name [Symbol] The identifier of a method.
@param args [Array] A list of arguments.
@param block [Proc] A spec to compare against the computed value.
@return [Array] List of results.
|
[
"Add",
"on",
"method",
"to",
"the",
"DSL",
"."
] |
7023dce0c28eb92db7a2dbafe56e81246f215e31
|
https://github.com/fixrb/fix/blob/7023dce0c28eb92db7a2dbafe56e81246f215e31/lib/fix/on.rb#L88-L96
|
21,528 |
fixrb/fix
|
lib/fix/on.rb
|
Fix.On.context
|
def context(*, &block)
o = On.new(described,
[],
challenges,
helpers.dup,
configuration)
results.concat(Aw.fork! { o.instance_eval(&block) })
end
|
ruby
|
def context(*, &block)
o = On.new(described,
[],
challenges,
helpers.dup,
configuration)
results.concat(Aw.fork! { o.instance_eval(&block) })
end
|
[
"def",
"context",
"(",
"*",
",",
"&",
"block",
")",
"o",
"=",
"On",
".",
"new",
"(",
"described",
",",
"[",
"]",
",",
"challenges",
",",
"helpers",
".",
"dup",
",",
"configuration",
")",
"results",
".",
"concat",
"(",
"Aw",
".",
"fork!",
"{",
"o",
".",
"instance_eval",
"(",
"block",
")",
"}",
")",
"end"
] |
Add context method to the DSL, to build an isolated scope.
@api public
@example Context when logged in.
context 'when logged in' do
it { MUST equal 200 }
end
@param block [Proc] A block of specs to test in isolation.
@return [Array] List of results.
|
[
"Add",
"context",
"method",
"to",
"the",
"DSL",
"to",
"build",
"an",
"isolated",
"scope",
"."
] |
7023dce0c28eb92db7a2dbafe56e81246f215e31
|
https://github.com/fixrb/fix/blob/7023dce0c28eb92db7a2dbafe56e81246f215e31/lib/fix/on.rb#L127-L135
|
21,529 |
aastronautss/emittance
|
lib/emittance/watcher.rb
|
Emittance.Watcher.watch
|
def watch(identifier, callback_method = nil, **params, &callback)
if callback
_dispatcher(params).register identifier, params, &callback
else
_dispatcher(params).register_method_call identifier, self, callback_method, params
end
end
|
ruby
|
def watch(identifier, callback_method = nil, **params, &callback)
if callback
_dispatcher(params).register identifier, params, &callback
else
_dispatcher(params).register_method_call identifier, self, callback_method, params
end
end
|
[
"def",
"watch",
"(",
"identifier",
",",
"callback_method",
"=",
"nil",
",",
"**",
"params",
",",
"&",
"callback",
")",
"if",
"callback",
"_dispatcher",
"(",
"params",
")",
".",
"register",
"identifier",
",",
"params",
",",
"callback",
"else",
"_dispatcher",
"(",
"params",
")",
".",
"register_method_call",
"identifier",
",",
"self",
",",
"callback_method",
",",
"params",
"end",
"end"
] |
Watch for an event, identified by its class' identifier. If a callback method is provided, then it will call that
method on the caller of +watch+ when the event happens. Otherwise, it will run the callback block.
@param identifier [Symbol] the event's identifier
@param callback_method [Symbol] one option for adding a callback--the method on the object to call when the
event fires
@param params [Hash] any parameters related to the registration of a watcher
@param callback [Block] the other option for adding a callback--the block you wish to be executed when the event
fires
@return [Proc] the block that will run when the event fires
|
[
"Watch",
"for",
"an",
"event",
"identified",
"by",
"its",
"class",
"identifier",
".",
"If",
"a",
"callback",
"method",
"is",
"provided",
"then",
"it",
"will",
"call",
"that",
"method",
"on",
"the",
"caller",
"of",
"+",
"watch",
"+",
"when",
"the",
"event",
"happens",
".",
"Otherwise",
"it",
"will",
"run",
"the",
"callback",
"block",
"."
] |
3ff045c4b3f35c1ca7743bc09bcb1fff7934204c
|
https://github.com/aastronautss/emittance/blob/3ff045c4b3f35c1ca7743bc09bcb1fff7934204c/lib/emittance/watcher.rb#L18-L24
|
21,530 |
archangel/archangel
|
app/services/archangel/render_service.rb
|
Archangel.RenderService.call
|
def call
liquid = ::Liquid::Template.parse(template)
liquid.send(:render, stringify_assigns, liquid_options).html_safe
end
|
ruby
|
def call
liquid = ::Liquid::Template.parse(template)
liquid.send(:render, stringify_assigns, liquid_options).html_safe
end
|
[
"def",
"call",
"liquid",
"=",
"::",
"Liquid",
"::",
"Template",
".",
"parse",
"(",
"template",
")",
"liquid",
".",
"send",
"(",
":render",
",",
"stringify_assigns",
",",
"liquid_options",
")",
".",
"html_safe",
"end"
] |
Render the Liquid content
@return [String] the rendered content
|
[
"Render",
"the",
"Liquid",
"content"
] |
f84a9e3a0003f64a6057f8f4db1e05163a7d43b6
|
https://github.com/archangel/archangel/blob/f84a9e3a0003f64a6057f8f4db1e05163a7d43b6/app/services/archangel/render_service.rb#L59-L62
|
21,531 |
archangel/archangel
|
app/helpers/archangel/application_helper.rb
|
Archangel.ApplicationHelper.frontend_resource_path
|
def frontend_resource_path(resource)
permalink_path = proc do |permalink|
archangel.frontend_page_path(permalink).gsub("%2F", "/")
end
return permalink_path.call(resource) unless resource.class == Page
return archangel.frontend_root_path if resource.homepage?
permalink_path.call(resource.permalink)
end
|
ruby
|
def frontend_resource_path(resource)
permalink_path = proc do |permalink|
archangel.frontend_page_path(permalink).gsub("%2F", "/")
end
return permalink_path.call(resource) unless resource.class == Page
return archangel.frontend_root_path if resource.homepage?
permalink_path.call(resource.permalink)
end
|
[
"def",
"frontend_resource_path",
"(",
"resource",
")",
"permalink_path",
"=",
"proc",
"do",
"|",
"permalink",
"|",
"archangel",
".",
"frontend_page_path",
"(",
"permalink",
")",
".",
"gsub",
"(",
"\"%2F\"",
",",
"\"/\"",
")",
"end",
"return",
"permalink_path",
".",
"call",
"(",
"resource",
")",
"unless",
"resource",
".",
"class",
"==",
"Page",
"return",
"archangel",
".",
"frontend_root_path",
"if",
"resource",
".",
"homepage?",
"permalink_path",
".",
"call",
"(",
"resource",
".",
"permalink",
")",
"end"
] |
Frontend resource permalink.
Same as `frontend_page_path` except it prints out nested resources in a
nice way.
Example
<%= frontend_resource_path('foo/bar') %> #=> /foo/bar
<%= frontend_resource_path(@page) %> #=> /foo/bar
@return [String] frontend resource permalink
|
[
"Frontend",
"resource",
"permalink",
"."
] |
f84a9e3a0003f64a6057f8f4db1e05163a7d43b6
|
https://github.com/archangel/archangel/blob/f84a9e3a0003f64a6057f8f4db1e05163a7d43b6/app/helpers/archangel/application_helper.rb#L20-L29
|
21,532 |
YotpoLtd/seoshop-api
|
lib/seoshop-api/client.rb
|
Seoshop.Client.get
|
def get(url, params = {})
params = params.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}
preform(url, :get, params: params) do
return connection.get(url, params)
end
end
|
ruby
|
def get(url, params = {})
params = params.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}
preform(url, :get, params: params) do
return connection.get(url, params)
end
end
|
[
"def",
"get",
"(",
"url",
",",
"params",
"=",
"{",
"}",
")",
"params",
"=",
"params",
".",
"inject",
"(",
"{",
"}",
")",
"{",
"|",
"memo",
",",
"(",
"k",
",",
"v",
")",
"|",
"memo",
"[",
"k",
".",
"to_s",
"]",
"=",
"v",
";",
"memo",
"}",
"preform",
"(",
"url",
",",
":get",
",",
"params",
":",
"params",
")",
"do",
"return",
"connection",
".",
"get",
"(",
"url",
",",
"params",
")",
"end",
"end"
] |
Does a GET request to the url with the params
@param url [String] the relative path in the Seoshop API
@param params [Hash] the url params that should be passed in the request
|
[
"Does",
"a",
"GET",
"request",
"to",
"the",
"url",
"with",
"the",
"params"
] |
7b1ea990824222be8c4543384c97b5640b97e433
|
https://github.com/YotpoLtd/seoshop-api/blob/7b1ea990824222be8c4543384c97b5640b97e433/lib/seoshop-api/client.rb#L73-L78
|
21,533 |
YotpoLtd/seoshop-api
|
lib/seoshop-api/client.rb
|
Seoshop.Client.post
|
def post(url, params)
params = convert_hash_keys(params)
preform(url, :post, params: params) do
return connection.post(url, params)
end
end
|
ruby
|
def post(url, params)
params = convert_hash_keys(params)
preform(url, :post, params: params) do
return connection.post(url, params)
end
end
|
[
"def",
"post",
"(",
"url",
",",
"params",
")",
"params",
"=",
"convert_hash_keys",
"(",
"params",
")",
"preform",
"(",
"url",
",",
":post",
",",
"params",
":",
"params",
")",
"do",
"return",
"connection",
".",
"post",
"(",
"url",
",",
"params",
")",
"end",
"end"
] |
Does a POST request to the url with the params
@param url [String] the relative path in the Seoshop API
@param params [Hash] the body of the request
|
[
"Does",
"a",
"POST",
"request",
"to",
"the",
"url",
"with",
"the",
"params"
] |
7b1ea990824222be8c4543384c97b5640b97e433
|
https://github.com/YotpoLtd/seoshop-api/blob/7b1ea990824222be8c4543384c97b5640b97e433/lib/seoshop-api/client.rb#L85-L90
|
21,534 |
YotpoLtd/seoshop-api
|
lib/seoshop-api/client.rb
|
Seoshop.Client.put
|
def put(url, params)
params = convert_hash_keys(params)
preform(url, :put, params: params) do
return connection.put(url, params)
end
end
|
ruby
|
def put(url, params)
params = convert_hash_keys(params)
preform(url, :put, params: params) do
return connection.put(url, params)
end
end
|
[
"def",
"put",
"(",
"url",
",",
"params",
")",
"params",
"=",
"convert_hash_keys",
"(",
"params",
")",
"preform",
"(",
"url",
",",
":put",
",",
"params",
":",
"params",
")",
"do",
"return",
"connection",
".",
"put",
"(",
"url",
",",
"params",
")",
"end",
"end"
] |
Does a PUT request to the url with the params
@param url [String] the relative path in the Seoshop API
@param params [Hash] the body of the request
|
[
"Does",
"a",
"PUT",
"request",
"to",
"the",
"url",
"with",
"the",
"params"
] |
7b1ea990824222be8c4543384c97b5640b97e433
|
https://github.com/YotpoLtd/seoshop-api/blob/7b1ea990824222be8c4543384c97b5640b97e433/lib/seoshop-api/client.rb#L97-L102
|
21,535 |
archangel/archangel
|
lib/archangel/liquid_view.rb
|
Archangel.LiquidView.render
|
def render(template, local_assigns = {})
default_controller.headers["Content-Type"] ||= "text/html; charset=utf-8"
assigns = default_assigns(local_assigns)
options = { registers: default_registers }
Archangel::RenderService.call(template, assigns, options)
end
|
ruby
|
def render(template, local_assigns = {})
default_controller.headers["Content-Type"] ||= "text/html; charset=utf-8"
assigns = default_assigns(local_assigns)
options = { registers: default_registers }
Archangel::RenderService.call(template, assigns, options)
end
|
[
"def",
"render",
"(",
"template",
",",
"local_assigns",
"=",
"{",
"}",
")",
"default_controller",
".",
"headers",
"[",
"\"Content-Type\"",
"]",
"||=",
"\"text/html; charset=utf-8\"",
"assigns",
"=",
"default_assigns",
"(",
"local_assigns",
")",
"options",
"=",
"{",
"registers",
":",
"default_registers",
"}",
"Archangel",
"::",
"RenderService",
".",
"call",
"(",
"template",
",",
"assigns",
",",
"options",
")",
"end"
] |
Render Liquid content
@param template [String] the content
@param local_assigns [Hash] the local assigned variables
@return [String] the rendered content
|
[
"Render",
"Liquid",
"content"
] |
f84a9e3a0003f64a6057f8f4db1e05163a7d43b6
|
https://github.com/archangel/archangel/blob/f84a9e3a0003f64a6057f8f4db1e05163a7d43b6/lib/archangel/liquid_view.rb#L35-L42
|
21,536 |
inukshuk/bibtex-ruby
|
lib/bibtex/lexer.rb
|
BibTeX.Lexer.push
|
def push(value)
case value[0]
when :CONTENT, :STRING_LITERAL
value[1].gsub!(/\n\s*/, ' ') if strip_line_breaks?
if [email protected]? && value[0] == @stack[-1][0]
@stack[-1][1] << value[1]
else
@stack.push(value)
end
when :ERROR
@stack.push(value) if @include_errors
leave_object
when :META_CONTENT
@stack.push(value) if @include_meta_content
else
@stack.push(value)
end
self
end
|
ruby
|
def push(value)
case value[0]
when :CONTENT, :STRING_LITERAL
value[1].gsub!(/\n\s*/, ' ') if strip_line_breaks?
if [email protected]? && value[0] == @stack[-1][0]
@stack[-1][1] << value[1]
else
@stack.push(value)
end
when :ERROR
@stack.push(value) if @include_errors
leave_object
when :META_CONTENT
@stack.push(value) if @include_meta_content
else
@stack.push(value)
end
self
end
|
[
"def",
"push",
"(",
"value",
")",
"case",
"value",
"[",
"0",
"]",
"when",
":CONTENT",
",",
":STRING_LITERAL",
"value",
"[",
"1",
"]",
".",
"gsub!",
"(",
"/",
"\\n",
"\\s",
"/",
",",
"' '",
")",
"if",
"strip_line_breaks?",
"if",
"!",
"@stack",
".",
"empty?",
"&&",
"value",
"[",
"0",
"]",
"==",
"@stack",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"@stack",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"<<",
"value",
"[",
"1",
"]",
"else",
"@stack",
".",
"push",
"(",
"value",
")",
"end",
"when",
":ERROR",
"@stack",
".",
"push",
"(",
"value",
")",
"if",
"@include_errors",
"leave_object",
"when",
":META_CONTENT",
"@stack",
".",
"push",
"(",
"value",
")",
"if",
"@include_meta_content",
"else",
"@stack",
".",
"push",
"(",
"value",
")",
"end",
"self",
"end"
] |
Pushes a value onto the parse stack. Returns the Lexer.
|
[
"Pushes",
"a",
"value",
"onto",
"the",
"parse",
"stack",
".",
"Returns",
"the",
"Lexer",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/lexer.rb#L145-L165
|
21,537 |
inukshuk/bibtex-ruby
|
lib/bibtex/lexer.rb
|
BibTeX.Lexer.analyse
|
def analyse(string = nil)
raise(ArgumentError, 'Lexer: failed to start analysis: no source given!') unless
string || @scanner
self.data = string || @scanner.string
until @scanner.eos?
send("parse_#{MODE[@mode]}")
end
push([false, '$end'])
end
|
ruby
|
def analyse(string = nil)
raise(ArgumentError, 'Lexer: failed to start analysis: no source given!') unless
string || @scanner
self.data = string || @scanner.string
until @scanner.eos?
send("parse_#{MODE[@mode]}")
end
push([false, '$end'])
end
|
[
"def",
"analyse",
"(",
"string",
"=",
"nil",
")",
"raise",
"(",
"ArgumentError",
",",
"'Lexer: failed to start analysis: no source given!'",
")",
"unless",
"string",
"||",
"@scanner",
"self",
".",
"data",
"=",
"string",
"||",
"@scanner",
".",
"string",
"until",
"@scanner",
".",
"eos?",
"send",
"(",
"\"parse_#{MODE[@mode]}\"",
")",
"end",
"push",
"(",
"[",
"false",
",",
"'$end'",
"]",
")",
"end"
] |
Start the lexical analysis.
|
[
"Start",
"the",
"lexical",
"analysis",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/lexer.rb#L168-L179
|
21,538 |
inukshuk/bibtex-ruby
|
lib/bibtex/lexer.rb
|
BibTeX.Lexer.enter_object
|
def enter_object
@brace_level = 0
push [:AT,'@']
case
when @scanner.scan(Lexer.patterns[:string])
@mode = @active_object = :string
push [:STRING, @scanner.matched]
when @scanner.scan(Lexer.patterns[:preamble])
@mode = @active_object = :preamble
push [:PREAMBLE, @scanner.matched]
when @scanner.scan(Lexer.patterns[:comment])
@mode = @active_object = :comment
push [:COMMENT, @scanner.matched]
when @scanner.scan(Lexer.patterns[:entry])
@mode = @active_object = :entry
push [:NAME, @scanner.matched]
# TODO: DRY - try to parse key
if @scanner.scan(Lexer.patterns[:lbrace])
@brace_level += 1
push([:LBRACE,'{'])
@mode = :content if @brace_level > 1 || @brace_level == 1 && active?(:comment)
if @scanner.scan(Lexer.patterns[allow_missing_keys? ? :optional_key : :key])
push [:KEY, @scanner.matched.chop.strip]
end
end
else
error_unexpected_object
end
end
|
ruby
|
def enter_object
@brace_level = 0
push [:AT,'@']
case
when @scanner.scan(Lexer.patterns[:string])
@mode = @active_object = :string
push [:STRING, @scanner.matched]
when @scanner.scan(Lexer.patterns[:preamble])
@mode = @active_object = :preamble
push [:PREAMBLE, @scanner.matched]
when @scanner.scan(Lexer.patterns[:comment])
@mode = @active_object = :comment
push [:COMMENT, @scanner.matched]
when @scanner.scan(Lexer.patterns[:entry])
@mode = @active_object = :entry
push [:NAME, @scanner.matched]
# TODO: DRY - try to parse key
if @scanner.scan(Lexer.patterns[:lbrace])
@brace_level += 1
push([:LBRACE,'{'])
@mode = :content if @brace_level > 1 || @brace_level == 1 && active?(:comment)
if @scanner.scan(Lexer.patterns[allow_missing_keys? ? :optional_key : :key])
push [:KEY, @scanner.matched.chop.strip]
end
end
else
error_unexpected_object
end
end
|
[
"def",
"enter_object",
"@brace_level",
"=",
"0",
"push",
"[",
":AT",
",",
"'@'",
"]",
"case",
"when",
"@scanner",
".",
"scan",
"(",
"Lexer",
".",
"patterns",
"[",
":string",
"]",
")",
"@mode",
"=",
"@active_object",
"=",
":string",
"push",
"[",
":STRING",
",",
"@scanner",
".",
"matched",
"]",
"when",
"@scanner",
".",
"scan",
"(",
"Lexer",
".",
"patterns",
"[",
":preamble",
"]",
")",
"@mode",
"=",
"@active_object",
"=",
":preamble",
"push",
"[",
":PREAMBLE",
",",
"@scanner",
".",
"matched",
"]",
"when",
"@scanner",
".",
"scan",
"(",
"Lexer",
".",
"patterns",
"[",
":comment",
"]",
")",
"@mode",
"=",
"@active_object",
"=",
":comment",
"push",
"[",
":COMMENT",
",",
"@scanner",
".",
"matched",
"]",
"when",
"@scanner",
".",
"scan",
"(",
"Lexer",
".",
"patterns",
"[",
":entry",
"]",
")",
"@mode",
"=",
"@active_object",
"=",
":entry",
"push",
"[",
":NAME",
",",
"@scanner",
".",
"matched",
"]",
"# TODO: DRY - try to parse key",
"if",
"@scanner",
".",
"scan",
"(",
"Lexer",
".",
"patterns",
"[",
":lbrace",
"]",
")",
"@brace_level",
"+=",
"1",
"push",
"(",
"[",
":LBRACE",
",",
"'{'",
"]",
")",
"@mode",
"=",
":content",
"if",
"@brace_level",
">",
"1",
"||",
"@brace_level",
"==",
"1",
"&&",
"active?",
"(",
":comment",
")",
"if",
"@scanner",
".",
"scan",
"(",
"Lexer",
".",
"patterns",
"[",
"allow_missing_keys?",
"?",
":optional_key",
":",
":key",
"]",
")",
"push",
"[",
":KEY",
",",
"@scanner",
".",
"matched",
".",
"chop",
".",
"strip",
"]",
"end",
"end",
"else",
"error_unexpected_object",
"end",
"end"
] |
Called when the lexer encounters a new BibTeX object.
|
[
"Called",
"when",
"the",
"lexer",
"encounters",
"a",
"new",
"BibTeX",
"object",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/lexer.rb#L285-L317
|
21,539 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.initialize_copy
|
def initialize_copy(other)
@fields = {}
self.type = other.type
self.key = other.key
add(other.fields)
end
|
ruby
|
def initialize_copy(other)
@fields = {}
self.type = other.type
self.key = other.key
add(other.fields)
end
|
[
"def",
"initialize_copy",
"(",
"other",
")",
"@fields",
"=",
"{",
"}",
"self",
".",
"type",
"=",
"other",
".",
"type",
"self",
".",
"key",
"=",
"other",
".",
"key",
"add",
"(",
"other",
".",
"fields",
")",
"end"
] |
Creates a new instance. If a hash is given, the entry is populated accordingly.
|
[
"Creates",
"a",
"new",
"instance",
".",
"If",
"a",
"hash",
"is",
"given",
"the",
"entry",
"is",
"populated",
"accordingly",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L85-L92
|
21,540 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.key=
|
def key=(key)
key = key.to_s
if registered?
bibliography.entries.delete(@key)
key = register(key)
end
@key = key
rescue => e
raise BibTeXError, "failed to set key to #{key.inspect}: #{e.message}"
end
|
ruby
|
def key=(key)
key = key.to_s
if registered?
bibliography.entries.delete(@key)
key = register(key)
end
@key = key
rescue => e
raise BibTeXError, "failed to set key to #{key.inspect}: #{e.message}"
end
|
[
"def",
"key",
"=",
"(",
"key",
")",
"key",
"=",
"key",
".",
"to_s",
"if",
"registered?",
"bibliography",
".",
"entries",
".",
"delete",
"(",
"@key",
")",
"key",
"=",
"register",
"(",
"key",
")",
"end",
"@key",
"=",
"key",
"rescue",
"=>",
"e",
"raise",
"BibTeXError",
",",
"\"failed to set key to #{key.inspect}: #{e.message}\"",
"end"
] |
Sets the Entry's key. If the Entry is currently registered with a
Bibliography, re-registers the Entry with the new key; note that this
may change the key value if another Entry is already regsitered with
the same key.
Returns the new key.
|
[
"Sets",
"the",
"Entry",
"s",
"key",
".",
"If",
"the",
"Entry",
"is",
"currently",
"registered",
"with",
"a",
"Bibliography",
"re",
"-",
"registers",
"the",
"Entry",
"with",
"the",
"new",
"key",
";",
"note",
"that",
"this",
"may",
"change",
"the",
"key",
"value",
"if",
"another",
"Entry",
"is",
"already",
"regsitered",
"with",
"the",
"same",
"key",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L183-L194
|
21,541 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.provide
|
def provide(name)
return nil unless name.respond_to?(:to_sym)
name = name.to_sym
fields[name] || fields[aliases[name]]
end
|
ruby
|
def provide(name)
return nil unless name.respond_to?(:to_sym)
name = name.to_sym
fields[name] || fields[aliases[name]]
end
|
[
"def",
"provide",
"(",
"name",
")",
"return",
"nil",
"unless",
"name",
".",
"respond_to?",
"(",
":to_sym",
")",
"name",
"=",
"name",
".",
"to_sym",
"fields",
"[",
"name",
"]",
"||",
"fields",
"[",
"aliases",
"[",
"name",
"]",
"]",
"end"
] |
Returns the field value referenced by the passed-in name.
For example, this will return the 'title' value for 'booktitle' if a
corresponding alias is defined.
|
[
"Returns",
"the",
"field",
"value",
"referenced",
"by",
"the",
"passed",
"-",
"in",
"name",
".",
"For",
"example",
"this",
"will",
"return",
"the",
"title",
"value",
"for",
"booktitle",
"if",
"a",
"corresponding",
"alias",
"is",
"defined",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L251-L255
|
21,542 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.field_names
|
def field_names(filter = [], include_inherited = true)
names = fields.keys
if include_inherited && has_parent?
names.concat(inherited_fields)
end
unless filter.empty?
names = names & filter.map(&:to_sym)
end
names.sort!
names
end
|
ruby
|
def field_names(filter = [], include_inherited = true)
names = fields.keys
if include_inherited && has_parent?
names.concat(inherited_fields)
end
unless filter.empty?
names = names & filter.map(&:to_sym)
end
names.sort!
names
end
|
[
"def",
"field_names",
"(",
"filter",
"=",
"[",
"]",
",",
"include_inherited",
"=",
"true",
")",
"names",
"=",
"fields",
".",
"keys",
"if",
"include_inherited",
"&&",
"has_parent?",
"names",
".",
"concat",
"(",
"inherited_fields",
")",
"end",
"unless",
"filter",
".",
"empty?",
"names",
"=",
"names",
"&",
"filter",
".",
"map",
"(",
":to_sym",
")",
"end",
"names",
".",
"sort!",
"names",
"end"
] |
Returns a sorted list of the Entry's field names. If a +filter+ is passed
as argument, returns all field names that are also defined by the filter.
If the +filter+ is empty, returns all field names.
If the second optional argument is true (default) and the Entry contains
a cross-reference, the list will include all inherited fields.
|
[
"Returns",
"a",
"sorted",
"list",
"of",
"the",
"Entry",
"s",
"field",
"names",
".",
"If",
"a",
"+",
"filter",
"+",
"is",
"passed",
"as",
"argument",
"returns",
"all",
"field",
"names",
"that",
"are",
"also",
"defined",
"by",
"the",
"filter",
".",
"If",
"the",
"+",
"filter",
"+",
"is",
"empty",
"returns",
"all",
"field",
"names",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L275-L288
|
21,543 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.inherited_fields
|
def inherited_fields
return [] unless has_parent?
names = parent.fields.keys - fields.keys
names.concat(parent.aliases.reject { |k,v| !parent.has_field?(v) }.keys)
names.sort!
names
end
|
ruby
|
def inherited_fields
return [] unless has_parent?
names = parent.fields.keys - fields.keys
names.concat(parent.aliases.reject { |k,v| !parent.has_field?(v) }.keys)
names.sort!
names
end
|
[
"def",
"inherited_fields",
"return",
"[",
"]",
"unless",
"has_parent?",
"names",
"=",
"parent",
".",
"fields",
".",
"keys",
"-",
"fields",
".",
"keys",
"names",
".",
"concat",
"(",
"parent",
".",
"aliases",
".",
"reject",
"{",
"|",
"k",
",",
"v",
"|",
"!",
"parent",
".",
"has_field?",
"(",
"v",
")",
"}",
".",
"keys",
")",
"names",
".",
"sort!",
"names",
"end"
] |
Returns a sorted list of all field names referenced by this Entry's cross-reference.
|
[
"Returns",
"a",
"sorted",
"list",
"of",
"all",
"field",
"names",
"referenced",
"by",
"this",
"Entry",
"s",
"cross",
"-",
"reference",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L291-L299
|
21,544 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.rename!
|
def rename!(*arguments)
Hash[*arguments.flatten].each_pair do |from,to|
if fields.has_key?(from) && !fields.has_key?(to)
fields[to] = fields[from]
fields.delete(from)
end
end
self
end
|
ruby
|
def rename!(*arguments)
Hash[*arguments.flatten].each_pair do |from,to|
if fields.has_key?(from) && !fields.has_key?(to)
fields[to] = fields[from]
fields.delete(from)
end
end
self
end
|
[
"def",
"rename!",
"(",
"*",
"arguments",
")",
"Hash",
"[",
"arguments",
".",
"flatten",
"]",
".",
"each_pair",
"do",
"|",
"from",
",",
"to",
"|",
"if",
"fields",
".",
"has_key?",
"(",
"from",
")",
"&&",
"!",
"fields",
".",
"has_key?",
"(",
"to",
")",
"fields",
"[",
"to",
"]",
"=",
"fields",
"[",
"from",
"]",
"fields",
".",
"delete",
"(",
"from",
")",
"end",
"end",
"self",
"end"
] |
Renames the given field names unless a field with the new name already
exists.
|
[
"Renames",
"the",
"given",
"field",
"names",
"unless",
"a",
"field",
"with",
"the",
"new",
"name",
"already",
"exists",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L330-L338
|
21,545 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.valid?
|
def valid?
REQUIRED_FIELDS[type].all? do |f|
f.is_a?(Array) ? !(f & fields.keys).empty? : !fields[f].nil?
end
end
|
ruby
|
def valid?
REQUIRED_FIELDS[type].all? do |f|
f.is_a?(Array) ? !(f & fields.keys).empty? : !fields[f].nil?
end
end
|
[
"def",
"valid?",
"REQUIRED_FIELDS",
"[",
"type",
"]",
".",
"all?",
"do",
"|",
"f",
"|",
"f",
".",
"is_a?",
"(",
"Array",
")",
"?",
"!",
"(",
"f",
"&",
"fields",
".",
"keys",
")",
".",
"empty?",
":",
"!",
"fields",
"[",
"f",
"]",
".",
"nil?",
"end",
"end"
] |
Returns false if the entry is one of the standard entry types and does not have
definitions of all the required fields for that type.
|
[
"Returns",
"false",
"if",
"the",
"entry",
"is",
"one",
"of",
"the",
"standard",
"entry",
"types",
"and",
"does",
"not",
"have",
"definitions",
"of",
"all",
"the",
"required",
"fields",
"for",
"that",
"type",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L389-L393
|
21,546 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.digest
|
def digest(filter = [])
names = field_names(filter)
digest = type.to_s
names.zip(values_at(*names)).each do |key, value|
digest << "|#{key}:#{value}"
end
digest = yield(digest, self) if block_given?
digest
end
|
ruby
|
def digest(filter = [])
names = field_names(filter)
digest = type.to_s
names.zip(values_at(*names)).each do |key, value|
digest << "|#{key}:#{value}"
end
digest = yield(digest, self) if block_given?
digest
end
|
[
"def",
"digest",
"(",
"filter",
"=",
"[",
"]",
")",
"names",
"=",
"field_names",
"(",
"filter",
")",
"digest",
"=",
"type",
".",
"to_s",
"names",
".",
"zip",
"(",
"values_at",
"(",
"names",
")",
")",
".",
"each",
"do",
"|",
"key",
",",
"value",
"|",
"digest",
"<<",
"\"|#{key}:#{value}\"",
"end",
"digest",
"=",
"yield",
"(",
"digest",
",",
"self",
")",
"if",
"block_given?",
"digest",
"end"
] |
Creates the entry's digest based on the passed-in filters.
The digest contains the type and all key-value pairs based
on the passed in filter.
If a block is given, the computed digest will be passed to
the block for post-processing (the entry itself will be passed
as the second parameter).
@see #field_names
@param [<Symbol>] the field names to use
@return [String] the digest string
|
[
"Creates",
"the",
"entry",
"s",
"digest",
"based",
"on",
"the",
"passed",
"-",
"in",
"filters",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L408-L418
|
21,547 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.added_to_bibliography
|
def added_to_bibliography(bibliography)
super
@key = register(key)
[:parse_names, :parse_months].each do |parser|
send(parser) if bibliography.options[parser]
end
if bibliography.options.has_key?(:filter)
[*bibliography.options[:filter]].each do |filter|
convert!(filter)
end
end
self
end
|
ruby
|
def added_to_bibliography(bibliography)
super
@key = register(key)
[:parse_names, :parse_months].each do |parser|
send(parser) if bibliography.options[parser]
end
if bibliography.options.has_key?(:filter)
[*bibliography.options[:filter]].each do |filter|
convert!(filter)
end
end
self
end
|
[
"def",
"added_to_bibliography",
"(",
"bibliography",
")",
"super",
"@key",
"=",
"register",
"(",
"key",
")",
"[",
":parse_names",
",",
":parse_months",
"]",
".",
"each",
"do",
"|",
"parser",
"|",
"send",
"(",
"parser",
")",
"if",
"bibliography",
".",
"options",
"[",
"parser",
"]",
"end",
"if",
"bibliography",
".",
"options",
".",
"has_key?",
"(",
":filter",
")",
"[",
"bibliography",
".",
"options",
"[",
":filter",
"]",
"]",
".",
"each",
"do",
"|",
"filter",
"|",
"convert!",
"(",
"filter",
")",
"end",
"end",
"self",
"end"
] |
Called when the element was added to a bibliography.
|
[
"Called",
"when",
"the",
"element",
"was",
"added",
"to",
"a",
"bibliography",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L434-L450
|
21,548 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.register
|
def register(key)
return nil if bibliography.nil?
k = key.dup
k.succ! while bibliography.has_key?(k)
bibliography.entries[k] = self
k
end
|
ruby
|
def register(key)
return nil if bibliography.nil?
k = key.dup
k.succ! while bibliography.has_key?(k)
bibliography.entries[k] = self
k
end
|
[
"def",
"register",
"(",
"key",
")",
"return",
"nil",
"if",
"bibliography",
".",
"nil?",
"k",
"=",
"key",
".",
"dup",
"k",
".",
"succ!",
"while",
"bibliography",
".",
"has_key?",
"(",
"k",
")",
"bibliography",
".",
"entries",
"[",
"k",
"]",
"=",
"self",
"k",
"end"
] |
Registers this Entry in the associated Bibliographies entries hash.
This method may change the Entry's key, if another entry is already
registered with the current key.
Returns the key or nil if the Entry is not associated with a Bibliography.
|
[
"Registers",
"this",
"Entry",
"in",
"the",
"associated",
"Bibliographies",
"entries",
"hash",
".",
"This",
"method",
"may",
"change",
"the",
"Entry",
"s",
"key",
"if",
"another",
"entry",
"is",
"already",
"registered",
"with",
"the",
"current",
"key",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L469-L476
|
21,549 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.parse_names
|
def parse_names
strings = bibliography ? bibliography.strings.values : []
NAME_FIELDS.each do |key|
if name = fields[key]
name = name.dup.replace(strings).join.to_name
fields[key] = name unless name.nil?
end
end
self
end
|
ruby
|
def parse_names
strings = bibliography ? bibliography.strings.values : []
NAME_FIELDS.each do |key|
if name = fields[key]
name = name.dup.replace(strings).join.to_name
fields[key] = name unless name.nil?
end
end
self
end
|
[
"def",
"parse_names",
"strings",
"=",
"bibliography",
"?",
"bibliography",
".",
"strings",
".",
"values",
":",
"[",
"]",
"NAME_FIELDS",
".",
"each",
"do",
"|",
"key",
"|",
"if",
"name",
"=",
"fields",
"[",
"key",
"]",
"name",
"=",
"name",
".",
"dup",
".",
"replace",
"(",
"strings",
")",
".",
"join",
".",
"to_name",
"fields",
"[",
"key",
"]",
"=",
"name",
"unless",
"name",
".",
"nil?",
"end",
"end",
"self",
"end"
] |
Parses all name values of the entry. Tries to replace and join the
value prior to parsing.
|
[
"Parses",
"all",
"name",
"values",
"of",
"the",
"entry",
".",
"Tries",
"to",
"replace",
"and",
"join",
"the",
"value",
"prior",
"to",
"parsing",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L522-L533
|
21,550 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.convert!
|
def convert!(*filters)
filters = filters.flatten.map { |f| Filters.resolve!(f) }
fields.each_pair do |k, v|
(!block_given? || yield(k, v)) ? v.convert!(*filters) : v
end
self
end
|
ruby
|
def convert!(*filters)
filters = filters.flatten.map { |f| Filters.resolve!(f) }
fields.each_pair do |k, v|
(!block_given? || yield(k, v)) ? v.convert!(*filters) : v
end
self
end
|
[
"def",
"convert!",
"(",
"*",
"filters",
")",
"filters",
"=",
"filters",
".",
"flatten",
".",
"map",
"{",
"|",
"f",
"|",
"Filters",
".",
"resolve!",
"(",
"f",
")",
"}",
"fields",
".",
"each_pair",
"do",
"|",
"k",
",",
"v",
"|",
"(",
"!",
"block_given?",
"||",
"yield",
"(",
"k",
",",
"v",
")",
")",
"?",
"v",
".",
"convert!",
"(",
"filters",
")",
":",
"v",
"end",
"self",
"end"
] |
In-place variant of @see #convert
|
[
"In",
"-",
"place",
"variant",
"of"
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L617-L625
|
21,551 |
inukshuk/bibtex-ruby
|
lib/bibtex/entry.rb
|
BibTeX.Entry.default_key
|
def default_key
k = names[0]
k = k.respond_to?(:family) ? k.family : k.to_s
k = BibTeX.transliterate(k).gsub(/["']/, '')
k = k[/[A-Za-z-]+/] || 'unknown'
k << (year.to_s[/\d+/] || '-')
k << 'a'
k.downcase!
k
end
|
ruby
|
def default_key
k = names[0]
k = k.respond_to?(:family) ? k.family : k.to_s
k = BibTeX.transliterate(k).gsub(/["']/, '')
k = k[/[A-Za-z-]+/] || 'unknown'
k << (year.to_s[/\d+/] || '-')
k << 'a'
k.downcase!
k
end
|
[
"def",
"default_key",
"k",
"=",
"names",
"[",
"0",
"]",
"k",
"=",
"k",
".",
"respond_to?",
"(",
":family",
")",
"?",
"k",
".",
"family",
":",
"k",
".",
"to_s",
"k",
"=",
"BibTeX",
".",
"transliterate",
"(",
"k",
")",
".",
"gsub",
"(",
"/",
"/",
",",
"''",
")",
"k",
"=",
"k",
"[",
"/",
"/",
"]",
"||",
"'unknown'",
"k",
"<<",
"(",
"year",
".",
"to_s",
"[",
"/",
"\\d",
"/",
"]",
"||",
"'-'",
")",
"k",
"<<",
"'a'",
"k",
".",
"downcase!",
"k",
"end"
] |
Returns a default key for this entry.
|
[
"Returns",
"a",
"default",
"key",
"for",
"this",
"entry",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/entry.rb#L679-L688
|
21,552 |
inukshuk/bibtex-ruby
|
lib/bibtex/bibliography.rb
|
BibTeX.Bibliography.initialize_copy
|
def initialize_copy(other)
@options = other.options.dup
@errors = other.errors.dup
@data, @strings = [], {}
@entries = Hash.new { |h,k| h.fetch(k.to_s, nil) }
other.each do |element|
add element.dup
end
self
end
|
ruby
|
def initialize_copy(other)
@options = other.options.dup
@errors = other.errors.dup
@data, @strings = [], {}
@entries = Hash.new { |h,k| h.fetch(k.to_s, nil) }
other.each do |element|
add element.dup
end
self
end
|
[
"def",
"initialize_copy",
"(",
"other",
")",
"@options",
"=",
"other",
".",
"options",
".",
"dup",
"@errors",
"=",
"other",
".",
"errors",
".",
"dup",
"@data",
",",
"@strings",
"=",
"[",
"]",
",",
"{",
"}",
"@entries",
"=",
"Hash",
".",
"new",
"{",
"|",
"h",
",",
"k",
"|",
"h",
".",
"fetch",
"(",
"k",
".",
"to_s",
",",
"nil",
")",
"}",
"other",
".",
"each",
"do",
"|",
"element",
"|",
"add",
"element",
".",
"dup",
"end",
"self",
"end"
] |
Creates a new bibliography.
|
[
"Creates",
"a",
"new",
"bibliography",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/bibliography.rb#L105-L116
|
21,553 |
inukshuk/bibtex-ruby
|
lib/bibtex/bibliography.rb
|
BibTeX.Bibliography.add
|
def add(*arguments)
Element.parse(arguments.flatten, @options).each do |element|
data << element.added_to_bibliography(self)
end
self
end
|
ruby
|
def add(*arguments)
Element.parse(arguments.flatten, @options).each do |element|
data << element.added_to_bibliography(self)
end
self
end
|
[
"def",
"add",
"(",
"*",
"arguments",
")",
"Element",
".",
"parse",
"(",
"arguments",
".",
"flatten",
",",
"@options",
")",
".",
"each",
"do",
"|",
"element",
"|",
"data",
"<<",
"element",
".",
"added_to_bibliography",
"(",
"self",
")",
"end",
"self",
"end"
] |
Adds a new element, or a list of new elements to the bibliography.
Returns the Bibliography for chainability.
|
[
"Adds",
"a",
"new",
"element",
"or",
"a",
"list",
"of",
"new",
"elements",
"to",
"the",
"bibliography",
".",
"Returns",
"the",
"Bibliography",
"for",
"chainability",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/bibliography.rb#L120-L125
|
21,554 |
inukshuk/bibtex-ruby
|
lib/bibtex/bibliography.rb
|
BibTeX.Bibliography.save_to
|
def save_to(path, options = {})
options[:quotes] ||= %w({ })
File.open(path, 'w:UTF-8') do |f|
f.write(to_s(options))
end
self
end
|
ruby
|
def save_to(path, options = {})
options[:quotes] ||= %w({ })
File.open(path, 'w:UTF-8') do |f|
f.write(to_s(options))
end
self
end
|
[
"def",
"save_to",
"(",
"path",
",",
"options",
"=",
"{",
"}",
")",
"options",
"[",
":quotes",
"]",
"||=",
"%w(",
"{",
"}",
")",
"File",
".",
"open",
"(",
"path",
",",
"'w:UTF-8'",
")",
"do",
"|",
"f",
"|",
"f",
".",
"write",
"(",
"to_s",
"(",
"options",
")",
")",
"end",
"self",
"end"
] |
Saves the bibliography to a file at the given path. Returns the bibliography.
|
[
"Saves",
"the",
"bibliography",
"to",
"a",
"file",
"at",
"the",
"given",
"path",
".",
"Returns",
"the",
"bibliography",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/bibliography.rb#L137-L145
|
21,555 |
inukshuk/bibtex-ruby
|
lib/bibtex/bibliography.rb
|
BibTeX.Bibliography.delete
|
def delete(*arguments, &block)
objects = q(*arguments, &block).map { |o| o.removed_from_bibliography(self) }
@data = @data - objects
objects.length == 1 ? objects[0] : objects
end
|
ruby
|
def delete(*arguments, &block)
objects = q(*arguments, &block).map { |o| o.removed_from_bibliography(self) }
@data = @data - objects
objects.length == 1 ? objects[0] : objects
end
|
[
"def",
"delete",
"(",
"*",
"arguments",
",",
"&",
"block",
")",
"objects",
"=",
"q",
"(",
"arguments",
",",
"block",
")",
".",
"map",
"{",
"|",
"o",
"|",
"o",
".",
"removed_from_bibliography",
"(",
"self",
")",
"}",
"@data",
"=",
"@data",
"-",
"objects",
"objects",
".",
"length",
"==",
"1",
"?",
"objects",
"[",
"0",
"]",
":",
"objects",
"end"
] |
Deletes an object, or a list of objects from the bibliography.
If a list of objects is to be deleted, you can either supply the list
of objects or use a query or block to define the list.
Returns the object (or the list of objects) that were deleted; nil
if the object was not part of the bibliography.
|
[
"Deletes",
"an",
"object",
"or",
"a",
"list",
"of",
"objects",
"from",
"the",
"bibliography",
".",
"If",
"a",
"list",
"of",
"objects",
"is",
"to",
"be",
"deleted",
"you",
"can",
"either",
"supply",
"the",
"list",
"of",
"objects",
"or",
"use",
"a",
"query",
"or",
"block",
"to",
"define",
"the",
"list",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/bibliography.rb#L189-L193
|
21,556 |
inukshuk/bibtex-ruby
|
lib/bibtex/bibliography.rb
|
BibTeX.Bibliography.extend_initials!
|
def extend_initials!
groups = Hash.new do |h,k|
h[k] = { :prototype => nil, :names => [] }
end
# group names together
names.each do |name|
group = groups[name.sort_order(:initials => true).downcase]
group[:names] << name
if group[:prototype].nil? || group[:prototype].first.to_s.length < name.first.to_s.length
group[:prototype] = name
end
end
# extend all names in group to prototype
groups.each_value do |group|
group[:names].each do |name|
name.set(group[:prototype])
end
end
self
end
|
ruby
|
def extend_initials!
groups = Hash.new do |h,k|
h[k] = { :prototype => nil, :names => [] }
end
# group names together
names.each do |name|
group = groups[name.sort_order(:initials => true).downcase]
group[:names] << name
if group[:prototype].nil? || group[:prototype].first.to_s.length < name.first.to_s.length
group[:prototype] = name
end
end
# extend all names in group to prototype
groups.each_value do |group|
group[:names].each do |name|
name.set(group[:prototype])
end
end
self
end
|
[
"def",
"extend_initials!",
"groups",
"=",
"Hash",
".",
"new",
"do",
"|",
"h",
",",
"k",
"|",
"h",
"[",
"k",
"]",
"=",
"{",
":prototype",
"=>",
"nil",
",",
":names",
"=>",
"[",
"]",
"}",
"end",
"# group names together",
"names",
".",
"each",
"do",
"|",
"name",
"|",
"group",
"=",
"groups",
"[",
"name",
".",
"sort_order",
"(",
":initials",
"=>",
"true",
")",
".",
"downcase",
"]",
"group",
"[",
":names",
"]",
"<<",
"name",
"if",
"group",
"[",
":prototype",
"]",
".",
"nil?",
"||",
"group",
"[",
":prototype",
"]",
".",
"first",
".",
"to_s",
".",
"length",
"<",
"name",
".",
"first",
".",
"to_s",
".",
"length",
"group",
"[",
":prototype",
"]",
"=",
"name",
"end",
"end",
"# extend all names in group to prototype",
"groups",
".",
"each_value",
"do",
"|",
"group",
"|",
"group",
"[",
":names",
"]",
".",
"each",
"do",
"|",
"name",
"|",
"name",
".",
"set",
"(",
"group",
"[",
":prototype",
"]",
")",
"end",
"end",
"self",
"end"
] |
This method combines all names in the bibliography that look identical
when using initials as first names and then tries to extend the first
names for all names in each group to the longest available form.
Returns the bibliography.
If your bibliography contains the names 'Poe, Edgar A.', 'Poe, E.A.',
and 'Poe, E. A.' calling this method would convert all three names to
'Poe, Edgar A.'.
|
[
"This",
"method",
"combines",
"all",
"names",
"in",
"the",
"bibliography",
"that",
"look",
"identical",
"when",
"using",
"initials",
"as",
"first",
"names",
"and",
"then",
"tries",
"to",
"extend",
"the",
"first",
"names",
"for",
"all",
"names",
"in",
"each",
"group",
"to",
"the",
"longest",
"available",
"form",
".",
"Returns",
"the",
"bibliography",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/bibliography.rb#L320-L343
|
21,557 |
inukshuk/bibtex-ruby
|
lib/bibtex/elements.rb
|
BibTeX.Element.matches?
|
def matches?(query)
return true if query.nil? || query.respond_to?(:empty?) && query.empty?
case query
when Symbol
query.to_s == id.to_s
when Element
query == self
when Regexp
to_s.match(query)
when /^\/(.+)\/$/
to_s.match(Regexp.new($1))
when /@(\*|\w+)(?:\[([^\]]*)\])?/
query.scan(/(!)?@(\*|\w+)(?:\[([^\]]*)\])?/).any? do |non, type, condition|
if (non ? !has_type?(type) : has_type?(type))
if condition.nil? || condition.empty?
true
else
condition.to_s.split(/\s*\|\|\s*/).any? do |conditions|
meets_all? conditions.split(/\s*(?:,|&&)\s*/)
end
end
end
end
else
id.to_s == query
end
end
|
ruby
|
def matches?(query)
return true if query.nil? || query.respond_to?(:empty?) && query.empty?
case query
when Symbol
query.to_s == id.to_s
when Element
query == self
when Regexp
to_s.match(query)
when /^\/(.+)\/$/
to_s.match(Regexp.new($1))
when /@(\*|\w+)(?:\[([^\]]*)\])?/
query.scan(/(!)?@(\*|\w+)(?:\[([^\]]*)\])?/).any? do |non, type, condition|
if (non ? !has_type?(type) : has_type?(type))
if condition.nil? || condition.empty?
true
else
condition.to_s.split(/\s*\|\|\s*/).any? do |conditions|
meets_all? conditions.split(/\s*(?:,|&&)\s*/)
end
end
end
end
else
id.to_s == query
end
end
|
[
"def",
"matches?",
"(",
"query",
")",
"return",
"true",
"if",
"query",
".",
"nil?",
"||",
"query",
".",
"respond_to?",
"(",
":empty?",
")",
"&&",
"query",
".",
"empty?",
"case",
"query",
"when",
"Symbol",
"query",
".",
"to_s",
"==",
"id",
".",
"to_s",
"when",
"Element",
"query",
"==",
"self",
"when",
"Regexp",
"to_s",
".",
"match",
"(",
"query",
")",
"when",
"/",
"\\/",
"\\/",
"/",
"to_s",
".",
"match",
"(",
"Regexp",
".",
"new",
"(",
"$1",
")",
")",
"when",
"/",
"\\*",
"\\w",
"\\[",
"\\]",
"\\]",
"/",
"query",
".",
"scan",
"(",
"/",
"\\*",
"\\w",
"\\[",
"\\]",
"\\]",
"/",
")",
".",
"any?",
"do",
"|",
"non",
",",
"type",
",",
"condition",
"|",
"if",
"(",
"non",
"?",
"!",
"has_type?",
"(",
"type",
")",
":",
"has_type?",
"(",
"type",
")",
")",
"if",
"condition",
".",
"nil?",
"||",
"condition",
".",
"empty?",
"true",
"else",
"condition",
".",
"to_s",
".",
"split",
"(",
"/",
"\\s",
"\\|",
"\\|",
"\\s",
"/",
")",
".",
"any?",
"do",
"|",
"conditions",
"|",
"meets_all?",
"conditions",
".",
"split",
"(",
"/",
"\\s",
"\\s",
"/",
")",
"end",
"end",
"end",
"end",
"else",
"id",
".",
"to_s",
"==",
"query",
"end",
"end"
] |
Returns true if the element matches the given query.
|
[
"Returns",
"true",
"if",
"the",
"element",
"matches",
"the",
"given",
"query",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/elements.rb#L92-L119
|
21,558 |
inukshuk/bibtex-ruby
|
lib/bibtex/names.rb
|
BibTeX.Name.set
|
def set(attributes = {})
attributes.each_pair do |key, value|
send("#{key}=", value) if respond_to?(key)
end
self
end
|
ruby
|
def set(attributes = {})
attributes.each_pair do |key, value|
send("#{key}=", value) if respond_to?(key)
end
self
end
|
[
"def",
"set",
"(",
"attributes",
"=",
"{",
"}",
")",
"attributes",
".",
"each_pair",
"do",
"|",
"key",
",",
"value",
"|",
"send",
"(",
"\"#{key}=\"",
",",
"value",
")",
"if",
"respond_to?",
"(",
"key",
")",
"end",
"self",
"end"
] |
Set the name tokens to the values defined in the passed-in hash.
|
[
"Set",
"the",
"name",
"tokens",
"to",
"the",
"values",
"defined",
"in",
"the",
"passed",
"-",
"in",
"hash",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/names.rb#L155-L161
|
21,559 |
inukshuk/bibtex-ruby
|
lib/bibtex/names.rb
|
BibTeX.Name.extend_initials
|
def extend_initials(with_first, for_last)
rename_if :first => with_first do |name|
if name.last == for_last
mine = name.initials.split(/\.[^[:alpha:]]*/)
other = initials(with_first).split(/\.[^[:alpha:]]*/)
mine == other || mine.length < other.length && mine == other[0, mine.length]
end
end
end
|
ruby
|
def extend_initials(with_first, for_last)
rename_if :first => with_first do |name|
if name.last == for_last
mine = name.initials.split(/\.[^[:alpha:]]*/)
other = initials(with_first).split(/\.[^[:alpha:]]*/)
mine == other || mine.length < other.length && mine == other[0, mine.length]
end
end
end
|
[
"def",
"extend_initials",
"(",
"with_first",
",",
"for_last",
")",
"rename_if",
":first",
"=>",
"with_first",
"do",
"|",
"name",
"|",
"if",
"name",
".",
"last",
"==",
"for_last",
"mine",
"=",
"name",
".",
"initials",
".",
"split",
"(",
"/",
"\\.",
"/",
")",
"other",
"=",
"initials",
"(",
"with_first",
")",
".",
"split",
"(",
"/",
"\\.",
"/",
")",
"mine",
"==",
"other",
"||",
"mine",
".",
"length",
"<",
"other",
".",
"length",
"&&",
"mine",
"==",
"other",
"[",
"0",
",",
"mine",
".",
"length",
"]",
"end",
"end",
"end"
] |
Sets the name's first name to the passed-in name if the last name equals
for_last and the current first name has the same initials as with_first.
|
[
"Sets",
"the",
"name",
"s",
"first",
"name",
"to",
"the",
"passed",
"-",
"in",
"name",
"if",
"the",
"last",
"name",
"equals",
"for_last",
"and",
"the",
"current",
"first",
"name",
"has",
"the",
"same",
"initials",
"as",
"with_first",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/names.rb#L187-L196
|
21,560 |
inukshuk/bibtex-ruby
|
lib/bibtex/names.rb
|
BibTeX.Name.rename_if
|
def rename_if(attributes, conditions = {})
if block_given?
set(attributes) if yield self
else
set(attributes) if conditions.all? do |key, value|
respond_to?(key) && send(key) == value
end
end
self
end
|
ruby
|
def rename_if(attributes, conditions = {})
if block_given?
set(attributes) if yield self
else
set(attributes) if conditions.all? do |key, value|
respond_to?(key) && send(key) == value
end
end
self
end
|
[
"def",
"rename_if",
"(",
"attributes",
",",
"conditions",
"=",
"{",
"}",
")",
"if",
"block_given?",
"set",
"(",
"attributes",
")",
"if",
"yield",
"self",
"else",
"set",
"(",
"attributes",
")",
"if",
"conditions",
".",
"all?",
"do",
"|",
"key",
",",
"value",
"|",
"respond_to?",
"(",
"key",
")",
"&&",
"send",
"(",
"key",
")",
"==",
"value",
"end",
"end",
"self",
"end"
] |
Renames the tokens according to the passed-in attributes if all of the
conditions match or if the given block returns true.
|
[
"Renames",
"the",
"tokens",
"according",
"to",
"the",
"passed",
"-",
"in",
"attributes",
"if",
"all",
"of",
"the",
"conditions",
"match",
"or",
"if",
"the",
"given",
"block",
"returns",
"true",
"."
] |
69bd06d35499929f1eb1df6b98fa7b5a2d768b1d
|
https://github.com/inukshuk/bibtex-ruby/blob/69bd06d35499929f1eb1df6b98fa7b5a2d768b1d/lib/bibtex/names.rb#L200-L210
|
21,561 |
redding/assert
|
lib/assert/context.rb
|
Assert.Context.assert
|
def assert(assertion, desc = nil)
if assertion
pass
else
what = if block_given?
yield
else
"Failed assert: assertion was "\
"`#{Assert::U.show(assertion, __assert_config__)}`."
end
fail(fail_message(desc, what))
end
end
|
ruby
|
def assert(assertion, desc = nil)
if assertion
pass
else
what = if block_given?
yield
else
"Failed assert: assertion was "\
"`#{Assert::U.show(assertion, __assert_config__)}`."
end
fail(fail_message(desc, what))
end
end
|
[
"def",
"assert",
"(",
"assertion",
",",
"desc",
"=",
"nil",
")",
"if",
"assertion",
"pass",
"else",
"what",
"=",
"if",
"block_given?",
"yield",
"else",
"\"Failed assert: assertion was \"",
"\"`#{Assert::U.show(assertion, __assert_config__)}`.\"",
"end",
"fail",
"(",
"fail_message",
"(",
"desc",
",",
"what",
")",
")",
"end",
"end"
] |
check if the assertion is a truthy value, if so create a new pass result,
otherwise create a new fail result with the desc and what failed msg.
all other assertion helpers use this one in the end
|
[
"check",
"if",
"the",
"assertion",
"is",
"a",
"truthy",
"value",
"if",
"so",
"create",
"a",
"new",
"pass",
"result",
"otherwise",
"create",
"a",
"new",
"fail",
"result",
"with",
"the",
"desc",
"and",
"what",
"failed",
"msg",
".",
"all",
"other",
"assertion",
"helpers",
"use",
"this",
"one",
"in",
"the",
"end"
] |
52c0adf2db40bdd3490021539b3a26a0b530d5a4
|
https://github.com/redding/assert/blob/52c0adf2db40bdd3490021539b3a26a0b530d5a4/lib/assert/context.rb#L68-L80
|
21,562 |
redding/assert
|
lib/assert/context.rb
|
Assert.Context.pass
|
def pass(pass_msg = nil)
if @__assert_pending__ == 0
capture_result(Assert::Result::Pass, pass_msg)
else
capture_result(Assert::Result::Fail, "Pending pass (make it "\
"not pending)")
end
end
|
ruby
|
def pass(pass_msg = nil)
if @__assert_pending__ == 0
capture_result(Assert::Result::Pass, pass_msg)
else
capture_result(Assert::Result::Fail, "Pending pass (make it "\
"not pending)")
end
end
|
[
"def",
"pass",
"(",
"pass_msg",
"=",
"nil",
")",
"if",
"@__assert_pending__",
"==",
"0",
"capture_result",
"(",
"Assert",
"::",
"Result",
"::",
"Pass",
",",
"pass_msg",
")",
"else",
"capture_result",
"(",
"Assert",
"::",
"Result",
"::",
"Fail",
",",
"\"Pending pass (make it \"",
"\"not pending)\"",
")",
"end",
"end"
] |
adds a Pass result to the end of the test's results
does not break test execution
|
[
"adds",
"a",
"Pass",
"result",
"to",
"the",
"end",
"of",
"the",
"test",
"s",
"results",
"does",
"not",
"break",
"test",
"execution"
] |
52c0adf2db40bdd3490021539b3a26a0b530d5a4
|
https://github.com/redding/assert/blob/52c0adf2db40bdd3490021539b3a26a0b530d5a4/lib/assert/context.rb#L94-L101
|
21,563 |
redding/assert
|
lib/assert/context.rb
|
Assert.Context.fail
|
def fail(message = nil)
if @__assert_pending__ == 0
if halt_on_fail?
raise Result::TestFailure, message || ""
else
capture_result(Assert::Result::Fail, message || "")
end
else
if halt_on_fail?
raise Result::TestSkipped, "Pending fail: #{message || ""}"
else
capture_result(Assert::Result::Skip, "Pending fail: #{message || ""}")
end
end
end
|
ruby
|
def fail(message = nil)
if @__assert_pending__ == 0
if halt_on_fail?
raise Result::TestFailure, message || ""
else
capture_result(Assert::Result::Fail, message || "")
end
else
if halt_on_fail?
raise Result::TestSkipped, "Pending fail: #{message || ""}"
else
capture_result(Assert::Result::Skip, "Pending fail: #{message || ""}")
end
end
end
|
[
"def",
"fail",
"(",
"message",
"=",
"nil",
")",
"if",
"@__assert_pending__",
"==",
"0",
"if",
"halt_on_fail?",
"raise",
"Result",
"::",
"TestFailure",
",",
"message",
"||",
"\"\"",
"else",
"capture_result",
"(",
"Assert",
"::",
"Result",
"::",
"Fail",
",",
"message",
"||",
"\"\"",
")",
"end",
"else",
"if",
"halt_on_fail?",
"raise",
"Result",
"::",
"TestSkipped",
",",
"\"Pending fail: #{message || \"\"}\"",
"else",
"capture_result",
"(",
"Assert",
"::",
"Result",
"::",
"Skip",
",",
"\"Pending fail: #{message || \"\"}\"",
")",
"end",
"end",
"end"
] |
adds a Fail result to the end of the test's results
break test execution if assert is configured to halt on failures
|
[
"adds",
"a",
"Fail",
"result",
"to",
"the",
"end",
"of",
"the",
"test",
"s",
"results",
"break",
"test",
"execution",
"if",
"assert",
"is",
"configured",
"to",
"halt",
"on",
"failures"
] |
52c0adf2db40bdd3490021539b3a26a0b530d5a4
|
https://github.com/redding/assert/blob/52c0adf2db40bdd3490021539b3a26a0b530d5a4/lib/assert/context.rb#L111-L125
|
21,564 |
justinweiss/resque_unit
|
lib/resque_unit/scheduler.rb
|
ResqueUnit.Scheduler.enqueue_in
|
def enqueue_in(number_of_seconds_from_now, klass, *args)
enqueue_at(Time.now + number_of_seconds_from_now, klass, *args)
end
|
ruby
|
def enqueue_in(number_of_seconds_from_now, klass, *args)
enqueue_at(Time.now + number_of_seconds_from_now, klass, *args)
end
|
[
"def",
"enqueue_in",
"(",
"number_of_seconds_from_now",
",",
"klass",
",",
"*",
"args",
")",
"enqueue_at",
"(",
"Time",
".",
"now",
"+",
"number_of_seconds_from_now",
",",
"klass",
",",
"args",
")",
"end"
] |
Identical to enqueue_at but takes number_of_seconds_from_now
instead of a timestamp.
|
[
"Identical",
"to",
"enqueue_at",
"but",
"takes",
"number_of_seconds_from_now",
"instead",
"of",
"a",
"timestamp",
"."
] |
064f54b6405980dfdd37afabe03b959884b3b166
|
https://github.com/justinweiss/resque_unit/blob/064f54b6405980dfdd37afabe03b959884b3b166/lib/resque_unit/scheduler.rb#L21-L23
|
21,565 |
justinweiss/resque_unit
|
lib/resque_unit/helpers.rb
|
ResqueUnit.Helpers.decode
|
def decode(object)
return unless object
if defined? Yajl
begin
Yajl::Parser.parse(object, :check_utf8 => false)
rescue Yajl::ParseError
end
else
begin
JSON.parse(object)
rescue JSON::ParserError
end
end
end
|
ruby
|
def decode(object)
return unless object
if defined? Yajl
begin
Yajl::Parser.parse(object, :check_utf8 => false)
rescue Yajl::ParseError
end
else
begin
JSON.parse(object)
rescue JSON::ParserError
end
end
end
|
[
"def",
"decode",
"(",
"object",
")",
"return",
"unless",
"object",
"if",
"defined?",
"Yajl",
"begin",
"Yajl",
"::",
"Parser",
".",
"parse",
"(",
"object",
",",
":check_utf8",
"=>",
"false",
")",
"rescue",
"Yajl",
"::",
"ParseError",
"end",
"else",
"begin",
"JSON",
".",
"parse",
"(",
"object",
")",
"rescue",
"JSON",
"::",
"ParserError",
"end",
"end",
"end"
] |
Given a string, returns a Ruby object.
|
[
"Given",
"a",
"string",
"returns",
"a",
"Ruby",
"object",
"."
] |
064f54b6405980dfdd37afabe03b959884b3b166
|
https://github.com/justinweiss/resque_unit/blob/064f54b6405980dfdd37afabe03b959884b3b166/lib/resque_unit/helpers.rb#L14-L28
|
21,566 |
justinweiss/resque_unit
|
lib/resque_unit/helpers.rb
|
ResqueUnit.Helpers.constantize
|
def constantize(camel_cased_word)
camel_cased_word = camel_cased_word.to_s
if camel_cased_word.include?('-')
camel_cased_word = classify(camel_cased_word)
end
names = camel_cased_word.split('::')
names.shift if names.empty? || names.first.empty?
constant = Object
names.each do |name|
constant = constant.const_get(name) || constant.const_missing(name)
end
constant
end
|
ruby
|
def constantize(camel_cased_word)
camel_cased_word = camel_cased_word.to_s
if camel_cased_word.include?('-')
camel_cased_word = classify(camel_cased_word)
end
names = camel_cased_word.split('::')
names.shift if names.empty? || names.first.empty?
constant = Object
names.each do |name|
constant = constant.const_get(name) || constant.const_missing(name)
end
constant
end
|
[
"def",
"constantize",
"(",
"camel_cased_word",
")",
"camel_cased_word",
"=",
"camel_cased_word",
".",
"to_s",
"if",
"camel_cased_word",
".",
"include?",
"(",
"'-'",
")",
"camel_cased_word",
"=",
"classify",
"(",
"camel_cased_word",
")",
"end",
"names",
"=",
"camel_cased_word",
".",
"split",
"(",
"'::'",
")",
"names",
".",
"shift",
"if",
"names",
".",
"empty?",
"||",
"names",
".",
"first",
".",
"empty?",
"constant",
"=",
"Object",
"names",
".",
"each",
"do",
"|",
"name",
"|",
"constant",
"=",
"constant",
".",
"const_get",
"(",
"name",
")",
"||",
"constant",
".",
"const_missing",
"(",
"name",
")",
"end",
"constant",
"end"
] |
Given a camel cased word, returns the constant it represents
constantize('JobName') # => JobName
|
[
"Given",
"a",
"camel",
"cased",
"word",
"returns",
"the",
"constant",
"it",
"represents"
] |
064f54b6405980dfdd37afabe03b959884b3b166
|
https://github.com/justinweiss/resque_unit/blob/064f54b6405980dfdd37afabe03b959884b3b166/lib/resque_unit/helpers.rb#L40-L55
|
21,567 |
joshwlewis/unitwise
|
lib/unitwise/term.rb
|
Unitwise.Term.atom=
|
def atom=(value)
value.is_a?(Atom) ? super(value) : super(Atom.find(value.to_s))
end
|
ruby
|
def atom=(value)
value.is_a?(Atom) ? super(value) : super(Atom.find(value.to_s))
end
|
[
"def",
"atom",
"=",
"(",
"value",
")",
"value",
".",
"is_a?",
"(",
"Atom",
")",
"?",
"super",
"(",
"value",
")",
":",
"super",
"(",
"Atom",
".",
"find",
"(",
"value",
".",
"to_s",
")",
")",
"end"
] |
Set the atom.
@param value [String, Atom] Either a string representing an Atom, or an
Atom
@api public
|
[
"Set",
"the",
"atom",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/term.rb#L12-L14
|
21,568 |
joshwlewis/unitwise
|
lib/unitwise/term.rb
|
Unitwise.Term.prefix=
|
def prefix=(value)
value.is_a?(Prefix) ? super(value) : super(Prefix.find(value.to_s))
end
|
ruby
|
def prefix=(value)
value.is_a?(Prefix) ? super(value) : super(Prefix.find(value.to_s))
end
|
[
"def",
"prefix",
"=",
"(",
"value",
")",
"value",
".",
"is_a?",
"(",
"Prefix",
")",
"?",
"super",
"(",
"value",
")",
":",
"super",
"(",
"Prefix",
".",
"find",
"(",
"value",
".",
"to_s",
")",
")",
"end"
] |
Set the prefix.
@param value [String, Prefix] Either a string representing a Prefix, or
a Prefix
|
[
"Set",
"the",
"prefix",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/term.rb#L19-L21
|
21,569 |
joshwlewis/unitwise
|
lib/unitwise/term.rb
|
Unitwise.Term.root_terms
|
def root_terms
if terminal?
[self]
else
atom.scale.root_terms.map do |t|
self.class.new(:atom => t.atom, :exponent => t.exponent * exponent)
end
end
end
|
ruby
|
def root_terms
if terminal?
[self]
else
atom.scale.root_terms.map do |t|
self.class.new(:atom => t.atom, :exponent => t.exponent * exponent)
end
end
end
|
[
"def",
"root_terms",
"if",
"terminal?",
"[",
"self",
"]",
"else",
"atom",
".",
"scale",
".",
"root_terms",
".",
"map",
"do",
"|",
"t",
"|",
"self",
".",
"class",
".",
"new",
"(",
":atom",
"=>",
"t",
".",
"atom",
",",
":exponent",
"=>",
"t",
".",
"exponent",
"*",
"exponent",
")",
"end",
"end",
"end"
] |
The base units this term is derived from
@return [Array] An array of Unitwise::Term
@api public
|
[
"The",
"base",
"units",
"this",
"term",
"is",
"derived",
"from"
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/term.rb#L77-L85
|
21,570 |
joshwlewis/unitwise
|
lib/unitwise/term.rb
|
Unitwise.Term.operate
|
def operate(operator, other)
exp = operator == '/' ? -1 : 1
if other.respond_to?(:terms)
Unit.new(other.terms.map { |t| t ** exp } << self)
elsif other.respond_to?(:atom)
Unit.new([self, other ** exp])
elsif other.is_a?(Numeric)
self.class.new(to_hash.merge(:factor => factor.send(operator, other)))
end
end
|
ruby
|
def operate(operator, other)
exp = operator == '/' ? -1 : 1
if other.respond_to?(:terms)
Unit.new(other.terms.map { |t| t ** exp } << self)
elsif other.respond_to?(:atom)
Unit.new([self, other ** exp])
elsif other.is_a?(Numeric)
self.class.new(to_hash.merge(:factor => factor.send(operator, other)))
end
end
|
[
"def",
"operate",
"(",
"operator",
",",
"other",
")",
"exp",
"=",
"operator",
"==",
"'/'",
"?",
"-",
"1",
":",
"1",
"if",
"other",
".",
"respond_to?",
"(",
":terms",
")",
"Unit",
".",
"new",
"(",
"other",
".",
"terms",
".",
"map",
"{",
"|",
"t",
"|",
"t",
"**",
"exp",
"}",
"<<",
"self",
")",
"elsif",
"other",
".",
"respond_to?",
"(",
":atom",
")",
"Unit",
".",
"new",
"(",
"[",
"self",
",",
"other",
"**",
"exp",
"]",
")",
"elsif",
"other",
".",
"is_a?",
"(",
"Numeric",
")",
"self",
".",
"class",
".",
"new",
"(",
"to_hash",
".",
"merge",
"(",
":factor",
"=>",
"factor",
".",
"send",
"(",
"operator",
",",
"other",
")",
")",
")",
"end",
"end"
] |
Multiply or divide a term
@api private
|
[
"Multiply",
"or",
"divide",
"a",
"term"
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/term.rb#L134-L143
|
21,571 |
joshwlewis/unitwise
|
lib/unitwise/base.rb
|
Unitwise.Base.to_s
|
def to_s(mode = :primary_code)
res = send(mode) || primary_code
res.respond_to?(:each) ? res.first.to_s : res.to_s
end
|
ruby
|
def to_s(mode = :primary_code)
res = send(mode) || primary_code
res.respond_to?(:each) ? res.first.to_s : res.to_s
end
|
[
"def",
"to_s",
"(",
"mode",
"=",
":primary_code",
")",
"res",
"=",
"send",
"(",
"mode",
")",
"||",
"primary_code",
"res",
".",
"respond_to?",
"(",
":each",
")",
"?",
"res",
".",
"first",
".",
"to_s",
":",
"res",
".",
"to_s",
"end"
] |
String representation for the instance.
@param mode [symbol] The attribute to for stringification
@return [String]
@api public
|
[
"String",
"representation",
"for",
"the",
"instance",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/base.rb#L53-L56
|
21,572 |
joshwlewis/unitwise
|
lib/unitwise/atom.rb
|
Unitwise.Atom.scale=
|
def scale=(attrs)
@scale = if attrs[:function_code]
Functional.new(attrs[:value], attrs[:unit_code], attrs[:function_code])
else
Scale.new(attrs[:value], attrs[:unit_code])
end
end
|
ruby
|
def scale=(attrs)
@scale = if attrs[:function_code]
Functional.new(attrs[:value], attrs[:unit_code], attrs[:function_code])
else
Scale.new(attrs[:value], attrs[:unit_code])
end
end
|
[
"def",
"scale",
"=",
"(",
"attrs",
")",
"@scale",
"=",
"if",
"attrs",
"[",
":function_code",
"]",
"Functional",
".",
"new",
"(",
"attrs",
"[",
":value",
"]",
",",
"attrs",
"[",
":unit_code",
"]",
",",
"attrs",
"[",
":function_code",
"]",
")",
"else",
"Scale",
".",
"new",
"(",
"attrs",
"[",
":value",
"]",
",",
"attrs",
"[",
":unit_code",
"]",
")",
"end",
"end"
] |
Set the atom's scale. It can be set as a Scale or a Functional
@return [Unitwise::Functional, Unitwise::Scale]
@api public
|
[
"Set",
"the",
"atom",
"s",
"scale",
".",
"It",
"can",
"be",
"set",
"as",
"a",
"Scale",
"or",
"a",
"Functional"
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/atom.rb#L93-L99
|
21,573 |
joshwlewis/unitwise
|
lib/unitwise/atom.rb
|
Unitwise.Atom.validate!
|
def validate!
missing_properties = %i{primary_code names scale}.select do |prop|
val = liner_get(prop)
val.nil? || (val.respond_to?(:empty) && val.empty?)
end
if !missing_properties.empty?
missing_list = missing_properties.join(',')
raise Unitwise::DefinitionError,
"Atom has missing properties: #{missing_list}."
end
msg = "Atom definition could not be resolved. Ensure that it is a base " \
"unit or is defined relative to existing units."
begin
!scalar.nil? && !magnitude.nil? || raise(Unitwise::DefinitionError, msg)
rescue Unitwise::ExpressionError
raise Unitwise::DefinitionError, msg
end
end
|
ruby
|
def validate!
missing_properties = %i{primary_code names scale}.select do |prop|
val = liner_get(prop)
val.nil? || (val.respond_to?(:empty) && val.empty?)
end
if !missing_properties.empty?
missing_list = missing_properties.join(',')
raise Unitwise::DefinitionError,
"Atom has missing properties: #{missing_list}."
end
msg = "Atom definition could not be resolved. Ensure that it is a base " \
"unit or is defined relative to existing units."
begin
!scalar.nil? && !magnitude.nil? || raise(Unitwise::DefinitionError, msg)
rescue Unitwise::ExpressionError
raise Unitwise::DefinitionError, msg
end
end
|
[
"def",
"validate!",
"missing_properties",
"=",
"%i{",
"primary_code",
"names",
"scale",
"}",
".",
"select",
"do",
"|",
"prop",
"|",
"val",
"=",
"liner_get",
"(",
"prop",
")",
"val",
".",
"nil?",
"||",
"(",
"val",
".",
"respond_to?",
"(",
":empty",
")",
"&&",
"val",
".",
"empty?",
")",
"end",
"if",
"!",
"missing_properties",
".",
"empty?",
"missing_list",
"=",
"missing_properties",
".",
"join",
"(",
"','",
")",
"raise",
"Unitwise",
"::",
"DefinitionError",
",",
"\"Atom has missing properties: #{missing_list}.\"",
"end",
"msg",
"=",
"\"Atom definition could not be resolved. Ensure that it is a base \"",
"\"unit or is defined relative to existing units.\"",
"begin",
"!",
"scalar",
".",
"nil?",
"&&",
"!",
"magnitude",
".",
"nil?",
"||",
"raise",
"(",
"Unitwise",
"::",
"DefinitionError",
",",
"msg",
")",
"rescue",
"Unitwise",
"::",
"ExpressionError",
"raise",
"Unitwise",
"::",
"DefinitionError",
",",
"msg",
"end",
"end"
] |
A basic validator for atoms. It checks for the bare minimum properties
and that it's scalar and magnitude can be resolved. Note that this method
requires the units it depends on to already exist, so it is not used
when loading the initial data from UCUM.
@return [true] returns true if the atom is valid
@raise [Unitwise::DefinitionError]
|
[
"A",
"basic",
"validator",
"for",
"atoms",
".",
"It",
"checks",
"for",
"the",
"bare",
"minimum",
"properties",
"and",
"that",
"it",
"s",
"scalar",
"and",
"magnitude",
"can",
"be",
"resolved",
".",
"Note",
"that",
"this",
"method",
"requires",
"the",
"units",
"it",
"depends",
"on",
"to",
"already",
"exist",
"so",
"it",
"is",
"not",
"used",
"when",
"loading",
"the",
"initial",
"data",
"from",
"UCUM",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/atom.rb#L128-L148
|
21,574 |
joshwlewis/unitwise
|
lib/unitwise/compatible.rb
|
Unitwise.Compatible.composition
|
def composition
root_terms.reduce(SignedMultiset.new) do |s, t|
s.increment(t.atom.dim, t.exponent) if t.atom
s
end
end
|
ruby
|
def composition
root_terms.reduce(SignedMultiset.new) do |s, t|
s.increment(t.atom.dim, t.exponent) if t.atom
s
end
end
|
[
"def",
"composition",
"root_terms",
".",
"reduce",
"(",
"SignedMultiset",
".",
"new",
")",
"do",
"|",
"s",
",",
"t",
"|",
"s",
".",
"increment",
"(",
"t",
".",
"atom",
".",
"dim",
",",
"t",
".",
"exponent",
")",
"if",
"t",
".",
"atom",
"s",
"end",
"end"
] |
A representation of a unit based on the atoms it's derived from.
@return [SignedMultiset]
@api public
|
[
"A",
"representation",
"of",
"a",
"unit",
"based",
"on",
"the",
"atoms",
"it",
"s",
"derived",
"from",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/compatible.rb#L21-L26
|
21,575 |
joshwlewis/unitwise
|
lib/unitwise/compatible.rb
|
Unitwise.Compatible.composition_string
|
def composition_string
composition.sort.map do |k, v|
v == 1 ? k.to_s : "#{k}#{v}"
end.join('.')
end
|
ruby
|
def composition_string
composition.sort.map do |k, v|
v == 1 ? k.to_s : "#{k}#{v}"
end.join('.')
end
|
[
"def",
"composition_string",
"composition",
".",
"sort",
".",
"map",
"do",
"|",
"k",
",",
"v",
"|",
"v",
"==",
"1",
"?",
"k",
".",
"to_s",
":",
"\"#{k}#{v}\"",
"end",
".",
"join",
"(",
"'.'",
")",
"end"
] |
A string representation of a unit based on the atoms it's derived from
@return [String]
@api public
|
[
"A",
"string",
"representation",
"of",
"a",
"unit",
"based",
"on",
"the",
"atoms",
"it",
"s",
"derived",
"from"
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/compatible.rb#L38-L42
|
21,576 |
joshwlewis/unitwise
|
lib/unitwise/unit.rb
|
Unitwise.Unit.terms
|
def terms
unless frozen?
unless @terms
decomposer = Expression.decompose(@expression)
@mode = decomposer.mode
@terms = decomposer.terms
end
freeze
end
@terms
end
|
ruby
|
def terms
unless frozen?
unless @terms
decomposer = Expression.decompose(@expression)
@mode = decomposer.mode
@terms = decomposer.terms
end
freeze
end
@terms
end
|
[
"def",
"terms",
"unless",
"frozen?",
"unless",
"@terms",
"decomposer",
"=",
"Expression",
".",
"decompose",
"(",
"@expression",
")",
"@mode",
"=",
"decomposer",
".",
"mode",
"@terms",
"=",
"decomposer",
".",
"terms",
"end",
"freeze",
"end",
"@terms",
"end"
] |
Create a new unit. You can send an expression or a collection of terms
@param input [String, Unit, [Term]] A string expression, a unit, or a
collection of tems.
@api public
The collection of terms used by this unit.
@return [Array]
@api public
|
[
"Create",
"a",
"new",
"unit",
".",
"You",
"can",
"send",
"an",
"expression",
"or",
"a",
"collection",
"of",
"terms"
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/unit.rb#L27-L37
|
21,577 |
joshwlewis/unitwise
|
lib/unitwise/unit.rb
|
Unitwise.Unit.expression
|
def expression(mode=nil)
if @expression && (mode.nil? || mode == self.mode)
@expression
else
Expression.compose(terms, mode || self.mode)
end
end
|
ruby
|
def expression(mode=nil)
if @expression && (mode.nil? || mode == self.mode)
@expression
else
Expression.compose(terms, mode || self.mode)
end
end
|
[
"def",
"expression",
"(",
"mode",
"=",
"nil",
")",
"if",
"@expression",
"&&",
"(",
"mode",
".",
"nil?",
"||",
"mode",
"==",
"self",
".",
"mode",
")",
"@expression",
"else",
"Expression",
".",
"compose",
"(",
"terms",
",",
"mode",
"||",
"self",
".",
"mode",
")",
"end",
"end"
] |
Build a string representation of this unit by it's terms.
@param mode [Symbol] The mode to use to stringify the atoms
(:primary_code, :names, :secondary_code).
@return [String]
@api public
|
[
"Build",
"a",
"string",
"representation",
"of",
"this",
"unit",
"by",
"it",
"s",
"terms",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/unit.rb#L44-L50
|
21,578 |
joshwlewis/unitwise
|
lib/unitwise/unit.rb
|
Unitwise.Unit.scalar
|
def scalar(magnitude = 1)
terms.reduce(1) do |prod, term|
prod * term.scalar(magnitude)
end
end
|
ruby
|
def scalar(magnitude = 1)
terms.reduce(1) do |prod, term|
prod * term.scalar(magnitude)
end
end
|
[
"def",
"scalar",
"(",
"magnitude",
"=",
"1",
")",
"terms",
".",
"reduce",
"(",
"1",
")",
"do",
"|",
"prod",
",",
"term",
"|",
"prod",
"*",
"term",
".",
"scalar",
"(",
"magnitude",
")",
"end",
"end"
] |
Get a scalar value for this unit.
@param magnitude [Numeric] An optional magnitude on this unit's scale.
@return [Numeric] A scalar value on a linear scale
@api public
|
[
"Get",
"a",
"scalar",
"value",
"for",
"this",
"unit",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/unit.rb#L89-L93
|
21,579 |
joshwlewis/unitwise
|
lib/unitwise/unit.rb
|
Unitwise.Unit.**
|
def **(other)
if other.is_a?(Numeric)
self.class.new(terms.map { |t| t ** other })
else
fail TypeError, "Can't raise #{self} to #{other}."
end
end
|
ruby
|
def **(other)
if other.is_a?(Numeric)
self.class.new(terms.map { |t| t ** other })
else
fail TypeError, "Can't raise #{self} to #{other}."
end
end
|
[
"def",
"**",
"(",
"other",
")",
"if",
"other",
".",
"is_a?",
"(",
"Numeric",
")",
"self",
".",
"class",
".",
"new",
"(",
"terms",
".",
"map",
"{",
"|",
"t",
"|",
"t",
"**",
"other",
"}",
")",
"else",
"fail",
"TypeError",
",",
"\"Can't raise #{self} to #{other}.\"",
"end",
"end"
] |
Raise this unit to a numeric power.
@param other [Numeric]
@return [Unitwise::Unit]
@api public
|
[
"Raise",
"this",
"unit",
"to",
"a",
"numeric",
"power",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/unit.rb#L129-L135
|
21,580 |
joshwlewis/unitwise
|
lib/unitwise/unit.rb
|
Unitwise.Unit.operate
|
def operate(operator, other)
exp = operator == '/' ? -1 : 1
if other.respond_to?(:terms)
self.class.new(terms + other.terms.map { |t| t ** exp })
elsif other.respond_to?(:atom)
self.class.new(terms << other ** exp)
elsif other.is_a?(Numeric)
self.class.new(terms.map { |t| t.send(operator, other) })
end
end
|
ruby
|
def operate(operator, other)
exp = operator == '/' ? -1 : 1
if other.respond_to?(:terms)
self.class.new(terms + other.terms.map { |t| t ** exp })
elsif other.respond_to?(:atom)
self.class.new(terms << other ** exp)
elsif other.is_a?(Numeric)
self.class.new(terms.map { |t| t.send(operator, other) })
end
end
|
[
"def",
"operate",
"(",
"operator",
",",
"other",
")",
"exp",
"=",
"operator",
"==",
"'/'",
"?",
"-",
"1",
":",
"1",
"if",
"other",
".",
"respond_to?",
"(",
":terms",
")",
"self",
".",
"class",
".",
"new",
"(",
"terms",
"+",
"other",
".",
"terms",
".",
"map",
"{",
"|",
"t",
"|",
"t",
"**",
"exp",
"}",
")",
"elsif",
"other",
".",
"respond_to?",
"(",
":atom",
")",
"self",
".",
"class",
".",
"new",
"(",
"terms",
"<<",
"other",
"**",
"exp",
")",
"elsif",
"other",
".",
"is_a?",
"(",
"Numeric",
")",
"self",
".",
"class",
".",
"new",
"(",
"terms",
".",
"map",
"{",
"|",
"t",
"|",
"t",
".",
"send",
"(",
"operator",
",",
"other",
")",
"}",
")",
"end",
"end"
] |
Multiply or divide units
@api private
|
[
"Multiply",
"or",
"divide",
"units"
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/unit.rb#L169-L178
|
21,581 |
joshwlewis/unitwise
|
lib/unitwise/measurement.rb
|
Unitwise.Measurement.convert_to
|
def convert_to(other_unit)
other_unit = Unit.new(other_unit)
if compatible_with?(other_unit)
new(converted_value(other_unit), other_unit)
else
fail ConversionError, "Can't convert #{self} to #{other_unit}."
end
end
|
ruby
|
def convert_to(other_unit)
other_unit = Unit.new(other_unit)
if compatible_with?(other_unit)
new(converted_value(other_unit), other_unit)
else
fail ConversionError, "Can't convert #{self} to #{other_unit}."
end
end
|
[
"def",
"convert_to",
"(",
"other_unit",
")",
"other_unit",
"=",
"Unit",
".",
"new",
"(",
"other_unit",
")",
"if",
"compatible_with?",
"(",
"other_unit",
")",
"new",
"(",
"converted_value",
"(",
"other_unit",
")",
",",
"other_unit",
")",
"else",
"fail",
"ConversionError",
",",
"\"Can't convert #{self} to #{other_unit}.\"",
"end",
"end"
] |
Create a new Measurement
@param value [Numeric] The scalar value for the measurement
@param unit [String, Measurement::Unit] Either a string expression, or a
Measurement::Unit
@example
Unitwise::Measurement.new(20, 'm/s')
@api public
Convert this measurement to a compatible unit.
@param other_unit [String, Measurement::Unit] Either a string expression
or a Measurement::Unit
@example
measurement1.convert_to('foot')
measurement2.convert_to('kilogram')
@api public
|
[
"Create",
"a",
"new",
"Measurement"
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/measurement.rb#L26-L33
|
21,582 |
joshwlewis/unitwise
|
lib/unitwise/measurement.rb
|
Unitwise.Measurement.**
|
def **(other)
if other.is_a?(Numeric)
new(value ** other, unit ** other)
else
fail TypeError, "Can't raise #{self} to #{other} power."
end
end
|
ruby
|
def **(other)
if other.is_a?(Numeric)
new(value ** other, unit ** other)
else
fail TypeError, "Can't raise #{self} to #{other} power."
end
end
|
[
"def",
"**",
"(",
"other",
")",
"if",
"other",
".",
"is_a?",
"(",
"Numeric",
")",
"new",
"(",
"value",
"**",
"other",
",",
"unit",
"**",
"other",
")",
"else",
"fail",
"TypeError",
",",
"\"Can't raise #{self} to #{other} power.\"",
"end",
"end"
] |
Raise a measurement to a numeric power.
@param number [Numeric]
@example
measurement ** 2
@api public
|
[
"Raise",
"a",
"measurement",
"to",
"a",
"numeric",
"power",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/measurement.rb#L80-L86
|
21,583 |
joshwlewis/unitwise
|
lib/unitwise/measurement.rb
|
Unitwise.Measurement.round
|
def round(digits = nil)
rounded_value = digits ? value.round(digits) : value.round
self.class.new(rounded_value, unit)
end
|
ruby
|
def round(digits = nil)
rounded_value = digits ? value.round(digits) : value.round
self.class.new(rounded_value, unit)
end
|
[
"def",
"round",
"(",
"digits",
"=",
"nil",
")",
"rounded_value",
"=",
"digits",
"?",
"value",
".",
"round",
"(",
"digits",
")",
":",
"value",
".",
"round",
"self",
".",
"class",
".",
"new",
"(",
"rounded_value",
",",
"unit",
")",
"end"
] |
Round the measurement value. Delegates to the value's class.
@return [Integer, Float]
@api public
|
[
"Round",
"the",
"measurement",
"value",
".",
"Delegates",
"to",
"the",
"value",
"s",
"class",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/measurement.rb#L91-L94
|
21,584 |
joshwlewis/unitwise
|
lib/unitwise/measurement.rb
|
Unitwise.Measurement.method_missing
|
def method_missing(meth, *args, &block)
if args.empty? && !block_given? && (match = /\Ato_(\w+)\Z/.match(meth.to_s))
begin
convert_to(match[1])
rescue ExpressionError
super(meth, *args, &block)
end
else
super(meth, *args, &block)
end
end
|
ruby
|
def method_missing(meth, *args, &block)
if args.empty? && !block_given? && (match = /\Ato_(\w+)\Z/.match(meth.to_s))
begin
convert_to(match[1])
rescue ExpressionError
super(meth, *args, &block)
end
else
super(meth, *args, &block)
end
end
|
[
"def",
"method_missing",
"(",
"meth",
",",
"*",
"args",
",",
"&",
"block",
")",
"if",
"args",
".",
"empty?",
"&&",
"!",
"block_given?",
"&&",
"(",
"match",
"=",
"/",
"\\A",
"\\w",
"\\Z",
"/",
".",
"match",
"(",
"meth",
".",
"to_s",
")",
")",
"begin",
"convert_to",
"(",
"match",
"[",
"1",
"]",
")",
"rescue",
"ExpressionError",
"super",
"(",
"meth",
",",
"args",
",",
"block",
")",
"end",
"else",
"super",
"(",
"meth",
",",
"args",
",",
"block",
")",
"end",
"end"
] |
Will attempt to convert to a unit by method name.
@example
measurement.to_foot # => <Unitwise::Measurement 4 foot>
@api semipublic
|
[
"Will",
"attempt",
"to",
"convert",
"to",
"a",
"unit",
"by",
"method",
"name",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/measurement.rb#L139-L149
|
21,585 |
joshwlewis/unitwise
|
lib/unitwise/measurement.rb
|
Unitwise.Measurement.converted_value
|
def converted_value(other_unit)
if other_unit.special?
other_unit.magnitude scalar
else
scalar / other_unit.scalar
end
end
|
ruby
|
def converted_value(other_unit)
if other_unit.special?
other_unit.magnitude scalar
else
scalar / other_unit.scalar
end
end
|
[
"def",
"converted_value",
"(",
"other_unit",
")",
"if",
"other_unit",
".",
"special?",
"other_unit",
".",
"magnitude",
"scalar",
"else",
"scalar",
"/",
"other_unit",
".",
"scalar",
"end",
"end"
] |
Determine value of the unit after conversion to another unit
@api private
|
[
"Determine",
"value",
"of",
"the",
"unit",
"after",
"conversion",
"to",
"another",
"unit"
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/measurement.rb#L161-L167
|
21,586 |
joshwlewis/unitwise
|
lib/unitwise/measurement.rb
|
Unitwise.Measurement.combine
|
def combine(operator, other)
if other.respond_to?(:composition) && compatible_with?(other)
new(value.send(operator, other.convert_to(unit).value), unit)
end
end
|
ruby
|
def combine(operator, other)
if other.respond_to?(:composition) && compatible_with?(other)
new(value.send(operator, other.convert_to(unit).value), unit)
end
end
|
[
"def",
"combine",
"(",
"operator",
",",
"other",
")",
"if",
"other",
".",
"respond_to?",
"(",
":composition",
")",
"&&",
"compatible_with?",
"(",
"other",
")",
"new",
"(",
"value",
".",
"send",
"(",
"operator",
",",
"other",
".",
"convert_to",
"(",
"unit",
")",
".",
"value",
")",
",",
"unit",
")",
"end",
"end"
] |
Add or subtract other unit
@api private
|
[
"Add",
"or",
"subtract",
"other",
"unit"
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/measurement.rb#L171-L175
|
21,587 |
joshwlewis/unitwise
|
lib/unitwise/measurement.rb
|
Unitwise.Measurement.operate
|
def operate(operator, other)
if other.is_a?(Numeric)
new(value.send(operator, other), unit)
elsif other.respond_to?(:composition)
if compatible_with?(other)
converted = other.convert_to(unit)
new(value.send(operator, converted.value),
unit.send(operator, converted.unit))
else
new(value.send(operator, other.value),
unit.send(operator, other.unit))
end
end
end
|
ruby
|
def operate(operator, other)
if other.is_a?(Numeric)
new(value.send(operator, other), unit)
elsif other.respond_to?(:composition)
if compatible_with?(other)
converted = other.convert_to(unit)
new(value.send(operator, converted.value),
unit.send(operator, converted.unit))
else
new(value.send(operator, other.value),
unit.send(operator, other.unit))
end
end
end
|
[
"def",
"operate",
"(",
"operator",
",",
"other",
")",
"if",
"other",
".",
"is_a?",
"(",
"Numeric",
")",
"new",
"(",
"value",
".",
"send",
"(",
"operator",
",",
"other",
")",
",",
"unit",
")",
"elsif",
"other",
".",
"respond_to?",
"(",
":composition",
")",
"if",
"compatible_with?",
"(",
"other",
")",
"converted",
"=",
"other",
".",
"convert_to",
"(",
"unit",
")",
"new",
"(",
"value",
".",
"send",
"(",
"operator",
",",
"converted",
".",
"value",
")",
",",
"unit",
".",
"send",
"(",
"operator",
",",
"converted",
".",
"unit",
")",
")",
"else",
"new",
"(",
"value",
".",
"send",
"(",
"operator",
",",
"other",
".",
"value",
")",
",",
"unit",
".",
"send",
"(",
"operator",
",",
"other",
".",
"unit",
")",
")",
"end",
"end",
"end"
] |
Multiply or divide other unit
@api private
|
[
"Multiply",
"or",
"divide",
"other",
"unit"
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/measurement.rb#L179-L192
|
21,588 |
joshwlewis/unitwise
|
lib/unitwise/scale.rb
|
Unitwise.Scale.scalar
|
def scalar(magnitude = value)
if special?
unit.scalar(magnitude)
else
Number.rationalize(value) * Number.rationalize(unit.scalar)
end
end
|
ruby
|
def scalar(magnitude = value)
if special?
unit.scalar(magnitude)
else
Number.rationalize(value) * Number.rationalize(unit.scalar)
end
end
|
[
"def",
"scalar",
"(",
"magnitude",
"=",
"value",
")",
"if",
"special?",
"unit",
".",
"scalar",
"(",
"magnitude",
")",
"else",
"Number",
".",
"rationalize",
"(",
"value",
")",
"*",
"Number",
".",
"rationalize",
"(",
"unit",
".",
"scalar",
")",
"end",
"end"
] |
Get a scalar value for this scale.
@param magnitude [Numeric] An optional magnitude on this scale.
@return [Numeric] A scalar value on a linear scale
@api public
|
[
"Get",
"a",
"scalar",
"value",
"for",
"this",
"scale",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/scale.rb#L51-L57
|
21,589 |
joshwlewis/unitwise
|
lib/unitwise/scale.rb
|
Unitwise.Scale.magnitude
|
def magnitude(scalar = scalar())
if special?
unit.magnitude(scalar)
else
value * unit.magnitude
end
end
|
ruby
|
def magnitude(scalar = scalar())
if special?
unit.magnitude(scalar)
else
value * unit.magnitude
end
end
|
[
"def",
"magnitude",
"(",
"scalar",
"=",
"scalar",
"(",
")",
")",
"if",
"special?",
"unit",
".",
"magnitude",
"(",
"scalar",
")",
"else",
"value",
"*",
"unit",
".",
"magnitude",
"end",
"end"
] |
Get a magnitude based on a linear scale value. Only used by scales with
special atoms in it's hierarchy.
@param scalar [Numeric] A linear scalar value
@return [Numeric] The equivalent magnitude on this scale
@api public
|
[
"Get",
"a",
"magnitude",
"based",
"on",
"a",
"linear",
"scale",
"value",
".",
"Only",
"used",
"by",
"scales",
"with",
"special",
"atoms",
"in",
"it",
"s",
"hierarchy",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/scale.rb#L64-L70
|
21,590 |
joshwlewis/unitwise
|
lib/unitwise/scale.rb
|
Unitwise.Scale.to_s
|
def to_s(mode = nil)
unit_string = unit.to_s(mode)
if unit_string && unit_string != '1'
"#{simplified_value} #{unit_string}"
else
simplified_value.to_s
end
end
|
ruby
|
def to_s(mode = nil)
unit_string = unit.to_s(mode)
if unit_string && unit_string != '1'
"#{simplified_value} #{unit_string}"
else
simplified_value.to_s
end
end
|
[
"def",
"to_s",
"(",
"mode",
"=",
"nil",
")",
"unit_string",
"=",
"unit",
".",
"to_s",
"(",
"mode",
")",
"if",
"unit_string",
"&&",
"unit_string",
"!=",
"'1'",
"\"#{simplified_value} #{unit_string}\"",
"else",
"simplified_value",
".",
"to_s",
"end",
"end"
] |
Convert to a simple string representing the scale.
@api public
|
[
"Convert",
"to",
"a",
"simple",
"string",
"representing",
"the",
"scale",
"."
] |
f786d2e660721a0238949e7abce20852879de8ef
|
https://github.com/joshwlewis/unitwise/blob/f786d2e660721a0238949e7abce20852879de8ef/lib/unitwise/scale.rb#L104-L111
|
21,591 |
seanchas116/ruby-qml
|
lib/qml/engine.rb
|
QML.Engine.evaluate
|
def evaluate(str, file = '<in QML::Engine#evaluate>', lineno = 1)
evaluate_impl(str, file, lineno).tap do |result|
raise result.to_error if result.is_a?(JSObject) && result.error?
end
end
|
ruby
|
def evaluate(str, file = '<in QML::Engine#evaluate>', lineno = 1)
evaluate_impl(str, file, lineno).tap do |result|
raise result.to_error if result.is_a?(JSObject) && result.error?
end
end
|
[
"def",
"evaluate",
"(",
"str",
",",
"file",
"=",
"'<in QML::Engine#evaluate>'",
",",
"lineno",
"=",
"1",
")",
"evaluate_impl",
"(",
"str",
",",
"file",
",",
"lineno",
")",
".",
"tap",
"do",
"|",
"result",
"|",
"raise",
"result",
".",
"to_error",
"if",
"result",
".",
"is_a?",
"(",
"JSObject",
")",
"&&",
"result",
".",
"error?",
"end",
"end"
] |
Evaluates an JavaScript expression
@param [String] str The JavaScript string
@param [String] file The file name
@param [Integer] lineno The line number
|
[
"Evaluates",
"an",
"JavaScript",
"expression"
] |
8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a
|
https://github.com/seanchas116/ruby-qml/blob/8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a/lib/qml/engine.rb#L13-L17
|
21,592 |
seanchas116/ruby-qml
|
lib/qml/component.rb
|
QML.Component.load_path
|
def load_path(path)
path = path.to_s
check_error_string do
@path = Pathname.new(path)
load_path_impl(path)
end
self
end
|
ruby
|
def load_path(path)
path = path.to_s
check_error_string do
@path = Pathname.new(path)
load_path_impl(path)
end
self
end
|
[
"def",
"load_path",
"(",
"path",
")",
"path",
"=",
"path",
".",
"to_s",
"check_error_string",
"do",
"@path",
"=",
"Pathname",
".",
"new",
"(",
"path",
")",
"load_path_impl",
"(",
"path",
")",
"end",
"self",
"end"
] |
Creates an component. Either data or path must be specified.
@param [String] data the QML file data.
@param [#to_s] path the QML file path.
@return QML::Component
|
[
"Creates",
"an",
"component",
".",
"Either",
"data",
"or",
"path",
"must",
"be",
"specified",
"."
] |
8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a
|
https://github.com/seanchas116/ruby-qml/blob/8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a/lib/qml/component.rb#L32-L39
|
21,593 |
seanchas116/ruby-qml
|
lib/qml/signal.rb
|
QML.Signal.emit
|
def emit(*args)
if args.size != @arity
fail ::ArgumentError ,"wrong number of arguments for signal (#{args.size} for #{@arity})"
end
@listeners.each do |listener|
listener.call(*args)
end
end
|
ruby
|
def emit(*args)
if args.size != @arity
fail ::ArgumentError ,"wrong number of arguments for signal (#{args.size} for #{@arity})"
end
@listeners.each do |listener|
listener.call(*args)
end
end
|
[
"def",
"emit",
"(",
"*",
"args",
")",
"if",
"args",
".",
"size",
"!=",
"@arity",
"fail",
"::",
"ArgumentError",
",",
"\"wrong number of arguments for signal (#{args.size} for #{@arity})\"",
"end",
"@listeners",
".",
"each",
"do",
"|",
"listener",
"|",
"listener",
".",
"call",
"(",
"args",
")",
"end",
"end"
] |
Initializes the Signal.
@param [Array<#to_sym>, nil] params the parameter names (the signal will be variadic if nil).
Calls every connected procedure with given arguments.
Raises an ArgumentError when the arity is wrong.
@param args the arguments.
|
[
"Initializes",
"the",
"Signal",
"."
] |
8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a
|
https://github.com/seanchas116/ruby-qml/blob/8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a/lib/qml/signal.rb#L30-L37
|
21,594 |
seanchas116/ruby-qml
|
lib/qml/data/list_model.rb
|
QML.ListModel.moving
|
def moving(range, destination)
return if range.count == 0
@access.begin_move(range.min, range.max, destination)
ret = yield
@access.end_move
ret
end
|
ruby
|
def moving(range, destination)
return if range.count == 0
@access.begin_move(range.min, range.max, destination)
ret = yield
@access.end_move
ret
end
|
[
"def",
"moving",
"(",
"range",
",",
"destination",
")",
"return",
"if",
"range",
".",
"count",
"==",
"0",
"@access",
".",
"begin_move",
"(",
"range",
".",
"min",
",",
"range",
".",
"max",
",",
"destination",
")",
"ret",
"=",
"yield",
"@access",
".",
"end_move",
"ret",
"end"
] |
Notifies the list views that items are about to be and were moved.
@param [Range<Integer>] range the index range of the item being moved.
@param [Integer] destination the first index of the items after moved.
@yield the block that actually do moving operation of the items.
@return the result of given block.
@see http://qt-project.org/doc/qt-5/qabstractitemmodel.html#beginMoveRows QAbstractItemModel::beginMoveRows
@see http://qt-project.org/doc/qt-5/qabstractitemmodel.html#endMoveRows QAbstractItemModel::endMoveRows
@see #inserting
@see #removing
|
[
"Notifies",
"the",
"list",
"views",
"that",
"items",
"are",
"about",
"to",
"be",
"and",
"were",
"moved",
"."
] |
8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a
|
https://github.com/seanchas116/ruby-qml/blob/8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a/lib/qml/data/list_model.rb#L68-L75
|
21,595 |
seanchas116/ruby-qml
|
lib/qml/data/list_model.rb
|
QML.ListModel.inserting
|
def inserting(range, &block)
return if range.count == 0
@access.begin_insert(range.min, range.max)
ret = yield
@access.end_insert
ret
end
|
ruby
|
def inserting(range, &block)
return if range.count == 0
@access.begin_insert(range.min, range.max)
ret = yield
@access.end_insert
ret
end
|
[
"def",
"inserting",
"(",
"range",
",",
"&",
"block",
")",
"return",
"if",
"range",
".",
"count",
"==",
"0",
"@access",
".",
"begin_insert",
"(",
"range",
".",
"min",
",",
"range",
".",
"max",
")",
"ret",
"=",
"yield",
"@access",
".",
"end_insert",
"ret",
"end"
] |
Notifies the list views that items are about to be and were inserted.
@param [Range<Integer>] range the index range of the items after inserted.
@yield the block that actually do insertion of the items.
@return the result of give block.
@example
inserting(index ... index + items.size) do
@array.insert(index, *items)
end
@see http://qt-project.org/doc/qt-5/qabstractitemmodel.html#beginInsertRows QAbstractItemModel::beginInsertRows
@see http://qt-project.org/doc/qt-5/qabstractitemmodel.html#endInsertRows QAbstractItemModel::endInsertRows
@see #removing
@see #moving
|
[
"Notifies",
"the",
"list",
"views",
"that",
"items",
"are",
"about",
"to",
"be",
"and",
"were",
"inserted",
"."
] |
8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a
|
https://github.com/seanchas116/ruby-qml/blob/8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a/lib/qml/data/list_model.rb#L89-L96
|
21,596 |
seanchas116/ruby-qml
|
lib/qml/data/list_model.rb
|
QML.ListModel.removing
|
def removing(range, &block)
return if range.count == 0
@access.begin_remove(range.min, range.max)
ret = yield
@access.end_remove
ret
end
|
ruby
|
def removing(range, &block)
return if range.count == 0
@access.begin_remove(range.min, range.max)
ret = yield
@access.end_remove
ret
end
|
[
"def",
"removing",
"(",
"range",
",",
"&",
"block",
")",
"return",
"if",
"range",
".",
"count",
"==",
"0",
"@access",
".",
"begin_remove",
"(",
"range",
".",
"min",
",",
"range",
".",
"max",
")",
"ret",
"=",
"yield",
"@access",
".",
"end_remove",
"ret",
"end"
] |
Notifies the list views that items are about to be and were removed.
@param [Range<Integer>] range the index range of the items before removed.
@yield the block that actually do removal of the items.
@return the result of give block.
@see http://qt-project.org/doc/qt-5/qabstractitemmodel.html#beginRemoveRows QAbstractItemModel::beginRemoveRows
@see http://qt-project.org/doc/qt-5/qabstractitemmodel.html#endRemoveRows QAbstractItemModel::endRemoveRows
@see #inserting
@see #moving
|
[
"Notifies",
"the",
"list",
"views",
"that",
"items",
"are",
"about",
"to",
"be",
"and",
"were",
"removed",
"."
] |
8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a
|
https://github.com/seanchas116/ruby-qml/blob/8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a/lib/qml/data/list_model.rb#L106-L113
|
21,597 |
seanchas116/ruby-qml
|
lib/qml/data/array_model.rb
|
QML.ArrayModel.insert
|
def insert(index, *items)
inserting(index ... index + items.size) do
@array.insert(index, *items)
end
self
end
|
ruby
|
def insert(index, *items)
inserting(index ... index + items.size) do
@array.insert(index, *items)
end
self
end
|
[
"def",
"insert",
"(",
"index",
",",
"*",
"items",
")",
"inserting",
"(",
"index",
"...",
"index",
"+",
"items",
".",
"size",
")",
"do",
"@array",
".",
"insert",
"(",
"index",
",",
"items",
")",
"end",
"self",
"end"
] |
Inserts items.
@param [Integer] index
@return [self]
|
[
"Inserts",
"items",
"."
] |
8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a
|
https://github.com/seanchas116/ruby-qml/blob/8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a/lib/qml/data/array_model.rb#L42-L47
|
21,598 |
seanchas116/ruby-qml
|
lib/qml/js_object.rb
|
QML.JSObject.method_missing
|
def method_missing(method, *args, &block)
if method[-1] == '='
# setter
key = method.slice(0...-1).to_sym
unless has_key?(key)
super
end
self[key] = args[0]
else
unless has_key?(method)
super
end
prop = self[method]
if prop.is_a? JSFunction
prop.call_with_instance(self, *args, &block)
else
prop
end
end
end
|
ruby
|
def method_missing(method, *args, &block)
if method[-1] == '='
# setter
key = method.slice(0...-1).to_sym
unless has_key?(key)
super
end
self[key] = args[0]
else
unless has_key?(method)
super
end
prop = self[method]
if prop.is_a? JSFunction
prop.call_with_instance(self, *args, &block)
else
prop
end
end
end
|
[
"def",
"method_missing",
"(",
"method",
",",
"*",
"args",
",",
"&",
"block",
")",
"if",
"method",
"[",
"-",
"1",
"]",
"==",
"'='",
"# setter",
"key",
"=",
"method",
".",
"slice",
"(",
"0",
"...",
"-",
"1",
")",
".",
"to_sym",
"unless",
"has_key?",
"(",
"key",
")",
"super",
"end",
"self",
"[",
"key",
"]",
"=",
"args",
"[",
"0",
"]",
"else",
"unless",
"has_key?",
"(",
"method",
")",
"super",
"end",
"prop",
"=",
"self",
"[",
"method",
"]",
"if",
"prop",
".",
"is_a?",
"JSFunction",
"prop",
".",
"call_with_instance",
"(",
"self",
",",
"args",
",",
"block",
")",
"else",
"prop",
"end",
"end",
"end"
] |
Gets or sets a JS property, or call it as a method if it is a function.
|
[
"Gets",
"or",
"sets",
"a",
"JS",
"property",
"or",
"call",
"it",
"as",
"a",
"method",
"if",
"it",
"is",
"a",
"function",
"."
] |
8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a
|
https://github.com/seanchas116/ruby-qml/blob/8e809d1b1a03b856c4a4da6e221e5f8fcb0c3b9a/lib/qml/js_object.rb#L41-L62
|
21,599 |
zed-0xff/zpng
|
lib/zpng/adam7_decoder.rb
|
ZPNG.Adam7Decoder.convert_coords
|
def convert_coords x,y
# optimizing this into one switch/case statement gives
# about 1-2% speed increase (ruby 1.9.3p286)
if y%2 == 1
# 7th pass: last height/2 full scanlines
[x, y/2 + @pass_starts[7]]
elsif x%2 == 1 && y%2 == 0
# 6th pass
[x/2, y/2 + @pass_starts[6]]
elsif x%8 == 0 && y%8 == 0
# 1st pass, starts at 0
[x/8, y/8]
elsif x%8 == 4 && y%8 == 0
# 2nd pass
[x/8, y/8 + @pass_starts[2]]
elsif x%4 == 0 && y%8 == 4
# 3rd pass
[x/4, y/8 + @pass_starts[3]]
elsif x%4 == 2 && y%4 == 0
# 4th pass
[x/4, y/4 + @pass_starts[4]]
elsif x%2 == 0 && y%4 == 2
# 5th pass
[x/2, y/4 + @pass_starts[5]]
else
raise "invalid coords"
end
end
|
ruby
|
def convert_coords x,y
# optimizing this into one switch/case statement gives
# about 1-2% speed increase (ruby 1.9.3p286)
if y%2 == 1
# 7th pass: last height/2 full scanlines
[x, y/2 + @pass_starts[7]]
elsif x%2 == 1 && y%2 == 0
# 6th pass
[x/2, y/2 + @pass_starts[6]]
elsif x%8 == 0 && y%8 == 0
# 1st pass, starts at 0
[x/8, y/8]
elsif x%8 == 4 && y%8 == 0
# 2nd pass
[x/8, y/8 + @pass_starts[2]]
elsif x%4 == 0 && y%8 == 4
# 3rd pass
[x/4, y/8 + @pass_starts[3]]
elsif x%4 == 2 && y%4 == 0
# 4th pass
[x/4, y/4 + @pass_starts[4]]
elsif x%2 == 0 && y%4 == 2
# 5th pass
[x/2, y/4 + @pass_starts[5]]
else
raise "invalid coords"
end
end
|
[
"def",
"convert_coords",
"x",
",",
"y",
"# optimizing this into one switch/case statement gives",
"# about 1-2% speed increase (ruby 1.9.3p286)",
"if",
"y",
"%",
"2",
"==",
"1",
"# 7th pass: last height/2 full scanlines",
"[",
"x",
",",
"y",
"/",
"2",
"+",
"@pass_starts",
"[",
"7",
"]",
"]",
"elsif",
"x",
"%",
"2",
"==",
"1",
"&&",
"y",
"%",
"2",
"==",
"0",
"# 6th pass",
"[",
"x",
"/",
"2",
",",
"y",
"/",
"2",
"+",
"@pass_starts",
"[",
"6",
"]",
"]",
"elsif",
"x",
"%",
"8",
"==",
"0",
"&&",
"y",
"%",
"8",
"==",
"0",
"# 1st pass, starts at 0",
"[",
"x",
"/",
"8",
",",
"y",
"/",
"8",
"]",
"elsif",
"x",
"%",
"8",
"==",
"4",
"&&",
"y",
"%",
"8",
"==",
"0",
"# 2nd pass",
"[",
"x",
"/",
"8",
",",
"y",
"/",
"8",
"+",
"@pass_starts",
"[",
"2",
"]",
"]",
"elsif",
"x",
"%",
"4",
"==",
"0",
"&&",
"y",
"%",
"8",
"==",
"4",
"# 3rd pass",
"[",
"x",
"/",
"4",
",",
"y",
"/",
"8",
"+",
"@pass_starts",
"[",
"3",
"]",
"]",
"elsif",
"x",
"%",
"4",
"==",
"2",
"&&",
"y",
"%",
"4",
"==",
"0",
"# 4th pass",
"[",
"x",
"/",
"4",
",",
"y",
"/",
"4",
"+",
"@pass_starts",
"[",
"4",
"]",
"]",
"elsif",
"x",
"%",
"2",
"==",
"0",
"&&",
"y",
"%",
"4",
"==",
"2",
"# 5th pass",
"[",
"x",
"/",
"2",
",",
"y",
"/",
"4",
"+",
"@pass_starts",
"[",
"5",
"]",
"]",
"else",
"raise",
"\"invalid coords\"",
"end",
"end"
] |
convert "flat" coords in scanline number & pos in scanline
|
[
"convert",
"flat",
"coords",
"in",
"scanline",
"number",
"&",
"pos",
"in",
"scanline"
] |
d356182ab9bbc2ed3fe5c064488498cf1678b0f0
|
https://github.com/zed-0xff/zpng/blob/d356182ab9bbc2ed3fe5c064488498cf1678b0f0/lib/zpng/adam7_decoder.rb#L41-L69
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.