Module: Locomotive::Presentable::ClassMethods

Defined in:
lib/locomotive/presentable.rb

Instance Method Summary collapse

Instance Method Details

#alias_of(alias_name) ⇒ String

Get the name of the property for which the property passed in parameter is an alias.

Parameters:

  • alias_name (String)

    Name of the alias

Returns:

  • (String)

    The original property



201
202
203
204
205
206
# File 'lib/locomotive/presentable.rb', line 201

def alias_of(alias_name)
  self.setters.find do |name|
    list = [*(self.property_options[name] || {})[:alias]] || []
    list.include?(alias_name.to_sym)
  end
end

#collection(name, options = {}) ⇒ Object

Add a collection to the current instance. It creates getter/setter mapped to the collection of the source object.

Parameters:

  • name (String)

    The name of the collection

  • options (Hash) (defaults to: {})

    The options related to the collection (:alias)



156
157
158
# File 'lib/locomotive/presentable.rb', line 156

def collection(name, options = {})
  property(name, options.merge(:collection => true))
end

#define_getter(name, collection = false) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/locomotive/presentable.rb', line 160

def define_getter(name, collection = false)
  (@getters ||= []) << name.to_s

  class_eval <<-EOV
    def #{name}
      if #{collection.to_s}
        list = self.source.send(:#{name})
        list ? list.map(&:as_json) : []
      else
        self.source.send(:#{name})
      end
    end
  EOV
end

#define_setter(name, aliases = []) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/locomotive/presentable.rb', line 175

def define_setter(name, aliases = [])
  (@setters ||= []) << name.to_s
  @setters += aliases.map(&:to_s)

  class_eval <<-EOV
    def #{name}=(value)
      self.source.send(:#{name}=, value)
    end
  EOV

  aliases.each do |_name|
    class_eval <<-EOV
      def #{_name}=(value)
        self.#{name} = value
      end
    EOV
  end
end

#inherited(subclass) ⇒ Object

Override inherited in order to copy the parent list of getters, setters and property options.

Parameters:

  • subclass (Class)

    The subclass inheriting from the current class



106
107
108
109
110
111
112
# File 'lib/locomotive/presentable.rb', line 106

def inherited(subclass)
  subclass.getters = getters.clone if getters
  subclass.setters = setters.clone if setters
  subclass.property_options = property_options.clone if property_options

  super
end

#properties(*names) ⇒ Object

Add multiple properties all in once. If th last property name is a hash, then it will be used as the options for all the other properties.

Parameters:

  • args (Array)

    List of property names



120
121
122
123
124
125
126
# File 'lib/locomotive/presentable.rb', line 120

def properties(*names)
  options = names.last.is_a?(Hash) ? names.pop : {}

  names.each do |name|
    property(name, options)
  end
end

#property(name, options = {}) ⇒ Object

Add a property to the current instance. It creates getter/setter methods related to that property. By default, the getter and setter are bound to the source object.

Parameters:

  • name (String)

    The name of the property

  • options (Hash) (defaults to: {})

    The options related to the property.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/locomotive/presentable.rb', line 135

def property(name, options = {})
  aliases     = [*options[:alias]]
  collection  = options[:collection] == true

  (@property_options ||= {})[name.to_s] = options

  unless options[:only_setter] == true
    define_getter(name, collection)
  end

  unless options[:only_getter] == true
    define_setter(name, aliases)
  end
end