Module: Alula::ResourceAttributes
- Included in:
- BillingProgram, ConfigTemplate, Dealer, DealerAccountTransfer, DealerAddress, DealerBranding, DealerPhone, DealerProgram, DealerSuspensionLog, Device, DeviceAlias, DeviceCellularStatus, DeviceCharge, DeviceCredit, DeviceEventLog, DeviceFeaturePrice, DeviceNotification, DeviceProgram, DeviceUser, EventTrigger, EventWebhook, FeatureBySubject, FeaturePlan, FeaturePlanVideo, FeaturePrice, HelixUser, NfcTag, OAuthClient, ObjectField, Receiver, ReceiverBind, ReceiverConnection, ReceiverDisabled, ReceiverGroup, Revision, Self, Station, User, UserAddress, UserPhone, UserPreferences, UserPushtoken, UserVideoProfile, Video::BaseResource, VideoVerificationCamera, VideoVerificationEvent
- Defined in:
- lib/alula/resource_attributes.rb
Defined Under Namespace
Modules: InstanceMethods
Class Method Summary collapse
Instance Method Summary collapse
- #date_fields ⇒ Object
-
#field(field_name, **opts) ⇒ Object
Memoizes field names and options.
- #field_names ⇒ Object
- #filterable_fields ⇒ Object
- #get_fields ⇒ Object
- #get_http_methods ⇒ Object
- #get_resource_path(id = nil) ⇒ Object
- #get_type ⇒ Object
- #http_methods(methods) ⇒ Object
- #param_key ⇒ Object
- #read_only_attributes(record_persisted = false) ⇒ Object
-
#resource_path(name) ⇒ Object
Class methods for defining how fields and types work NOTE: We’re not using real getters and setters here.
- #sortable_fields ⇒ Object
- #type(type) ⇒ Object
Class Method Details
.extended(base) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/alula/resource_attributes.rb', line 4 def self.extended(base) base.class_eval do @resource_path = nil @type = nil @http_methods = [] @fields = {} def mark_dirty(field_name, old_value, new_value) @dirty_attributes << field_name if old_value != new_value end end base.include(InstanceMethods) end |
Instance Method Details
#date_fields ⇒ Object
123 124 125 126 127 |
# File 'lib/alula/resource_attributes.rb', line 123 def date_fields get_fields.each_pair.each_with_object([]) do |(field_name, opts), collector| collector << field_name if opts[:type].to_sym == :date end end |
#field(field_name, **opts) ⇒ Object
Memoizes field names and options
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/alula/resource_attributes.rb', line 51 def field(field_name, **opts) @fields ||= {} @fields[field_name] = opts self.instance_eval do json_key = Util.camelize(field_name) # Reader method for attribute define_method(field_name) do value = @values[json_key] if opts[:type] == :date && ![nil, ''].include?(value) begin DateTime.parse(value) rescue ArgumentError value end elsif opts[:type] == :object && opts[:use] && !value.nil? && value.respond_to?(:each) opts[:use].new(@dirty_attributes, field_name, value) elsif opts[:type] == :dcp_object && opts[:use] opts[:use].new(nil, nil, value) elsif opts[:type] == :boolean [true, 'true', 1, '1'].include? value elsif opts[:symbolize] == true # API sends a camelCase string; provide symbol to Client value ? Util.underscore(value).to_sym : nil elsif opts[:hex_convert_required] == true Util.convert_hex_crc?(program_id) ? value.to_s(16) : value elsif opts[:type] == :number value&.to_i else value end end # Setter method define_method("#{field_name}=") do |new_value| # # Coerce 'blank like' fields into a defined blank value # This helps HTML form submissions. A blank text field comes through as an # empty string, instead of a nil as the API validates for. new_value = nil if new_value == '' if opts[:symbolize] == true # Client provides a symbol; send camelCase string to API new_value = Util.camelize(new_value.to_s) elsif opts[:type] == :boolean new_value = [true, 'true', 1, '1'].include? new_value elsif opts[:type] == :dcp_object old_val = @values.deep_dup new_value = send(field_name).apply_attributes(new_value, merge_only: true) mark_dirty(field_name, old_val[json_key], new_value) end # Mark the attribute as dirty if the new value is different mark_dirty(field_name, @values[json_key], new_value) # # Assign the new value (always assigned even if a duplicate) @values[json_key] = new_value end end end |
#field_names ⇒ Object
141 142 143 |
# File 'lib/alula/resource_attributes.rb', line 141 def field_names get_fields.keys end |
#filterable_fields ⇒ Object
129 130 131 132 133 |
# File 'lib/alula/resource_attributes.rb', line 129 def filterable_fields get_fields.each_pair.each_with_object([]) do |(field_name, opts), collector| collector << field_name if opts[:filterable] == true end end |
#get_fields ⇒ Object
113 114 115 |
# File 'lib/alula/resource_attributes.rb', line 113 def get_fields @fields end |
#get_http_methods ⇒ Object
45 46 47 |
# File 'lib/alula/resource_attributes.rb', line 45 def get_http_methods @http_methods end |
#get_resource_path(id = nil) ⇒ Object
27 28 29 30 31 |
# File 'lib/alula/resource_attributes.rb', line 27 def get_resource_path(id = nil) return "#{@resource_path}/#{id}" if id @resource_path end |
#get_type ⇒ Object
37 38 39 |
# File 'lib/alula/resource_attributes.rb', line 37 def get_type @type end |
#http_methods(methods) ⇒ Object
41 42 43 |
# File 'lib/alula/resource_attributes.rb', line 41 def http_methods(methods) @http_methods = methods end |
#param_key ⇒ Object
145 146 147 |
# File 'lib/alula/resource_attributes.rb', line 145 def param_key self.name.gsub('::', '_').downcase end |
#read_only_attributes(record_persisted = false) ⇒ Object
117 118 119 120 121 |
# File 'lib/alula/resource_attributes.rb', line 117 def read_only_attributes(record_persisted = false) get_fields.each_pair.each_with_object([]) do |(field_name, opts), collector| collector << field_name if !field_patchable?(opts, record_persisted) end end |
#resource_path(name) ⇒ Object
Class methods for defining how fields and types work NOTE: We’re not using real getters and setters here. I want the method signature
to match how most other Ruby class configuration is done, and that is with
simple methods that take params.
23 24 25 |
# File 'lib/alula/resource_attributes.rb', line 23 def resource_path(name) @resource_path = name end |
#sortable_fields ⇒ Object
135 136 137 138 139 |
# File 'lib/alula/resource_attributes.rb', line 135 def sortable_fields get_fields.each_pair.each_with_object([]) do |(field_name, opts), collector| collector << field_name if opts[:sortable] == true end end |
#type(type) ⇒ Object
33 34 35 |
# File 'lib/alula/resource_attributes.rb', line 33 def type(type) @type = type end |