Class: Solid::ModelDrop

Inherits:
Liquid::Drop
  • Object
show all
Defined in:
lib/solid/model_drop.rb

Defined Under Namespace

Modules: ModelExtension

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_scope = nil, context = nil) ⇒ ModelDrop

Returns a new instance of ModelDrop.



68
69
70
71
# File 'lib/solid/model_drop.rb', line 68

def initialize(base_scope=nil, context=nil)
  @scope = base_scope
  @context ||= context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



115
116
117
# File 'lib/solid/model_drop.rb', line 115

def method_missing(name, *args, &block)
  before_method(name.to_s)
end

Class Method Details

.allow_scopes(*scopes) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/solid/model_drop.rb', line 50

def allow_scopes(*scopes)
  @allowed_scopes = scopes
  scopes.each do |scope_name|
    self.class_eval <<-END_EVAL, __FILE__, __LINE__ + 1
      def #{scope_name}(*args)
        @scope = scope.public_send(:#{scope_name}, *args)
      end
    END_EVAL
    self.immutable_method(scope_name)
  end
end

.immutable_method(method_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/solid/model_drop.rb', line 32

def immutable_method(method_name)
  self.class_eval <<-END_EVAL, __FILE__, __LINE__ + 1
    def #{method_name}_with_immutation(*args, &block)
      self.dup.tap do |clone|
        clone.#{method_name}_without_immutation(*args, &block)
      end
    end
  END_EVAL
  self.alias_method_chain method_name, :immutation
end

.model(model_name = nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/solid/model_drop.rb', line 20

def model(model_name=nil)
  if model_name
    @model_name = model_name
  else
    @model_name ||= self.name.gsub(/Drop$/, '')
  end
end

.model_classObject



28
29
30
# File 'lib/solid/model_drop.rb', line 28

def model_class
  @model_class ||= self.model.to_s.camelize.constantize
end

.respond(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
# File 'lib/solid/model_drop.rb', line 43

def respond(options={})
  raise ArgumentError.new(":to option should be a Regexp") unless options[:to].is_a?(Regexp)
  raise ArgumentError.new(":with option is mandatory") unless options[:with].present?
  self.dynamic_methods ||= []
  self.dynamic_methods += [[options[:to], options[:with]]]
end

Instance Method Details

#allObject



73
74
75
# File 'lib/solid/model_drop.rb', line 73

def all
  self
end

#before_method(method_name, *args) ⇒ Object

Raises:

  • (NoMethodError)


81
82
83
84
85
86
87
88
# File 'lib/solid/model_drop.rb', line 81

def before_method(method_name, *args)
  self.class.dynamic_methods.each do |pattern, method|
    if match_data = pattern.match(method_name)
      return self.send(method, *match_data[1..-1])
    end
  end
  raise NoMethodError.new("undefined method `#{method_name}' for #{self.inspect}")
end

#each(&block) ⇒ Object



77
78
79
# File 'lib/solid/model_drop.rb', line 77

def each(&block)
  scope.each(&block)
end