-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4973858
commit 0416cf4
Showing
18 changed files
with
413 additions
and
108 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
require 'probe_dock_ruby' | ||
require 'probedock_ruby' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
module ProbeDockProbe | ||
module Configurable | ||
def self.included mod | ||
mod.extend ClassMethods | ||
end | ||
|
||
def initialize attrs = {} | ||
set_configurable_attrs(nil_attrs.merge(attrs.kind_of?(Hash) ? attrs : {})) | ||
end | ||
|
||
def empty? | ||
self.class.configurable_attrs.all? do |attr| | ||
value = send(attr) | ||
value.nil? || value.respond_to?(:empty?) && value.empty? | ||
end | ||
end | ||
|
||
def update attrs = {} | ||
set_configurable_attrs(attrs) | ||
end | ||
|
||
def clear | ||
self.class.configurable_attrs.each do |attr| | ||
value = send(attr) | ||
if value.kind_of?(Configurable) | ||
value.clear | ||
else | ||
send("#{attr}=", nil) | ||
end | ||
end | ||
end | ||
|
||
def to_h | ||
self.class.configurable_attrs.inject({}) do |memo,attr| | ||
|
||
value = send(attr) | ||
if value.kind_of?(Configurable) | ||
memo[attr] = value.to_h | ||
elsif !attr_empty?(attr) | ||
memo[attr] = value | ||
end | ||
|
||
memo | ||
end | ||
end | ||
|
||
private | ||
|
||
def attr_empty?(attr) | ||
value = send(attr) | ||
value.nil? || value.respond_to?(:empty?) && value.empty? | ||
end | ||
|
||
def nil_attrs | ||
self.class.configurable_attrs.inject({}) do |memo,attr| | ||
memo[attr] = nil | ||
memo | ||
end | ||
end | ||
|
||
def set_boolean attr, value | ||
instance_variable_set("@#{attr}", value.nil? ? nil : !!value) | ||
end | ||
|
||
def set_integer attr, value | ||
instance_variable_set("@#{attr}", value.nil? ? nil : value.to_s.to_i) | ||
end | ||
|
||
def set_string attr, value | ||
instance_variable_set("@#{attr}", value.nil? ? nil : value.to_s) | ||
end | ||
|
||
def set_string_array attr, value | ||
instance_variable_set("@#{attr}", wrap(value).compact.collect(&:to_s)) | ||
end | ||
|
||
def set_configurable klass, attr, value | ||
variable = "@#{attr}" | ||
if configurable = instance_variable_get(variable) | ||
configurable.update value | ||
else | ||
instance_variable_set("@#{attr}", klass.new(value)) | ||
end | ||
end | ||
|
||
def set_configurable_attrs attrs = {} | ||
return self unless attrs.kind_of?(Hash) | ||
|
||
self.class.configurable_attrs.each do |attr| | ||
send("#{attr}=", attrs[attr]) if attrs.key?(attr) | ||
end | ||
|
||
self | ||
end | ||
|
||
def wrap a | ||
a.kind_of?(Array) ? a : [ a ] | ||
end | ||
|
||
module ClassMethods | ||
def configurable attr_definitions = {} | ||
|
||
@configurable_attrs = attr_definitions.keys | ||
|
||
attr_definitions.each do |attr,type| | ||
|
||
setter = if type.kind_of?(Class) && type.included_modules.include?(Configurable) | ||
:configurable | ||
elsif type.kind_of?(Symbol) | ||
type | ||
else | ||
raise "Unsupported type of configurable attribute #{type.inspect}; must be either a symbol or a configurable class" | ||
end | ||
|
||
attr_reader attr | ||
|
||
define_method "#{attr}=" do |value| | ||
if setter == :configurable | ||
send :set_configurable, type, attr, value | ||
else | ||
send "set_#{type}", attr, value | ||
end | ||
end | ||
end | ||
end | ||
|
||
def configurable_attrs *attrs | ||
@configurable_attrs || [] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require File.join(File.dirname(__FILE__), 'configurable.rb') | ||
|
||
module ProbeDockProbe | ||
class Project | ||
include Configurable | ||
|
||
configurable({ | ||
api_id: :string, | ||
version: :string, | ||
category: :string, | ||
tags: :string_array, | ||
tickets: :string_array | ||
}) | ||
|
||
def validate! | ||
required = { "version" => @version, "API identifier" => @api_id } | ||
missing = required.inject([]){ |memo,(k,v)| v.to_s.strip.length <= 0 ? memo << k : memo } | ||
raise PayloadError.new("Missing project options: #{missing.join ', '}") if missing.any? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'ostruct' | ||
require File.join(File.dirname(__FILE__), 'configurable.rb') | ||
|
||
module ProbeDockProbe | ||
class ScmRemoteUrl | ||
include Configurable | ||
|
||
configurable({ | ||
fetch: :string, | ||
push: :string | ||
}) | ||
end | ||
|
||
class ScmRemote | ||
include Configurable | ||
|
||
configurable({ | ||
name: :string, | ||
ahead: :integer, | ||
behind: :integer, | ||
url: ScmRemoteUrl | ||
}) | ||
end | ||
|
||
class Scm | ||
include Configurable | ||
|
||
configurable({ | ||
name: :string, | ||
version: :string, | ||
dirty: :boolean, | ||
remote: ScmRemote | ||
}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.