Module: Servactory::Utils
Instance Method Summary collapse
- #adapt(data) ⇒ Object
- #constantize_class(class_or_name) ⇒ Object
- #define_attribute_with(input: nil, internal: nil, output: nil) ⇒ Object
- #extract_special_character_from(string) ⇒ Object
- #fetch_hash_with_desired_attribute(attribute) ⇒ Object
-
#query_attribute(value) ⇒ Boolean
NOTE: Based on ‘query_cast_attribute` from ActiveRecord: github.com/rails/rails/blob/main/activerecord/lib/active_record/attribute_methods/query.rb.
- #really_input?(attribute = nil) ⇒ Boolean
- #really_internal?(attribute = nil) ⇒ Boolean
- #really_output?(attribute = nil) ⇒ Boolean
- #true?(value) ⇒ Boolean
- #value_present?(value) ⇒ Boolean
Instance Method Details
#adapt(data) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/servactory/utils.rb', line 7 def adapt(data) if defined?(Datory::Base) && data.is_a?(Datory::Base) data = Servactory::Utils.send(:instance_variables_to_hash_from, data) end data.symbolize_keys end |
#constantize_class(class_or_name) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/servactory/utils.rb', line 104 def constantize_class(class_or_name) case class_or_name when String, Symbol begin Object.const_get(class_or_name) rescue NameError class_or_name.safe_constantize end else class_or_name end end |
#define_attribute_with(input: nil, internal: nil, output: nil) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/servactory/utils.rb', line 23 def define_attribute_with(input: nil, internal: nil, output: nil) return input if really_input?(input) return internal if really_internal?(internal) return output if really_output?(output) raise ArgumentError, "missing keyword: :input, :internal or :output" end |
#extract_special_character_from(string) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/servactory/utils.rb', line 49 def extract_special_character_from(string) if string.end_with?("!", "?") [string.chop, string[-1]] else [string, nil] end end |
#fetch_hash_with_desired_attribute(attribute) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/servactory/utils.rb', line 15 def fetch_hash_with_desired_attribute(attribute) return { input: attribute.class::Actor.new(attribute) } if really_input?(attribute) return { internal: attribute.class::Actor.new(attribute) } if really_internal?(attribute) return { output: attribute.class::Actor.new(attribute) } if really_output?(attribute) raise ArgumentError, "Failed to define attribute" end |
#query_attribute(value) ⇒ Boolean
NOTE: Based on ‘query_cast_attribute` from ActiveRecord:
https://github.com/rails/rails/blob/main/activerecord/lib/active_record/attribute_methods/query.rb
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/servactory/utils.rb', line 89 def query_attribute(value) # rubocop:disable Metrics/MethodLength, Naming/PredicateMethod case value when true then true when false, nil then false else if value.is_a?(Numeric) || (value.respond_to?(:match?) && !value.match?(/[^0-9]/)) !value.to_i.zero? else return false if FALSE_VALUES.include?(value) value.present? end end end |
#really_input?(attribute = nil) ⇒ Boolean
31 32 33 34 35 |
# File 'lib/servactory/utils.rb', line 31 def really_input?(attribute = nil) return true if attribute.present? && attribute.input? false end |
#really_internal?(attribute = nil) ⇒ Boolean
37 38 39 40 41 |
# File 'lib/servactory/utils.rb', line 37 def really_internal?(attribute = nil) return true if attribute.present? && attribute.internal? false end |
#really_output?(attribute = nil) ⇒ Boolean
43 44 45 46 47 |
# File 'lib/servactory/utils.rb', line 43 def really_output?(attribute = nil) return true if attribute.present? && attribute.output? false end |
#true?(value) ⇒ Boolean
73 74 75 |
# File 'lib/servactory/utils.rb', line 73 def true?(value) FALSE_VALUES.exclude?(value) end |
#value_present?(value) ⇒ Boolean
79 80 81 82 83 |
# File 'lib/servactory/utils.rb', line 79 def value_present?(value) !value.nil? && ( value.respond_to?(:empty?) ? !value.empty? : true ) end |