idx
int64
0
24.9k
question
stringlengths
68
4.14k
target
stringlengths
9
749
1,300
def inline_node_content ( node ) inline_content = node . script if contains_interpolation? ( inline_content ) strip_surrounding_quotes ( inline_content ) else inline_content end end
Get the inline content for this node .
1,301
def next_node ( node ) return unless node siblings = node . parent ? node . parent . children : [ node ] next_sibling = siblings [ siblings . index ( node ) + 1 ] if siblings . count > 1 return next_sibling if next_sibling next_node ( node . parent ) end
Gets the next node following this node ascending up the ancestor chain recursively if this node has no siblings .
1,302
def parse ( source ) buffer = :: Parser :: Source :: Buffer . new ( '(string)' ) buffer . source = source @parser . reset @parser . parse ( buffer ) end
Creates a reusable parser . Parse the given Ruby source into an abstract syntax tree .
1,303
def act_on_options ( options ) configure_logger ( options ) if options [ :help ] print_help ( options ) Sysexits :: EX_OK elsif options [ :version ] || options [ :verbose_version ] print_version ( options ) Sysexits :: EX_OK elsif options [ :show_linters ] print_available_linters Sysexits :: EX_OK elsif options [ :show_reporters ] print_available_reporters Sysexits :: EX_OK else scan_for_lints ( options ) end end
Given the provided options execute the appropriate command .
1,304
def configure_logger ( options ) log . color_enabled = options . fetch ( :color , log . tty? ) log . summary_enabled = options . fetch ( :summary , true ) end
Given the provided options configure the logger .
1,305
def handle_exception ( exception ) case exception when HamlLint :: Exceptions :: ConfigurationError log . error exception . message Sysexits :: EX_CONFIG when HamlLint :: Exceptions :: InvalidCLIOption log . error exception . message log . log "Run `#{APP_NAME}` --help for usage documentation" Sysexits :: EX_USAGE when HamlLint :: Exceptions :: InvalidFilePath log . error exception . message Sysexits :: EX_NOINPUT when HamlLint :: Exceptions :: NoLintersError log . error exception . message Sysexits :: EX_NOINPUT else print_unexpected_exception ( exception ) Sysexits :: EX_SOFTWARE end end
Outputs a message and returns an appropriate error code for the specified exception .
1,306
def reporter_from_options ( options ) if options [ :auto_gen_config ] HamlLint :: Reporter :: DisabledConfigReporter . new ( log , limit : options [ :auto_gen_exclude_limit ] || 15 ) else options . fetch ( :reporter , HamlLint :: Reporter :: DefaultReporter ) . new ( log ) end end
Instantiates a new reporter based on the options .
1,307
def scan_for_lints ( options ) reporter = reporter_from_options ( options ) report = Runner . new . run ( options . merge ( reporter : reporter ) ) report . display report . failed? ? Sysexits :: EX_DATAERR : Sysexits :: EX_OK end
Scans the files specified by the given options for lints .
1,308
def print_available_linters log . info 'Available linters:' linter_names = HamlLint :: LinterRegistry . linters . map do | linter | linter . name . split ( '::' ) . last end linter_names . sort . each do | linter_name | log . log " - #{linter_name}" end end
Outputs a list of all currently available linters .
1,309
def print_available_reporters log . info 'Available reporters:' HamlLint :: Reporter . available . map ( & :cli_name ) . sort . each do | reporter_name | log . log " - #{reporter_name}" end end
Outputs a list of currently available reporters .
1,310
def print_version ( options ) log . log "#{HamlLint::APP_NAME} #{HamlLint::VERSION}" if options [ :verbose_version ] log . log "haml #{Gem.loaded_specs['haml'].version}" log . log "rubocop #{Gem.loaded_specs['rubocop'].version}" log . log RUBY_DESCRIPTION end end
Outputs the application name and version .
1,311
def print_unexpected_exception ( exception ) log . bold_error exception . message log . error exception . backtrace . join ( "\n" ) log . warning 'Report this bug at ' , false log . info HamlLint :: BUG_REPORT_URL log . newline log . success 'To help fix this issue, please include:' log . log '- The above stack trace' log . log '- Haml-Lint version: ' , false log . info HamlLint :: VERSION log . log '- Haml version: ' , false log . info Gem . loaded_specs [ 'haml' ] . version log . log '- RuboCop version: ' , false log . info Gem . loaded_specs [ 'rubocop' ] . version log . log '- Ruby version: ' , false log . info RUBY_VERSION end
Outputs the backtrace of an exception with instructions on how to report the issue .
1,312
def strip_frontmatter ( source ) frontmatter = / \A \s \n \n \. \. \. \s \n /mx if config [ 'skip_frontmatter' ] && match = source . match ( frontmatter ) newlines = match [ 0 ] . count ( "\n" ) source . sub! ( frontmatter , "\n" * newlines ) end source end
Removes YAML frontmatter
1,313
def disabled? ( visitor ) visitor . is_a? ( HamlLint :: Linter ) && comment_configuration . disabled? ( visitor . name ) end
Checks whether a visitor is disabled due to comment configuration .
1,314
def each return to_enum ( __callee__ ) unless block_given? node = self loop do yield node break unless ( node = node . next_node ) end end
Implements the Enumerable interface to walk through an entire tree .
1,315
def line_numbers return ( line .. line ) unless @value && text end_line = line + lines . count end_line = nontrivial_end_line if line == end_line ( line .. end_line ) end
The line numbers that are contained within the node .
1,316
def nontrivial_end_line if ( last_child = children . last ) last_child . line_numbers . end - 1 elsif successor successor . line_numbers . begin - 1 else @document . source_lines . count end end
Discovers the end line of the node when there are no lines .
1,317
def node_for_line ( line ) find ( -> { HamlLint :: Tree :: NullNode . new } ) { | node | node . line_numbers . cover? ( line ) } end
Gets the node of the syntax tree for a given line number .
1,318
def extract_enabled_linters ( config , options ) included_linters = LinterRegistry . extract_linters_from ( options . fetch ( :included_linters , [ ] ) ) included_linters = LinterRegistry . linters if included_linters . empty? excluded_linters = LinterRegistry . extract_linters_from ( options . fetch ( :excluded_linters , [ ] ) ) linters = ( included_linters - excluded_linters ) . map do | linter_class | linter_config = config . for_linter ( linter_class ) linter_class . new ( linter_config ) if linter_config [ 'enabled' ] end . compact if linters . empty? raise HamlLint :: Exceptions :: NoLintersError , 'No linters specified' end linters end
Returns a list of linters that are enabled given the specified configuration and additional options .
1,319
def run_linter_on_file? ( config , linter , file ) linter_config = config . for_linter ( linter ) if linter_config [ 'include' ] . any? && ! HamlLint :: Utils . any_glob_matches? ( linter_config [ 'include' ] , file ) return false end if HamlLint :: Utils . any_glob_matches? ( linter_config [ 'exclude' ] , file ) return false end true end
Whether to run the given linter against the specified file .
1,320
def attributes_source @attributes_source ||= begin _explicit_tag , static_attrs , rest = source_code . scan ( / \A \s \w \w \. \# /m ) [ 0 ] attr_types = { '{' => [ :hash , %w[ { } ] ] , '(' => [ :html , %w[ ( ) ] ] , '[' => [ :object_ref , %w[ [ ] ] ] , } attr_source = { static : static_attrs } while rest type , chars = attr_types [ rest [ 0 ] ] break unless type break if attr_source [ type ] attr_source [ type ] , rest = Haml :: Util . balance ( rest , * chars ) end attr_source end end
Returns the source code for the static and dynamic attributes of a tag .
1,321
def find_lints ( ruby , source_map ) rubocop = :: RuboCop :: CLI . new filename = if document . file "#{document.file}.rb" else 'ruby_script.rb' end with_ruby_from_stdin ( ruby ) do extract_lints_from_offenses ( lint_file ( rubocop , filename ) , source_map ) end end
Executes RuboCop against the given Ruby code and records the offenses as lints .
1,322
def with_ruby_from_stdin ( ruby , & _block ) original_stdin = $stdin stdin = StringIO . new stdin . write ( ruby ) stdin . rewind $stdin = stdin yield ensure $stdin = original_stdin end
Overrides the global stdin to allow RuboCop to read Ruby code from it .
1,323
def get_abs_and_rel_path ( path ) original_path = Pathname . new ( path ) root_dir_path = Pathname . new ( File . expand_path ( Dir . pwd ) ) if original_path . absolute? [ path , original_path . relative_path_from ( root_dir_path ) ] else [ root_dir_path + original_path , path ] end end
Returns an array of two items the first being the absolute path the second the relative path .
1,324
def extract_interpolated_values ( text ) dumped_text = text . dump newline_positions = extract_substring_positions ( dumped_text , '\\\n' ) Haml :: Util . handle_interpolation ( dumped_text ) do | scan | line = ( newline_positions . find_index { | marker | scan . pos <= marker } || newline_positions . size ) + 1 escape_count = ( scan [ 2 ] . size - 1 ) / 2 break unless escape_count . even? dumped_interpolated_str = Haml :: Util . balance ( scan , '{' , '}' , 1 ) [ 0 ] [ 0 ... - 1 ] yield [ eval ( '"' + dumped_interpolated_str + '"' ) , line ] end end
Yields interpolated values within a block of text .
1,325
def for_consecutive_items ( items , satisfies , min_consecutive = 2 ) current_index = - 1 while ( current_index += 1 ) < items . count next unless satisfies [ items [ current_index ] ] count = count_consecutive ( items , current_index , & satisfies ) next unless count >= min_consecutive yield items [ current_index ... ( current_index + count ) ] current_index += count end end
Find all consecutive items satisfying the given block of a minimum size yielding each group of consecutive items to the provided block .
1,326
def with_environment ( env ) old_env = { } env . each do | var , value | old_env [ var ] = ENV [ var . to_s ] ENV [ var . to_s ] = value end yield ensure old_env . each { | var , value | ENV [ var . to_s ] = value } end
Calls a block of code with a modified set of environment variables restoring them once the code has executed .
1,327
def run_cli ( task_args ) cli_args = parse_args logger = quiet ? HamlLint :: Logger . silent : HamlLint :: Logger . new ( STDOUT ) result = HamlLint :: CLI . new ( logger ) . run ( Array ( cli_args ) + files_to_lint ( task_args ) ) fail "#{HamlLint::APP_NAME} failed with exit code #{result}" unless result == 0 end
Executes the CLI given the specified task arguments .
1,328
def files_to_lint ( task_args ) explicit_files = Array ( task_args [ :files ] ) + Array ( task_args . extras ) explicit_files . any? ? explicit_files : files end
Returns the list of files that should be linted given the specified task arguments .
1,329
def log_notices ( notices = [ ] , level = :warn ) notices . each do | concern | message = concern . delete ( :message ) @logger . send ( level , message , redact_sensitive ( concern ) ) end end
Log setting or extension loading notices sensitive information is redacted .
1,330
def setup_spawn @logger . info ( "configuring sensu spawn" , :settings => @settings [ :sensu ] [ :spawn ] ) threadpool_size = @settings [ :sensu ] [ :spawn ] [ :limit ] + 10 @logger . debug ( "setting eventmachine threadpool size" , :size => threadpool_size ) EM :: threadpool_size = threadpool_size Spawn . setup ( @settings [ :sensu ] [ :spawn ] ) end
Set up Sensu spawn creating a worker to create control and limit spawned child processes . This method adjusts the EventMachine thread pool size to accommodate the concurrent process spawn limit and other Sensu process operations .
1,331
def retry_until_true ( wait = 0.5 , & block ) EM :: Timer . new ( wait ) do unless block . call retry_until_true ( wait , & block ) end end end
Retry a code block until it retures true . The first attempt and following retries are delayed .
1,332
def deep_merge ( hash_one , hash_two ) merged = hash_one . dup hash_two . each do | key , value | merged [ key ] = case when hash_one [ key ] . is_a? ( Hash ) && value . is_a? ( Hash ) deep_merge ( hash_one [ key ] , value ) when hash_one [ key ] . is_a? ( Array ) && value . is_a? ( Array ) ( hash_one [ key ] + value ) . uniq else value end end merged end
Deep merge two hashes . Nested hashes are deep merged arrays are concatenated and duplicate array items are removed .
1,333
def deep_dup ( obj ) if obj . class == Hash new_obj = obj . dup new_obj . each do | key , value | new_obj [ deep_dup ( key ) ] = deep_dup ( value ) end new_obj elsif obj . class == Array arr = [ ] obj . each do | item | arr << deep_dup ( item ) end arr elsif obj . class == String obj . dup else obj end end
Creates a deep dup of basic ruby objects with support for walking hashes and arrays .
1,334
def system_address :: Socket . ip_address_list . find { | address | address . ipv4? && ! address . ipv4_loopback? } . ip_address rescue nil end
Retrieve the system IP address . If a valid non - loopback IPv4 address cannot be found and an error is thrown nil will be returned .
1,335
def find_attribute_value ( tree , path , default ) attribute = tree [ path . shift ] if attribute . is_a? ( Hash ) find_attribute_value ( attribute , path , default ) else attribute . nil? ? default : attribute end end
Traverse a hash for an attribute value with a fallback default value if nil .
1,336
def determine_check_cron_time ( check ) cron_parser = CronParser . new ( check [ :cron ] ) current_time = Time . now next_cron_time = cron_parser . next ( current_time ) next_cron_time - current_time end
Determine the next check cron time .
1,337
def with_restart_on_deadlock yield rescue ActiveRecord :: StatementInvalid => exception if exception . message =~ / /i || exception . message =~ / /i ActiveSupport :: Notifications . publish ( 'deadlock_restart.double_entry' , :exception => exception ) raise ActiveRecord :: RestartTransaction else raise end end
Execute the given block and retry the current restartable transaction if a MySQL deadlock occurs .
1,338
def calculate ( account , args = { } ) options = Options . new ( account , args ) relations = RelationBuilder . new ( options ) lines = relations . build if options . between? || options . code? Money . new ( lines . sum ( :amount ) , account . currency ) else result = lines . from ( lines_table_name ( options ) ) . order ( 'id DESC' ) . limit ( 1 ) . pluck ( :balance ) result . empty? ? Money . zero ( account . currency ) : Money . new ( result . first , account . currency ) end end
Get the current or historic balance of an account .
1,339
def populate_page_with ( data ) data . to_h . each do | key , value | populate_section ( key , value ) if value . respond_to? ( :to_h ) populate_value ( self , key , value ) end end
This method will populate all matched page TextFields TextAreas SelectLists FileFields Checkboxes and Radio Buttons from the Hash passed as an argument . The way it find an element is by matching the Hash key to the name you provided when declaring the element on your page .
1,340
def page_url ( url ) define_method ( "goto" ) do platform . navigate_to self . page_url_value end define_method ( 'page_url_value' ) do lookup = url . kind_of? ( Symbol ) ? self . send ( url ) : url erb = ERB . new ( %Q{#{lookup}} ) merged_params = self . class . instance_variable_get ( "@merged_params" ) params = merged_params ? merged_params : self . class . params erb . result ( binding ) end end
Specify the url for the page . A call to this method will generate a goto method to take you to the page .
1,341
def in_frame ( identifier , frame = nil , & block ) frame = frame . nil? ? [ ] : frame . dup frame << { frame : identifier } block . call ( frame ) end
Identify an element as existing within a frame . A frame parameter is passed to the block and must be passed to the other calls to PageObject . You can nest calls to in_frame by passing the frame to the next level .
1,342
def in_iframe ( identifier , frame = nil , & block ) frame = frame . nil? ? [ ] : frame . dup frame << { iframe : identifier } block . call ( frame ) end
Identify an element as existing within an iframe . A frame parameter is passed to the block and must be passed to the other calls to PageObject . You can nest calls to in_frame by passing the frame to the next level .
1,343
def text_field ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'text_field_for' , & block ) define_method ( name ) do return platform . text_field_value_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . value end define_method ( "#{name}=" ) do | value | return platform . text_field_value_set ( identifier . clone , value ) unless block_given? self . send ( "#{name}_element" ) . value = value end end
adds four methods to the page object - one to set text in a text field another to retrieve text from a text field another to return the text field element another to check the text field s existence .
1,344
def hidden_field ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'hidden_field_for' , & block ) define_method ( name ) do return platform . hidden_field_value_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . value end end
adds three methods to the page object - one to get the text from a hidden field another to retrieve the hidden field element and another to check the hidden field s existence .
1,345
def text_area ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'text_area_for' , & block ) define_method ( name ) do return platform . text_area_value_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . value end define_method ( "#{name}=" ) do | value | return platform . text_area_value_set ( identifier . clone , value ) unless block_given? self . send ( "#{name}_element" ) . value = value end end
adds four methods to the page object - one to set text in a text area another to retrieve text from a text area another to return the text area element and another to check the text area s existence .
1,346
def select_list ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'select_list_for' , & block ) define_method ( name ) do return platform . select_list_value_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . value end define_method ( "#{name}=" ) do | value | return platform . select_list_value_set ( identifier . clone , value ) unless block_given? self . send ( "#{name}_element" ) . select ( value ) end define_method ( "#{name}_options" ) do element = self . send ( "#{name}_element" ) ( element && element . options ) ? element . options . collect ( & :text ) : [ ] end end
adds five methods - one to select an item in a drop - down another to fetch the currently selected item text another to retrieve the select list element another to check the drop down s existence and another to get all the available options to select from .
1,347
def button ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'button_for' , & block ) define_method ( name ) do return platform . click_button_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . click end end
adds three methods - one to click a button another to return the button element and another to check the button s existence .
1,348
def div ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'div_for' , & block ) define_method ( name ) do return platform . div_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text from a div another to return the div element and another to check the div s existence .
1,349
def span ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'span_for' , & block ) define_method ( name ) do return platform . span_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text from a span another to return the span element and another to check the span s existence .
1,350
def table ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'table_for' , & block ) define_method ( name ) do return platform . table_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to return the text for the table one to retrieve the table element and another to check the table s existence .
1,351
def cell ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'cell_for' , & block ) define_method ( "#{name}" ) do return platform . cell_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text from a table cell another to return the table cell element and another to check the cell s existence .
1,352
def row ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'row_for' , & block ) define_method ( "#{name}" ) do return platform . row_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text from a table row another to return the table row element and another to check the row s existence .
1,353
def image ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'image_for' , & block ) define_method ( "#{name}_loaded?" ) do return platform . image_loaded_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . loaded? end end
adds three methods - one to retrieve the image element another to check the load status of the image and another to check the image s existence .
1,354
def list_item ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'list_item_for' , & block ) define_method ( name ) do return platform . list_item_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text from a list item another to return the list item element and another to check the list item s existence .
1,355
def unordered_list ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'unordered_list_for' , & block ) define_method ( name ) do return platform . unordered_list_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to return the text within the unordered list one to retrieve the unordered list element and another to check it s existence .
1,356
def ordered_list ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'ordered_list_for' , & block ) define_method ( name ) do return platform . ordered_list_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to return the text within the ordered list one to retrieve the ordered list element and another to test it s existence .
1,357
def h1 ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'h1_for' , & block ) define_method ( name ) do return platform . h1_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text of a h1 element another to retrieve a h1 element and another to check for it s existence .
1,358
def h2 ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'h2_for' , & block ) define_method ( name ) do return platform . h2_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text of a h2 element another to retrieve a h2 element and another to check for it s existence .
1,359
def h3 ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'h3_for' , & block ) define_method ( name ) do return platform . h3_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text of a h3 element another to return a h3 element and another to check for it s existence .
1,360
def h4 ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'h4_for' , & block ) define_method ( name ) do return platform . h4_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text of a h4 element another to return a h4 element and another to check for it s existence .
1,361
def h5 ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'h5_for' , & block ) define_method ( name ) do return platform . h5_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text of a h5 element another to return a h5 element and another to check for it s existence .
1,362
def h6 ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'h6_for' , & block ) define_method ( name ) do return platform . h6_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text of a h6 element another to return a h6 element and another to check for it s existence .
1,363
def paragraph ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'paragraph_for' , & block ) define_method ( name ) do return platform . paragraph_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text of a paragraph another to retrieve a paragraph element and another to check the paragraph s existence .
1,364
def file_field ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'file_field_for' , & block ) define_method ( "#{name}=" ) do | value | return platform . file_field_value_set ( identifier . clone , value ) unless block_given? self . send ( "#{name}_element" ) . value = value end end
adds three methods - one to set the file for a file field another to retrieve the file field element and another to check it s existence .
1,365
def label ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'label_for' , & block ) define_method ( name ) do return platform . label_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text from a label another to return the label element and another to check the label s existence .
1,366
def area ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'area_for' , & block ) define_method ( name ) do return platform . click_area_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . click end end
adds three methods - one to click the area another to return the area element and another to check the area s existence .
1,367
def b ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'b_for' , & block ) define_method ( name ) do return platform . b_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text of a b element another to retrieve a b element and another to check for it s existence .
1,368
def i ( name , identifier = { :index => 0 } , & block ) standard_methods ( name , identifier , 'i_for' , & block ) define_method ( name ) do return platform . i_text_for identifier . clone unless block_given? self . send ( "#{name}_element" ) . text end end
adds three methods - one to retrieve the text of a i element another to retrieve a i element and another to check for it s existence .
1,369
def element ( name , tag = :element , identifier = { :index => 0 } , & block ) if tag . is_a? ( Hash ) identifier = tag tag = :element end standard_methods ( name , identifier , 'element_for' , & block ) define_method ( "#{name}" ) do element = self . send ( "#{name}_element" ) %w( Button TextField Radio Hidden CheckBox FileField ) . each do | klass | next unless element . element . class . to_s == "Watir::#{klass}" self . class . send ( klass . gsub ( / / , '\1_\2' ) . downcase , name , identifier , & block ) return self . send name end element . text end define_method ( "#{name}_element" ) do return call_block ( & block ) if block_given? platform . element_for ( tag , identifier . clone ) end define_method ( "#{name}?" ) do self . send ( "#{name}_element" ) . exists? end define_method ( "#{name}=" ) do | value | element = self . send ( "#{name}_element" ) klass = case element . element when Watir :: TextField 'text_field' when Watir :: TextArea 'text_area' when Watir :: Select 'select_list' when Watir :: FileField 'file_field' else raise "Can not set a #{element.element} element with #=" end self . class . send ( klass , name , identifier , & block ) self . send ( "#{name}=" , value ) end end
adds three methods - one to retrieve the text of an element another to retrieve an element and another to check the element s existence .
1,370
def elements ( name , tag = :element , identifier = { :index => 0 } , & block ) if tag . is_a? ( Hash ) identifier = tag tag = :element end define_method ( "#{name}_elements" ) do return call_block ( & block ) if block_given? platform . elements_for ( tag , identifier . clone ) end end
adds a method to return a collection of generic Element objects for a specific tag .
1,371
def page_section ( name , section_class , identifier ) define_method ( name ) do platform . page_for ( identifier , section_class ) end end
adds a method to return a page object rooted at an element
1,372
def page_sections ( name , section_class , identifier ) define_method ( name ) do platform . pages_for ( identifier , section_class ) end end
adds a method to return a collection of page objects rooted at elements
1,373
def on_page ( page_class , params = { :using_params => { } } , visit = false , & block ) page_class = class_from_string ( page_class ) if page_class . is_a? String return super ( page_class , params , visit , & block ) unless page_class . ancestors . include? PageObject merged = page_class . params . merge ( params [ :using_params ] ) page_class . instance_variable_set ( "@merged_params" , merged ) unless merged . empty? @current_page = page_class . new ( @browser , visit ) block . call @current_page if block @current_page end
Create a page object .
1,374
def if_page ( page_class , params = { :using_params => { } } , & block ) page_class = class_from_string ( page_class ) if page_class . is_a? String return @current_page unless @current_page . class == page_class on_page ( page_class , params , false , & block ) end
Create a page object if and only if the current page is the same page to be created
1,375
def method_missing ( m , * args , & block ) if m . to_s . end_with? ( '=' ) raise ArgumentError . new ( "wrong number of arguments (#{args.size} for 1 with #{m}) to method #{m}" ) if args . nil? or 1 != args . length m = m [ 0 .. - 2 ] return store ( m . to_s , args [ 0 ] ) if has_key? ( m . to_s ) return store ( m . to_sym , args [ 0 ] ) if has_key? ( m . to_sym ) return store ( m , args [ 0 ] ) else raise ArgumentError . new ( "wrong number of arguments (#{args.size} for 0 with #{m}) to method #{m}" ) unless args . nil? or args . empty? return fetch ( m , nil ) if has_key? ( m ) return fetch ( m . to_s , nil ) if has_key? ( m . to_s ) return fetch ( m . to_sym , nil ) if has_key? ( m . to_sym ) end raise NoMethodError . new ( "undefined method #{m}" , m ) end
Handles requests for Hash values . Others cause an Exception to be raised .
1,376
def process ( css , opts = { } ) opts = convert_options ( opts ) apply_wrapper = "(function(opts, pluginOpts) {" "return eval(process.apply(this, opts, pluginOpts));" "})" plugin_opts = params_with_browsers ( opts [ :from ] ) . merge ( opts ) process_opts = { from : plugin_opts . delete ( :from ) , to : plugin_opts . delete ( :to ) , map : plugin_opts . delete ( :map ) , } begin result = runtime . call ( apply_wrapper , [ css , process_opts , plugin_opts ] ) rescue ExecJS :: ProgramError => e contry_error = "BrowserslistError: " "Country statistics is not supported " "in client-side build of Browserslist" if e . message == contry_error raise "Country statistics is not supported in AutoprefixerRails. " "Use Autoprefixer with webpack or other Node.js builder." else raise e end end Result . new ( result [ "css" ] , result [ "map" ] , result [ "warnings" ] ) end
Process css and return result .
1,377
def parse_config ( config ) sections = { "defaults" => [ ] } current = "defaults" config . gsub ( / \n / , "" ) . split ( / \n / ) . map ( & :strip ) . reject ( & :empty? ) . each do | line | if IS_SECTION =~ line current = line . match ( IS_SECTION ) [ 1 ] . strip sections [ current ] ||= [ ] else sections [ current ] << line end end sections end
Parse Browserslist config
1,378
def convert_options ( opts ) converted = { } opts . each_pair do | name , value | if / / =~ name name = name . to_s . gsub ( / \w / ) { | i | i . delete ( "_" ) . upcase } . to_sym end value = convert_options ( value ) if value . is_a? Hash converted [ name ] = value end converted end
Convert ruby_options to jsOptions
1,379
def find_config ( file ) path = Pathname ( file ) . expand_path while path . parent != path config1 = path . join ( "browserslist" ) return config1 . read if config1 . exist? && ! config1 . directory? config2 = path . join ( ".browserslistrc" ) return config2 . read if config2 . exist? && ! config1 . directory? path = path . parent end nil end
Try to find Browserslist config
1,380
def runtime @runtime ||= begin if ExecJS . eval ( "typeof Uint8Array" ) != "function" if ExecJS . runtime . name . start_with? ( "therubyracer" ) raise "ExecJS::RubyRacerRuntime is not supported. " "Please replace therubyracer with mini_racer " "in your Gemfile or use Node.js as ExecJS runtime." else raise "#{ExecJS.runtime.name} runtime does’t support ES6. " \ "Please update or replace your current ExecJS runtime." end end if ExecJS . runtime == ExecJS :: Runtimes :: Node version = ExecJS . runtime . eval ( "process.version" ) first = version . match ( / \d / ) [ 1 ] . to_i if first < 6 raise "Autoprefixer doesn’t support Node #{version}. Update it." end end ExecJS . compile ( build_js ) end end
Lazy load for JS library
1,381
def read_js @@js ||= begin root = Pathname ( File . dirname ( __FILE__ ) ) path = root . join ( "../../vendor/autoprefixer.js" ) path . read end end
Cache autoprefixer . js content
1,382
def save ( filename ) data_to_save = @metadata . merge ( { "licenses" => licenses , "notices" => notices } ) FileUtils . mkdir_p ( File . dirname ( filename ) ) File . write ( filename , data_to_save . to_yaml ) end
Construct a new record
1,383
def license_contents matched_files . reject { | f | f == package_file } . group_by ( & :content ) . map { | content , files | { "sources" => license_content_sources ( files ) , "text" => content } } end
Returns the license text content from all matched sources except the package file which doesn t contain license text .
1,384
def notice_contents Dir . glob ( dir_path . join ( "*" ) ) . grep ( LEGAL_FILES_PATTERN ) . select { | path | File . file? ( path ) } . sort . map { | path | { "sources" => normalize_source_path ( path ) , "text" => File . read ( path ) . rstrip } } . select { | notice | notice [ "text" ] . length > 0 } end
Returns legal notices found at the dependency path
1,385
def license_content_sources ( files ) paths = Array ( files ) . map do | file | next file [ :uri ] if file [ :uri ] path = dir_path . join ( file [ :dir ] , file [ :name ] ) normalize_source_path ( path ) end paths . join ( ", " ) end
Returns the sources for a group of license file contents
1,386
def sources @sources ||= Licensed :: Sources :: Source . sources . select { | source_class | enabled? ( source_class . type ) } . map { | source_class | source_class . new ( self ) } end
Returns an array of enabled app sources
1,387
def enabled? ( source_type ) default = ! self [ "sources" ] . any? { | _ , enabled | enabled } self [ "sources" ] . fetch ( source_type , default ) end
Returns whether a source type is enabled
1,388
def authenticate_by_cookie ( authenticatable_class ) key = cookie_name ( authenticatable_class ) authenticatable_id = cookies . encrypted [ key ] return unless authenticatable_id authenticatable_class . find_by ( id : authenticatable_id ) end
Authenticate a record using cookies . Looks for a cookie corresponding to the _authenticatable_class_ . If found try to find it in the database .
1,389
def sign_in ( authenticatable ) key = cookie_name ( authenticatable . class ) cookies . encrypted . permanent [ key ] = { value : authenticatable . id } authenticatable end
Signs in user by assigning their id to a permanent cookie .
1,390
def sign_out ( authenticatable_class ) key = cookie_name ( authenticatable_class ) cookies . encrypted . permanent [ key ] = { value : nil } cookies . delete ( key ) true end
Signs out user by deleting their encrypted cookie .
1,391
def count ( stat , count , opts = EMPTY_OPTIONS ) opts = { :sample_rate => opts } if opts . is_a? Numeric send_stats stat , count , COUNTER_TYPE , opts end
Sends an arbitrary count for the given stat to the statsd server .
1,392
def gauge ( stat , value , opts = EMPTY_OPTIONS ) opts = { :sample_rate => opts } if opts . is_a? Numeric send_stats stat , value , GAUGE_TYPE , opts end
Sends an arbitary gauge value for the given stat to the statsd server .
1,393
def histogram ( stat , value , opts = EMPTY_OPTIONS ) send_stats stat , value , HISTOGRAM_TYPE , opts end
Sends a value to be tracked as a histogram to the statsd server .
1,394
def set ( stat , value , opts = EMPTY_OPTIONS ) opts = { :sample_rate => opts } if opts . is_a? Numeric send_stats stat , value , SET_TYPE , opts end
Sends a value to be tracked as a set to the statsd server .
1,395
def service_check ( name , status , opts = EMPTY_OPTIONS ) send_stat format_service_check ( name , status , opts ) end
This method allows you to send custom service check statuses .
1,396
def event ( title , text , opts = EMPTY_OPTIONS ) send_stat format_event ( title , text , opts ) end
This end point allows you to post events to the stream . You can tag them set priority and even aggregate them with other events .
1,397
def generate_redirect_from ( doc ) doc . redirect_from . each do | path | page = RedirectPage . redirect_from ( doc , path ) doc . site . pages << page redirects [ page . redirect_from ] = page . redirect_to end end
For every redirect_from entry generate a redirect page
1,398
def set_paths ( from , to ) @context ||= context from = ensure_leading_slash ( from ) data . merge! ( "permalink" => from , "redirect" => { "from" => from , "to" => to =~ %r! ! ? to : absolute_url ( to ) , } ) end
Helper function to set the appropriate path metadata
1,399
def compile parse @fragments = re_raise_with_location { process ( @sexp ) . flatten } @fragments << fragment ( "\n" , nil , s ( :newline ) ) unless @fragments . last . code . end_with? ( "\n" ) @result = @fragments . map ( & :code ) . join ( '' ) end
Compile some ruby code to a string .