Module: Alula::Dcp::ResourceAttributes

Included in:
Config::Synchronize, ObjectField, UsersData::InstallerPin, UsersData::User
Defined in:
lib/alula/dcp/resource_attributes.rb

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/alula/dcp/resource_attributes.rb', line 6

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



58
59
60
61
62
63
# File 'lib/alula/dcp/resource_attributes.rb', line 58

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

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

#field_namesObject



77
78
79
# File 'lib/alula/dcp/resource_attributes.rb', line 77

def field_names
  get_fields.keys
end

#filterable_fieldsObject



65
66
67
68
69
# File 'lib/alula/dcp/resource_attributes.rb', line 65

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



54
55
56
# File 'lib/alula/dcp/resource_attributes.rb', line 54

def get_fields
  @fields
end

#get_http_methodsObject



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

def get_http_methods
  @http_methods
end

#get_resource_path(index = nil) ⇒ Object



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

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

  @resource_path
end

#get_typeObject



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

def get_type
  @type
end

#http_methods(methods) ⇒ Object



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

def http_methods(methods)
  @http_methods = methods
end

#param_keyObject



81
82
83
# File 'lib/alula/dcp/resource_attributes.rb', line 81

def param_key
  name.gsub('::', '_').downcase
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.


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

def resource_path(name)
  @resource_path = name
end

#sortable_fieldsObject



71
72
73
74
75
# File 'lib/alula/dcp/resource_attributes.rb', line 71

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/dcp/resource_attributes.rb', line 31

def type(type)
  @type = type
end