Class: MustacheRender::FieldsFilterUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/mustache_render/utils/fields_filter_util.rb

Constant Summary collapse

RULE_JOIN_GAP =
'#'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule = {}) ⇒ FieldsFilterUtil

data = {

:deals => [
   {
     :name  => 'name_1',
     :title => 'title_1',
     :price => 23.4,
     :shops => [
        {
          :name => 'shop_1',
          :address => 'address 1'
        }
     ]
   }
],
:shop => {
  :name => 1,
  :deals => [
    {
       :name => "deal_1"
    }
  ]
},
:deal => {
  :name => ''
}

}

######## filter = {

:default => {
  :deals => {
    :name => true
  }
}

}

转译配置语言



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mustache_render/utils/fields_filter_util.rb', line 45

def initialize rule={}
  @rules = {}
  @rules_tree = {}

  @empty = (
    rule.is_a?(Hash) || rule.is_a?(Array)
  ) ? rule.empty? : true

  parse_rules :rule => rule

  @rules.freeze
  @rules_tree.freeze

  self
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



6
7
8
# File 'lib/mustache_render/utils/fields_filter_util.rb', line 6

def rules
  @rules
end

#rules_treeObject (readonly)

Returns the value of attribute rules_tree.



6
7
8
# File 'lib/mustache_render/utils/fields_filter_util.rb', line 6

def rules_tree
  @rules_tree
end

Instance Method Details

#at(formater = nil) ⇒ Object



83
84
85
# File 'lib/mustache_render/utils/fields_filter_util.rb', line 83

def at(formater=nil)
  @rules[generate_string_formater(formater)]
end

#empty?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/mustache_render/utils/fields_filter_util.rb', line 61

def empty?
  @empty
end

#load(formater = nil) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/mustache_render/utils/fields_filter_util.rb', line 69

def load formater=nil
  if @rules_tree.key?(str = generate_string_formater(formater))
    @rules_tree[str]
  else
    self.class.new
  end
end

#match?(formater = nil) ⇒ Boolean Also known as: hit?

Returns:

  • (Boolean)


77
78
79
# File 'lib/mustache_render/utils/fields_filter_util.rb', line 77

def match?(formater=nil)
  present? && !!at(formater)
end

#present?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/mustache_render/utils/fields_filter_util.rb', line 65

def present?
  !(empty?)
end