Module: NCore::Attributes::ClassMethods

Defined in:
lib/ncore/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attr(*attrs, predicate: true) ⇒ Object

attr(:name, …)

adds: obj.name  => raw json type
      obj.name? => bool


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ncore/attributes.rb', line 14

def attr(*attrs, predicate: true)
  attrs.each do |attr|
    check_existing_method(attr)
    class_eval <<-AR, __FILE__, __LINE__+1
      def #{attr}
        self[:#{attr}]
      end
    AR
    attr_boolean :"#{attr}?" if predicate
  end
end

#attr_boolean(*attrs) ⇒ Object

attr_boolean(:active, :active?, …)

adds: obj.active
adds: obj.active? - in attrs hash, this looks for the key :active, not :active?


73
74
75
76
77
78
79
80
81
82
# File 'lib/ncore/attributes.rb', line 73

def attr_boolean(*attrs)
  attrs.each do |attr|
    check_existing_method(attr)
    class_eval <<-AB, __FILE__, __LINE__+1
      def #{attr}
        !! self[:#{attr.to_s.sub(/\?$/,'')}]
      end
    AB
  end
end

#attr_datetime(*attrs, predicate: true) ⇒ Object

attr_datetime(:updated_at, …)

adds: obj.updated_at  => Time, or raw json type if not parseable
      obj.updated_at? => bool


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ncore/attributes.rb', line 29

def attr_datetime(*attrs, predicate: true)
  attrs.each do |attr|
    check_existing_method(attr)
    class_eval <<-AD, __FILE__, __LINE__+1
      def #{attr}
        case self[:#{attr}]
        when String
          Time.parse(self[:#{attr}]).utc
        when Numeric
          Time.at(self[:#{attr}]).utc
        else
          self[:#{attr}]
        end
      rescue ArgumentError, TypeError
        self[:#{attr}]
      end
    AD
    attr_boolean :"#{attr}?" if predicate
  end
end

#attr_decimal(*attrs, predicate: true) ⇒ Object

attr_decimal(:amount, …)

adds: obj.amount  => BigMoney if String, else raw json type
      obj.amount? => bool


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ncore/attributes.rb', line 53

def attr_decimal(*attrs, predicate: true)
  attrs.each do |attr|
    check_existing_method(attr)
    class_eval <<-AD, __FILE__, __LINE__+1
      def #{attr}
        case self[:#{attr}]
        when String
          BigMoney.new(self[:#{attr}])
        else
          self[:#{attr}]
        end
      end
    AD
    attr_boolean :"#{attr}?" if predicate
  end
end

#check_existing_method(attr) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/ncore/attributes.rb', line 84

def check_existing_method(attr)
  if method_defined?(attr) || private_method_defined?(attr)
    sc = self
    sc = sc.superclass while sc.superclass != Object
    warn "Warning: Existing method #{sc.name}##{attr} being overwritten at #{caller[3]}"
  end
end

#parse_request_params(params = {}, opts = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ncore/attributes.rb', line 92

def parse_request_params(params={}, opts={})
  params = params.with_indifferent_access
  req = params.delete(:request)
  hdr = params.delete(:headers)
  creds = params.delete(:credentials)
  cache = params.delete(:cache)
  if opts[:json_root]
    if params.key?(opts[:json_root])
      o = params
    else
      o = {opts[:json_root] => params}.with_indifferent_access
    end
  else
    o = params
  end
  o[:request] = req if req
  o[:headers] = hdr if hdr
  o[:credentials] = creds if creds
  o[:cache] = cache if cache
  o
end