Module: Alula::ResourceAttributes

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/alula/resource_attributes.rb', line 4

def self.extended(base)
  base.class_eval do
    @resource_path = nil
    @type = nil
    @http_methods = []
    @fields = {}
  end
  base.include(InstanceMethods)
end

Instance Method Details

#date_fieldsObject



116
117
118
119
120
# File 'lib/alula/resource_attributes.rb', line 116

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



49
50
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
# File 'lib/alula/resource_attributes.rb', line 49

def field(field_name, **opts)
  @fields ||= {}
  @fields[field_name] = opts

  self.instance_eval do
    jsonKey = Util.camelize(field_name)

    # Reader method for attribute
    define_method(field_name) do
      value = @values[jsonKey]

      if opts[:type] == :date && ![nil, ''].include?(value)
        begin
          DateTime.parse(value)
        rescue ArgumentError
          value
        end
      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?(self.program_id) ? value.to_s(16) : value
      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
      end

      #
      # Mark the attribute as dirty if the new value is different
      @dirty_attributes << field_name if @values[jsonKey] != new_value

      #
      # Assign the new value (always assigned even if a duplicate)
      @values[jsonKey] = new_value
    end


  end
end

#field_namesObject



134
135
136
# File 'lib/alula/resource_attributes.rb', line 134

def field_names
  get_fields.keys
end

#filterable_fieldsObject



122
123
124
125
126
# File 'lib/alula/resource_attributes.rb', line 122

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_fieldsObject



106
107
108
# File 'lib/alula/resource_attributes.rb', line 106

def get_fields
  @fields
end

#get_http_methodsObject



43
44
45
# File 'lib/alula/resource_attributes.rb', line 43

def get_http_methods
  @http_methods
end

#get_resource_path(id = nil) ⇒ Object



26
27
28
29
# File 'lib/alula/resource_attributes.rb', line 26

def get_resource_path(id = nil)
  return "#{@resource_path}/#{id}" if id
  @resource_path
end

#get_typeObject



35
36
37
# File 'lib/alula/resource_attributes.rb', line 35

def get_type
  @type
end

#http_methods(methods) ⇒ Object



39
40
41
# File 'lib/alula/resource_attributes.rb', line 39

def http_methods(methods)
  @http_methods = methods
end

#param_keyObject



138
139
140
# File 'lib/alula/resource_attributes.rb', line 138

def param_key
  self.name.gsub('::', '_').downcase
end

#read_only_attributes(record_persisted = false) ⇒ Object



110
111
112
113
114
# File 'lib/alula/resource_attributes.rb', line 110

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



22
23
24
# File 'lib/alula/resource_attributes.rb', line 22

def resource_path(name)
  @resource_path = name
end

#sortable_fieldsObject



128
129
130
131
132
# File 'lib/alula/resource_attributes.rb', line 128

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



31
32
33
# File 'lib/alula/resource_attributes.rb', line 31

def type(type)
  @type = type
end