Module: NCore::Attributes::ClassMethods
- Defined in:
- lib/ncore/attributes.rb
Instance Method Summary collapse
- #attr(*attrs) ⇒ Object
- #attr_datetime(*attrs) ⇒ Object
- #attr_decimal(*attrs) ⇒ Object
- #check_existing_method(attr) ⇒ Object
- #parse_request_params(params = {}, opts = {}) ⇒ Object
Instance Method Details
#attr(*attrs) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ncore/attributes.rb', line 11 def attr(*attrs) attrs.each do |attr| check_existing_method(attr) class_eval " def \#{attr}\n self[:\#{attr}]\n end\n AR\n end\nend\n", __FILE__, __LINE__+1 |
#attr_datetime(*attrs) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ncore/attributes.rb', line 22 def attr_datetime(*attrs) attrs.each do |attr| check_existing_method(attr) class_eval " def \#{attr}\n case self[:\#{attr}]\n when String\n Time.parse(self[:\#{attr}]).utc\n when Numeric\n Time.at(self[:\#{attr}]).utc\n else\n self[:\#{attr}]\n end\n rescue ArgumentError, TypeError\n self[:\#{attr}]\n end\n AD\n end\nend\n", __FILE__, __LINE__+1 |
#attr_decimal(*attrs) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ncore/attributes.rb', line 42 def attr_decimal(*attrs) attrs.each do |attr| check_existing_method(attr) class_eval " def \#{attr}\n case self[:\#{attr}]\n when String\n BigMoney.new(self[:\#{attr}])\n else\n self[:\#{attr}]\n end\n end\n AD\n end\nend\n", __FILE__, __LINE__+1 |
#check_existing_method(attr) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/ncore/attributes.rb', line 58 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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ncore/attributes.rb', line 66 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 |